generated from pagopa/template-java-spring-microservice
-
Notifications
You must be signed in to change notification settings - Fork 0
289 lines (253 loc) · 11.5 KB
/
07_report_generation.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
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
- massive_generation
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@65d7f2d534ac1bc67fcd62888c5f4f3d2cb2b236 # 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: Massively generates reports, up to 5 days after the date
if: ${{ env.operation == 'massive_generation' }}
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_massive_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 }}