Skip to content

Generate and send report #55

Generate and send report

Generate and send report #55

name: Generate and send report
on:
schedule:
# scheduled generation cron
- cron: '00 01 * * *' # (01:00AM GTM) generate last day report
- cron: '00 06 * * 1' # (06:00AM GTM) generate last week report on monday
- cron: '30 06 1 * *' # (06:00AM GTM) generate last month report on first day of the month
# scheduled send cron
- cron: '00 07 * * *' # (07:00AM GTM) send last day report
- cron: '30 07 * * 1' # (07:30AM GTM) send last week report on monday
- cron: '35 07 1 * *' # (07:35AM GTM) send last month report on first day of the month
workflow_dispatch:
inputs:
environment:
required: true
type: choice
description: Select the environment
options:
- uat
- prod
default: prod
report_type:
required: true
type: choice
description: Select the report type
options:
- daily
- weekly
- monthly
default: daily
report_date:
required: false
type: string
description: Define a specific date in yyyy-MM-dd
report_operation:
required: true
type: choice
description: Select the type of operation
options:
- generate
- send
- both
default: both
permissions:
id-token: write
contents: read
deployments: write
jobs:
approve_create_runner:
name: Execute auto-approve for 'Create Runner'
runs-on: ubuntu-latest
environment:
name: ${{ github.event.inputs == null || github.event.inputs == 'prod' }}
steps:
- name: Auto approve
uses: andrea-deri/auto-approve@042a1e58fa51acef74997dc9cebb1c665677d2ac
with:
pat_token: ${{ secrets.BOT_TOKEN_GITHUB }}
environment: ${{ inputs.environment || 'prod' }}
create_runner:
name: Create Runner
runs-on: ubuntu-22.04
environment:
name: ${{(github.event.inputs == null && 'prod') || inputs.environment }}
outputs:
runner_name: ${{ steps.create_github_runner.outputs.runner_name }}
steps:
- name: Create GitHub Runner
id: create_github_runner
# from https://github.com/pagopa/eng-github-actions-iac-template/tree/main/azure/github-self-hosted-runner-azure-create-action
uses: pagopa/eng-github-actions-iac-template/azure/github-self-hosted-runner-azure-create-action@main
with:
client_id: ${{ secrets.CD_CLIENT_ID }}
tenant_id: ${{ secrets.TENANT_ID }}
subscription_id: ${{ secrets.SUBSCRIPTION_ID }}
container_app_environment_name: ${{ vars.CONTAINER_APP_ENVIRONMENT_NAME }}
resource_group_name: ${{ vars.CONTAINER_APP_ENVIRONMENT_RESOURCE_GROUP_NAME }} # RG of the runner
pat_token: ${{ secrets.BOT_TOKEN_GITHUB }}
self_hosted_runner_image_tag: "latest"
approve_report_generation:
needs: [ create_runner ]
name: Execute auto-approve for 'Generate and/or send report' job
runs-on: ubuntu-latest
environment:
name: ${{ github.event.inputs == null || github.event.inputs == 'prod' }}
steps:
- name: Auto approve
uses: andrea-deri/auto-approve@042a1e58fa51acef74997dc9cebb1c665677d2ac
with:
pat_token: ${{ secrets.BOT_TOKEN_GITHUB }}
environment: ${{ inputs.environment || 'prod' }}
report_generation:
needs: [ create_runner ]
name: Generate and/or send report
runs-on: [ self-hosted, "${{ needs.create_runner.outputs.runner_name }}" ]
environment:
name: ${{(github.event.inputs == null && 'prod') || inputs.environment }}
steps:
- name: Generating daily report via scheduling
if: github.event.schedule == '00 01 * * *'
run: |
echo "Generating a daily report"
echo "report_type=daily" >> $GITHUB_ENV
echo "operation=generate" >> $GITHUB_ENV
echo "environment=prod" >> $GITHUB_ENV
- name: Generating weekly report via scheduling
if: github.event.schedule == '00 06 * * 1'
run: |
echo "Generating a weekly report"
echo "report_type=weekly" >> $GITHUB_ENV
echo "operation=generate" >> $GITHUB_ENV
echo "environment=prod" >> $GITHUB_ENV
- name: Generating monthly report via scheduling
if: github.event.schedule == '30 06 1 * *'
run: |
echo "Generating a monthly report"
echo "report_type=monthly" >> $GITHUB_ENV
echo "operation=generate" >> $GITHUB_ENV
echo "environment=prod" >> $GITHUB_ENV
- name: Sending daily report via scheduling
if: github.event.schedule == '00 07 * * *'
run: |
echo "Generating a daily report"
echo "report_type=daily" >> $GITHUB_ENV
echo "operation=send" >> $GITHUB_ENV
echo "environment=prod" >> $GITHUB_ENV
- name: Sending weekly report via scheduling
if: github.event.schedule == '30 07 * * 1'
run: |
echo "Generating a weekly report"
echo "report_type=weekly" >> $GITHUB_ENV
echo "operation=send" >> $GITHUB_ENV
echo "environment=prod" >> $GITHUB_ENV
- name: Sending monthly report via scheduling
if: github.event.schedule == '35 07 1 * *'
run: |
echo "Sending a monthly report"
echo "report_type=monthly" >> $GITHUB_ENV
echo "operation=send" >> $GITHUB_ENV
echo "environment=prod" >> $GITHUB_ENV
- name: Generating and sending report via manual trigger
if: github.event_name != 'schedule'
run: |
echo "Generating and sending a report via manual trigger"
echo "operation=${{ github.event.inputs.report_operation }}" >> $GITHUB_ENV
- name: Generate variables
id: generate_vars
run: |
echo "environment=${{ env.environment || github.event.inputs.environment }}" >> $GITHUB_ENV
echo "report_date=${{ env.report_date || github.event.inputs.report_date }}" >> $GITHUB_ENV
echo "report_type=${{ env.report_type || github.event.inputs.report_type }}" >> $GITHUB_ENV
- name: Checkout
id: checkout
uses: actions/checkout@1f9a0c22da41e6ebfa534300ef656657ea2c6707
- name: Login
id: login
# from https://github.com/Azure/login/commits/master
uses: azure/login@92a5484dfaf04ca78a94597f4f19fea633851fa2
with:
client-id: ${{ secrets.CD_CLIENT_ID }}
tenant-id: ${{ secrets.TENANT_ID }}
subscription-id: ${{ secrets.SUBSCRIPTION_ID }}
- name: Setup Python environment
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies on Python environment
run: |
cd ./scripts/report-generation
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Generate report
if: ${{ env.operation == 'both' || env.operation == 'generate' }}
run: |
cd ./scripts/report-generation
export REPORT_ENV="${{ env.environment }}" \
REPORT_TYPE="${{ env.report_type }}" \
REPORT_DATE="${{ env.report_date }}" \
REPORT_SLACK_WEBHOOK_URL="${{ secrets.REPORT_SLACK_WEBHOOK_URL }}" \
REPORT_DATAEXPLORER_URL="${{ vars.REPORT_DATAEXPLORER_URL }}" \
REPORT_DATAEXPLORER_CLIENT_ID="${{ secrets.REPORT_DATAEXPLORER_CLIENT_ID }}" \
REPORT_DATAEXPLORER_CLIENT_SECRET="${{ secrets.REPORT_DATAEXPLORER_CLIENT_SECRET }}" \
REPORT_DATAEXPLORER_TENANT_ID="${{ secrets.TENANT_ID }}" \
REPORT_DATABASE_URL="${{ vars.REPORT_DATABASE_URL }}" \
REPORT_DATABASE_KEY="${{ secrets.REPORT_DATABASE_KEY }}" \
REPORT_DATABASE_REGION="${{ vars.REPORT_DATABASE_REGION }}" \
REPORT_APICONFIG_CACHE_SUBKEY="${{ secrets.REPORT_APICONFIG_CACHE_SUBKEY }}"
python run_extraction.py
- name: Send report
if: ${{ env.operation == 'both' || env.operation == 'send' }}
run: |
cd ./scripts/report-generation
export REPORT_ENV="${{ env.environment }}" \
REPORT_TYPE="${{ env.report_type }}" \
REPORT_DATE="${{ env.report_date }}" \
REPORT_SLACK_WEBHOOK_URL="${{ secrets.REPORT_SLACK_WEBHOOK_URL }}" \
REPORT_DATAEXPLORER_URL="${{ vars.REPORT_DATAEXPLORER_URL }}" \
REPORT_DATAEXPLORER_CLIENT_ID="${{ secrets.REPORT_DATAEXPLORER_CLIENT_ID }}" \
REPORT_DATAEXPLORER_CLIENT_SECRET="${{ secrets.REPORT_DATAEXPLORER_CLIENT_SECRET }}" \
REPORT_DATAEXPLORER_TENANT_ID="${{ secrets.TENANT_ID }}" \
REPORT_DATABASE_URL="${{ vars.REPORT_DATABASE_URL }}" \
REPORT_DATABASE_KEY="${{ secrets.REPORT_DATABASE_KEY }}" \
REPORT_DATABASE_REGION="${{ vars.REPORT_DATABASE_REGION }}" \
REPORT_APICONFIG_CACHE_SUBKEY="${{ secrets.REPORT_APICONFIG_CACHE_SUBKEY }}"
python run_send.py
approve_cleanup_runner:
needs: [ report_generation ]
name: Execute auto-approve for 'Cleanup Runner' job
runs-on: ubuntu-latest
environment:
name: ${{ github.event.inputs == null || github.event.inputs == 'prod' }}
steps:
- name: Auto approve
uses: andrea-deri/auto-approve@042a1e58fa51acef74997dc9cebb1c665677d2ac
with:
pat_token: ${{ secrets.BOT_TOKEN_GITHUB }}
environment: ${{ inputs.environment || 'prod' }}
cleanup_runner:
name: Cleanup Runner
needs: [ create_runner, report_generation ]
if: ${{ success() }}
runs-on: ubuntu-22.04
environment:
name: ${{(github.event.inputs == null && 'prod') || inputs.environment }}
steps:
- name: Cleanup GitHub Runner
id: cleanup_github_runner
# from https://github.com/pagopa/eng-github-actions-iac-template/tree/main/azure/github-self-hosted-runner-azure-cleanup-action
uses: pagopa/eng-github-actions-iac-template/azure/github-self-hosted-runner-azure-cleanup-action@0ee2f58fd46d10ac7f00bce4304b98db3dbdbe9a
with:
client_id: ${{ secrets.CD_CLIENT_ID }}
tenant_id: ${{ secrets.TENANT_ID }}
subscription_id: ${{ secrets.SUBSCRIPTION_ID }}
resource_group_name: ${{ vars.CONTAINER_APP_ENVIRONMENT_RESOURCE_GROUP_NAME }}
runner_name: ${{ needs.create_runner.outputs.runner_name }}
pat_token: ${{ secrets.BOT_TOKEN_GITHUB }}