-
Notifications
You must be signed in to change notification settings - Fork 0
424 lines (388 loc) Β· 15.6 KB
/
cicd.yaml
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
name: CI CD Pipeline
on:
push:
branches:
- main
- master
jobs:
sonarqube:
name: Sonarqube Analysis
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: SonarQube Scan
uses: sonarsource/sonarqube-scan-action@master
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
- name: SonarQube Quality Gate check
id: sonarqube-quality-gate-check
uses: opsverseio/[email protected]
# Force to fail step after specific time.
timeout-minutes: 5
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
SET_SONAR_PROJECT_STATUS: true
METRIC_NAME: "new_coverage"
- name: "πβοΈ Fetch Reliability Rating Metric"
id: fetch-reliability-rating
run: |
# Fetching data from SonarQube
response=$(curl --location --location-trusted --max-redirs 10 --silent --fail --show-error --user ${{ secrets.SONARQUBE_TOKEN }}: "${{ secrets.SONAR_HOST_URL }}/api/measures/component?component=OpsVerseIO_hello-world-api-dotnet_fbb1f479-d532-46dd-b86d-c63f33391fe3&metricKeys=reliability_rating")
# Extracting reliability ratings
reliability_score=$(echo $response | jq -r '.component.measures[] | select(.metric=="reliability_rating") | .value')
best_reliability_score=$(echo $response | jq -r '.component.measures[] | select(.metric=="security_rating") | .bestValue')
# Setting the scores as outputs
echo "::set-output name=reliability::$reliability_score"
echo "::set-output name=best_reliability::$best_reliability_score"
# Print the Reliability rating score/metric
echo "Reliability Score: ${{ steps.fetch-reliability-rating.outputs.reliability }}"
echo "Best/Allowed Reliability Score: ${{ steps.fetch-reliability-rating.outputs.best_reliability_score }}"
env:
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
SONARQUBE_PROJECT_KEY: ${{ secrets.SONARQUBE_PROJECT_KEY }}
SONARQUBE_TOKEN: ${{ secrets.SONARQUBE_TOKEN }}
- name: "ππ Fetch Security Rating Metric"
id: fetch-security-rating
run: |
# Fetching data from SonarQube
response=$(curl --location --location-trusted --max-redirs 10 --silent --fail --show-error --user ${{ secrets.SONARQUBE_TOKEN }}: "${{ secrets.SONAR_HOST_URL }}/api/measures/component?component=OpsVerseIO_hello-world-api-dotnet_fbb1f479-d532-46dd-b86d-c63f33391fe3&metricKeys=security_rating")
# Extracting security ratings
security_score=$(echo $response | jq -r '.component.measures[] | select(.metric=="security_rating") | .value')
best_security_score=$(echo $response | jq -r '.component.measures[] | select(.metric=="security_rating") | .bestValue')
# Setting the scores as outputs
echo "::set-output name=security::$security_score"
echo "::set-output name=best_security::$best_security_score"
# Print the Security rating score/metric
echo "Security Score: ${{ steps.fetch-security-rating.outputs.security }}"
echo "Best/Allowed Security Score: ${{ steps.fetch-security-rating.outputs.best_security_score }}"
env:
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
SONARQUBE_PROJECT_KEY: ${{ secrets.SONARQUBE_PROJECT_KEY }}
SONARQUBE_TOKEN: ${{ secrets.SONARQUBE_TOKEN }}
- name: "Print Security Rating"
run: |
echo "Security Score: ${{ steps.fetch-security-rating.outputs.security }}"
outputs:
sonarcloud_output: ${{ steps.sonarqube-quality-gate-check.outputs.quality-gate-status }}
sonar_quality_gate_project_status: ${{ steps.sonarqube-quality-gate-check.outputs.quality_gate_project_status }}
reliability_score: ${{ steps.fetch-reliability-rating.outputs.reliability }}
best_reliability_score: ${{ steps.fetch-reliability-rating.outputs.best_reliability_score }}
security_score: ${{ steps.fetch-security-rating.outputs.security }}
best_security_score: ${{ steps.fetch-reliability-rating.outputs.best_security_score }}
opa-reliability-gate:
name: "π§ Reliability gate"
runs-on: ubuntu-latest
needs:
- sonarqube
steps:
- name: "β³ Validate Reliability score using OPA"
uses: OpsVerseIO/[email protected]
with:
opaServerUrl: "http://opa-opsverse.int.devopsnow.io"
opaServerAuthToken: ${{ secrets.OPA_SERVER_AUTH_TOKEN }}
# opaServerInput: '{"input": { "status": "${{needs.sonarqube.outputs.reliability_score}}", "metric": "unit-test"}}'
opaServerInput: '{"input": { "metricValue": "${{needs.sonarqube.outputs.reliability_score}}", "metricName": "reliability_rating"}}'
opaServerPackageName: "example/include"
skipTlsValidation: true
opa-security-gate:
name: "π§ Security gate"
runs-on: ubuntu-latest
needs:
- sonarqube
steps:
- name: "β³ Validate Security score using OPA"
uses: OpsVerseIO/[email protected]
with:
opaServerUrl: "http://opa-opsverse.int.devopsnow.io"
opaServerAuthToken: ${{ secrets.OPA_SERVER_AUTH_TOKEN }}
# opaServerInput: '{"input": { "status": "${{needs.sonarqube.outputs.security_score}}", "metric": "unit-test"}}'
opaServerInput: '{"input": { "metricValue": "${{needs.sonarqube.outputs.security_score}}", "metricName": "security_rating"}}'
opaServerPackageName: "example/include"
skipTlsValidation: true
opa:
name: "π§ Unit tests gate"
runs-on: ubuntu-latest
needs:
- sonarqube
steps:
- name: "Print Output"
run: |
echo ${{needs.sonarqube.outputs.sonarcloud_output}}
echo ${{needs.sonarqube.outputs.sonar_quality_gate_project_status}}
- name: "β³ Validate quality using OPA"
uses: OpsVerseIO/[email protected]
with:
opaServerUrl: "http://opa-opsverse.int.devopsnow.io"
opaServerAuthToken: ${{ secrets.OPA_SERVER_AUTH_TOKEN }}
# opaServerInput: '{"input": { "status": "${{needs.sonarqube.outputs.sonarcloud_output}}", "metric": "unit-test"}}'
opaServerInput: '{"input": { "metricValue": "${{needs.sonarqube.outputs.security_score}}", "metricName": "security_rating"}}'
opaServerPackageName: "example/include"
skipTlsValidation: true
opa-code-coverage:
name: "π§ Code coverage gate"
runs-on: ubuntu-latest
needs:
- sonarqube
steps:
- name: "β³ Validate quality using OPA"
uses: OpsVerseIO/[email protected]
with:
opaServerUrl: "http://opa-opsverse.int.devopsnow.io"
opaServerAuthToken: ${{ secrets.OPA_SERVER_AUTH_TOKEN }}
# opaServerInput: '{"input": { "status": "${{needs.sonarcloud.outputs.sonarcloud_output}}", "metric": "code-coverage"}}'
opaServerInput: '{"input": { "metricValue": "${{needs.sonarqube.outputs.security_score}}", "metricName": "security_rating"}}'
opaServerPackageName: "example/include"
skipTlsValidation: true
opa-sonarqube:
name: "π§ Static code analysis gate"
runs-on: ubuntu-latest
needs:
- sonarqube
steps:
- name: "β³ Validate quality using OPA"
uses: OpsVerseIO/[email protected]
with:
opaServerUrl: "http://opa-opsverse.int.devopsnow.io"
opaServerAuthToken: ${{ secrets.OPA_SERVER_AUTH_TOKEN }}
# opaServerInput: '{"input": { "status": "${{needs.sonarcloud.outputs.sonarcloud_output}}", "metric": "static-code-analysis"}}'
opaServerInput: '{"input": { "metricValue": "${{needs.sonarqube.outputs.security_score}}", "metricName": "security_rating"}}'
opaServerPackageName: "example/include"
skipTlsValidation: true
build:
name: "π¦ Build docker image and push to Amazon ECR"
runs-on: ubuntu-latest
env:
APP_NAME: hello-world-api-dotnet
needs:
- sonarqube
- opa-reliability-gate
- opa-security-gate
- opa
- opa-code-coverage
- opa-sonarqube
timeout-minutes: 10
steps:
- name: "π§ Add dynamic envs"
run: |
echo "SHORT_SHA=`echo ${GITHUB_SHA} | cut -c1-8`" >> $GITHUB_ENV
echo "SHA= ${GITHUB_SHA}"
echo "SHORT SHA= ${SHORT_SHA}"
- name: "βοΈ checkout repository"
uses: actions/checkout@v2
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-west-2
- name: "π Login to Amazon ECR π"
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: "π¦π Build, tag, and push docker image to Amazon ECR"
env:
REGISTRY: ${{ steps.login-ecr.outputs.registry }}
REPOSITORY: "hello-world-api-dotnet"
IMAGE_TAG: ${{ env.SHORT_SHA }}
run: |
docker build -t $REGISTRY/$REPOSITORY:$IMAGE_TAG .
docker push $REGISTRY/$REPOSITORY:$IMAGE_TAG
update-image-stage:
name: "π Update STAGE image tag"
runs-on: ubuntu-latest
needs:
- sonarqube
- opa-reliability-gate
- opa-security-gate
- opa
- opa-code-coverage
- opa-sonarqube
- build
timeout-minutes: 10
steps:
- name: "π§ Add dynamic envs"
id: "image-tag"
run: |
echo "SHORT_SHA=`echo ${GITHUB_SHA} | cut -c1-8`" >> $GITHUB_ENV
echo "SHA= ${GITHUB_SHA}"
echo "SHORT SHA= ${SHORT_SHA}"
echo "::set-output name=version::$SHORT_SHA"
- name: Checkout Target Repository
uses: actions/checkout@v3
with:
repository: "OpsVerseIO/deployment-manifests"
path: main
token: ${{ secrets.GH_PAT }}
- name: "π Update Image tag Version in the DEV manifest file"
uses: OpsVerseIO/[email protected]
with:
repository: "OpsVerseIO/deployment-manifests"
valueFile: 'hello-world-api-dotnet/helm/stage/values.yaml'
propertyPath: 'helloworldapidotnet.image.tag'
value: '${{ env.SHORT_SHA }}'
branch: main
createPR: false
message: '[STAGE] Update Image tag of hello-world-api-dotnet'
token: ${{ secrets.GH_PAT }}
workDir: main
masterBranchName: main
targetBranch: main
force: true
updateFile: true
release-stage:
name: "π Deploy to STAGE (ArgoCD Sync)"
runs-on: ubuntu-latest
needs:
- sonarqube
- opa-reliability-gate
- opa-security-gate
- opa
- opa-code-coverage
- opa-sonarqube
- build
- update-image-stage
timeout-minutes: 10
steps:
- name: "π§ Trigger ArgoCD Sync/Deployment in STAGE"
uses: opsverseio/[email protected]
with:
address: ${{ secrets.ARGOCD_SERVER }}
token: ${{ secrets.ARGOCD_TOKEN }}
action: sync
appName: ${{ secrets.ARGOCD_PROD_APP_NAME }}
disableTlsVerification: "true"
- name: "π Deploy to STAGE"
run: |
echo "β³ Deploying the application to STAGE"
echo "πβ
π Successfully synced STAGE ArgoCD and deployed the application to STAGE"
integration-test:
name: "π¨ Run integration test suite"
needs:
- sonarqube
- opa-reliability-gate
- opa-security-gate
- opa
- opa-code-coverage
- opa-sonarqube
- build
- release-stage
runs-on: ubuntu-latest
steps:
- name: "π©π¨ Message from Open Policy Agent (OPA) Server"
run: |
echo "Running integration test suite"
echo "β Connecting to STAGE application at: https://staging-server.opsverse.io"
echo "β
π Integration tests passed"
integration-test-opa:
name: "π§ Integration tests gate"
needs:
- sonarqube
- opa-reliability-gate
- opa-security-gate
- opa
- opa-code-coverage
- opa-sonarqube
- build
- release-stage
- integration-test
runs-on: ubuntu-latest
steps:
- name: "β³ Validate quality using OPA"
uses: OpsVerseIO/[email protected]
with:
opaServerUrl: "https://opa-opsverse.int.devopsnow.io"
opaServerAuthToken: ${{ secrets.OPA_SERVER_AUTH_TOKEN }}
opaServerInput: '{"input": { "status": "${{needs.sonarcloud.outputs.sonarcloud_output}}", "metric": "static-code-analysis"}}'
opaServerPackageName: "example/include"
skipTlsValidation: true
update-image-prod:
name: "π Update prod image tag"
runs-on: ubuntu-latest
needs:
- sonarqube
- opa-reliability-gate
- opa-security-gate
- opa
- opa-code-coverage
- opa-sonarqube
- build
- release-stage
- integration-test
- integration-test-opa
timeout-minutes: 10
steps:
- name: "π§ Add dynamic envs"
id: "image-tag"
run: |
echo "SHORT_SHA=`echo ${GITHUB_SHA} | cut -c1-8`" >> $GITHUB_ENV
echo "SHA= ${GITHUB_SHA}"
echo "SHORT SHA= ${SHORT_SHA}"
echo "::set-output name=version::$SHORT_SHA"
- name: Checkout Target Repository
uses: actions/checkout@v3
with:
repository: "OpsVerseIO/deployment-manifests"
path: main
token: ${{ secrets.GH_PAT }}
- name: "π Update Image tag Version in the PROD manifest file"
uses: OpsVerseIO/[email protected]
with:
repository: "OpsVerseIO/deployment-manifests"
valueFile: 'hello-world-api-dotnet/helm/prod/values.yaml'
propertyPath: 'helloworldapidotnet.image.tag'
value: '${{ env.SHORT_SHA }}'
branch: main
createPR: false
message: '[PROD] Update Image tag of hello-world-api-dotnet'
token: ${{ secrets.GH_PAT }}
workDir: main
masterBranchName: main
targetBranch: main
force: true
updateFile: true
release-prod:
environment:
name: production
name: "π Deploy to PROD (ArgoCD Sync)"
needs:
- sonarqube
- opa-reliability-gate
- opa-security-gate
- opa
- opa-code-coverage
- opa-sonarqube
- build
- release-stage
- integration-test
- integration-test-opa
- update-image-prod
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: "π§ Trigger ArgoCD Sync/Deployment in PROD"
uses: opsverseio/[email protected]
with:
address: ${{ secrets.ARGOCD_SERVER }}
token: ${{ secrets.ARGOCD_TOKEN }}
action: sync
appName: ${{ secrets.ARGOCD_PROD_APP_NAME }}
disableTlsVerification: "true"
- name: "π Deploy to PROD ENV"
run: |
echo "β³ Deploying the application to PROD"
echo "πβ
π Successfully synced PROD ArgoCD and deployed the application to PROD"
cleanup:
name: "β»οΈ Cleanup actions"
needs:
- release-stage
- release-prod
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: "β»οΈ remove build artifacts"
run: |
echo "β»οΈ Cleaning up the build artifacts"
echo "β»οΈβ
Successfully cleaned up the build artifacts"