-
Notifications
You must be signed in to change notification settings - Fork 1
427 lines (394 loc) · 15.8 KB
/
deploy_helm_chart.yml
File metadata and controls
427 lines (394 loc) · 15.8 KB
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
425
426
427
name: Deploy helm chart to EKS cluster
on:
workflow_call:
inputs:
annotate_grafana:
description: 'Annotate Grafana'
type: boolean
required: false
default: false
chart_action:
description: 'Helm action to perform'
type: string
required: false
default: 'install'
chart_name:
description: 'The name of the helm release'
type: string
required: true
chart_namespace:
description: 'Kubernetes namespace where chart will be deployed'
type: string
required: false
default: 'default'
chart_path:
description: 'Path to the Helm chart'
type: string
required: true
chart_repository:
description: 'Helm chart repository'
type: string
required: true
chart_timeout:
description: 'The timeout for the helm chart deployment'
type: string
required: false
default: '300s'
chart_values:
description: 'Comma separated list of value set for helms. Example: "key1=value1,key2=value2"'
type: string
required: false
default: null
chart_values_file:
description: 'Path to the Helm chart values file'
type: string
required: false
chart_values_files_template:
description: 'Path to the template values file used in conjunction with 1Password'
type: string
required: false
chart_version:
description: 'The version of the helm chart'
type: string
required: true
cluster_region:
description: 'AWS region of the EKS cluster'
type: string
required: false
default: 'eu-west-1'
deploy_branch:
description: 'Branch from which helm chart will be installed'
type: string
required: false
default: 'main'
environment:
description: 'Environment to deploy to'
type: string
required: true
iam_role_name:
description: 'Name of the IAM role to assume'
type: string
required: false
default: 'K8sAdmin'
kubectl_version:
description: 'kubectl version'
type: string
required: false
default: 'v1.23.4'
slack_notification_enabled:
description: 'Enable slack notification'
type: boolean
required: false
default: false
slack_channel_id:
description: 'Slack channel ID'
type: string
required: false
default: 'C05Q84211L2'
validate_url:
description: 'The url(s) to validate after deployment. Multiple values example: "https://example.com|http://example.net"'
type: string
required: false
secrets:
aws_account_id:
description: 'AWS account ID'
required: true
cluster_name:
description: 'EKS cluster name'
required: true
chart_secret_values:
description: 'Comma separated list of value set for helms which should not be exposed in runner logs. Example: "key1=value1,key2=value2"'
required: false
grafana_domain:
description: 'Grafana domain'
required: false
grafana_token:
description: 'Grafana service account token'
required: false
op_token:
description: '1Password service account token'
required: false
slack_token:
description: 'Slack token'
required: false
jobs:
diff:
runs-on: ubuntu-latest
if: ${{ !contains(format('{0}/{1}', 'refs/heads', inputs.deploy_branch ), github.ref ) }}
environment: ${{ inputs.environment }}
permissions:
contents: read
id-token: write
pull-requests: write
steps:
- name: Checkout code
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
- name: Set up Helm
uses: azure/setup-helm@dda3372f752e03dde6b3237bc9431cdc2f7a02a2 # v5.0.0
- name: Install Helm Diff plugin
run: |
helm plugin install https://github.com/databus23/helm-diff --verify=false
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@d979d5b3a71173a29b74b5b88418bfda9437d885 # v6.1.1
with:
aws-region: ${{ inputs.cluster_region }}
role-to-assume: arn:aws:iam::${{ secrets.aws_account_id }}:role/${{ inputs.iam_role_name }}
role-session-name: GithubActionsRoleSession
role-duration-seconds: 900
- name: Setup kubectl
uses: azure/setup-kubectl@829323503d1be3d00ca8346e5391ca0b07a9ab0d # v5.1.0
with:
version: ${{ inputs.kubectl_version }}
- name: Configure kubectl
run: |
aws eks update-kubeconfig --region ${{ inputs.cluster_region }} --name ${{ secrets.cluster_name }}
- name: Install 1Password CLI
uses: 1password/install-cli-action@8d006a0d0a4fd505af7f7ce589e7f768385ff5e4 # v3.0.0
with:
version: 2.33.1
- name: Template value file
if: ${{ env.OP_SERVICE_ACCOUNT_TOKEN != '' && inputs.chart_values_files_template != '' }}
env:
OP_SERVICE_ACCOUNT_TOKEN: ${{ secrets.op_token }}
run: |
op inject -i ${{ inputs.chart_values_files_template }} -o templated-values.yml
- name: Set inline chart values
id: prepare_values
run: |
values=""
if [ -n "${{ inputs.chart_values }}" ]; then
values="${{ inputs.chart_values }}"
fi
if [ -n "${{ secrets.chart_secret_values }}" ]; then
values="${values:+${values},}${{ secrets.chart_secret_values }}"
fi
echo "values=${values}" >> $GITHUB_ENV
- name: Create chart values files parameter
if: ${{ inputs.chart_values_file != '' || inputs.chart_values_files_template != '' }}
run: |
values="${{ inputs.chart_values_file }}"
secrets="${{ inputs.chart_values_files_template }}"
value_files=""
if [[ -n "$values" ]]; then
value_files="--values $values"
fi
if [[ -n "$secrets" ]]; then
if [[ -n "$value_files" ]]; then
value_files="$value_files --values templated-values.yml"
else
value_files="--values templated-values.yml"
fi
fi
echo "VALUE_FILES=${value_files}" >> $GITHUB_ENV
- name: Set chart repository name
run: |
chart_path="${{ inputs.chart_path }}"
chart_repo_name="${chart_path%%/*}"
echo "chart_repo_name=${chart_repo_name}" >> $GITHUB_ENV
- name: Add chart's repository
run: |
helm repo add ${{ env.chart_repo_name }} ${{ inputs.chart_repository }}
helm repo update
- name: Run Helm diff
id: helm_diff
run: |
if [ -n "${{ steps.prepare_values.outputs.values }}" ]; then
helm diff upgrade \
${{ inputs.chart_name }} \
${{ inputs.chart_path }} \
--version ${{ inputs.chart_version }} \
${{ env.VALUE_FILES }} \
--set ${{ steps.prepare_values.outputs.values }} \
--namespace ${{ inputs.chart_namespace }} \
--suppress-output-line-regex "(app.kubernetes.io/version|chart):.*" \
--three-way-merge \
--suppress-secrets \
--allow-unreleased \
--dry-run=server \
--output dyff >> diff.result
else
helm diff upgrade \
${{ inputs.chart_name }} \
${{ inputs.chart_path }} \
--version ${{ inputs.chart_version }} \
${{ env.VALUE_FILES }} \
--namespace ${{ inputs.chart_namespace }} \
--suppress-output-line-regex "(app.kubernetes.io/version|chart):.*" \
--three-way-merge \
--suppress-secrets \
--allow-unreleased \
--dry-run=server \
--output dyff > diff.result
fi
- name: Create the diff summary
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
DIFF: ${{ steps.helm_diff.outputs.diff }}
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
let diff = '';
const fs = require('fs');
if (fs.existsSync('diff.result')) {
diff = fs.readFileSync('diff.result', 'utf8');
}
const output = `## Release: \`${{ inputs.chart_name }}\`, environment: \`${{ inputs.environment }}\`
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Workflow: \`${{ github.workflow }}\`*
#### Helm Diff 📖\`${{ steps.helm_diff.outcome }}\`
<details><summary>Show Diff</summary>
\`\`\`\n${diff}\n\`\`\`
</details>
`;
fs.writeFileSync('./summary.md', output);
- name: Add diff summary to pull request
if: github.event_name == 'pull_request'
uses: thollander/actions-comment-pull-request@24bffb9b452ba05a4f3f77933840a6a841d1b32b # v3.0.1
with:
file-path: ./summary.md
comment-tag: '${{ inputs.chart_name }}-helm-diff-${{ inputs.environment }}'
deploy:
runs-on: ubuntu-latest
if: ${{ contains(format('{0}/{1}', 'refs/heads', inputs.deploy_branch ), github.ref ) }}
environment: ${{ inputs.environment }}
permissions:
id-token: write
contents: write
steps:
- name: Checkout
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@d979d5b3a71173a29b74b5b88418bfda9437d885 # v6.1.1
with:
aws-region: ${{ inputs.cluster_region }}
role-to-assume: arn:aws:iam::${{ secrets.aws_account_id }}:role/${{ inputs.iam_role_name }}
role-session-name: GithubActionsRoleSession
role-duration-seconds: 900
- name: Install 1Password CLI
uses: 1password/install-cli-action@8d006a0d0a4fd505af7f7ce589e7f768385ff5e4 # v3.0.0
with:
version: 2.33.1
- name: Template value file
if: ${{ env.OP_SERVICE_ACCOUNT_TOKEN != '' && inputs.chart_values_files_template != '' }}
env:
OP_SERVICE_ACCOUNT_TOKEN: ${{ secrets.op_token }}
run: |
op inject -i ${{ inputs.chart_values_files_template }} -o templated-values.yml
- name: Set chart values
run: |
if [ "${{ secrets.chart_secret_values }}" == "" ]
then
echo "VALUES=${{ inputs.chart_values }}" >> $GITHUB_ENV
else
echo "VALUES=${{ format('{0},{1}', secrets.chart_secret_values, inputs.chart_values) }}" >> $GITHUB_ENV
fi
- name: Create chart value files parameter
if: ${{ inputs.chart_values_file != '' || inputs.chart_values_files_template != '' }}
run: |
values="${{ inputs.chart_values_file }}"
secrets="${{ inputs.chart_values_files_template }}"
value_files=""
if [[ -n "$values" ]]; then
value_files="$values"
fi
if [[ -n "$secrets" ]]; then
if [[ -n "$value_files" ]]; then
value_files="$value_files,templated-values.yml"
else
value_files="templated-values.yml"
fi
fi
echo "VALUE_FILES=${value_files}" >> $GITHUB_ENV
- name: Fetch Last Commit Message
id: last_commit_msg
run: |
git fetch --depth=1 origin main
LAST_COMMIT_MSG=$(git log --format=%B -n 1 FETCH_HEAD | sed 's/`//g' | sed 's/"//g' | tr '\n' ' ')
echo "LAST_COMMIT_MSG=$LAST_COMMIT_MSG" >> $GITHUB_ENV
- name: Send notification
uses: slackapi/slack-github-action@45a88b9581bfab2566dc881e2cd66d334e621e2c #3.0.3
id: send_notification
if: ${{ fromJson( inputs.slack_notification_enabled ) }}
with:
method: 'chat.postMessage'
token: ${{ secrets.slack_token }}
payload: |
channel: ${{ inputs.slack_channel_id }}
blocks:
- type: "header"
text:
type: "plain_text"
text: "${{ inputs.chart_name }} ${{ inputs.environment }} deployment started (In Progress)"
emoji: true
- type: "section"
fields:
- type: "mrkdwn"
text: "*Status:*\n:large_yellow_square: In Progress"
- type: "mrkdwn"
text: "*Last Commit Message:*\n${{ env.LAST_COMMIT_MSG }}"
- type: "mrkdwn"
text: "*Workflow run:*\n<${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|View>"
- name: Deploy Helm
uses: bitovi/github-actions-deploy-eks-helm@29f556cf128eb1b42f834caf13b60a630ee2a20b # v1.2.12
id: deploy
with:
action: ${{ inputs.chart_action }}
aws-region: ${{ inputs.cluster_region }}
cluster-name: ${{ secrets.cluster_name }}
chart-repository: ${{ inputs.chart_repository }}
chart-path: ${{ inputs.chart_path }}
config-files: ${{ env.VALUE_FILES }}
values: ${{ env.VALUES }}
namespace: ${{ inputs.chart_namespace }}
name: ${{ inputs.chart_name }}
version: ${{ inputs.chart_version }}
atomic: true
helm-wait: true
timeout: ${{ inputs.chart_timeout }}
- name: Validate deployment
if: ${{ inputs.validate_url != '' }}
uses: jtalk/url-health-check-action@e7d5ebdc9027fbf494d2d034f3e8fc78f8b7a2b9 # v5
with:
url: ${{ inputs.validate_url }}
follow-redirect: true
max-attempts: 3
retry-delay: 10s
retry-all: false
- name: Annotate Grafana
if: ${{ fromJson( inputs.annotate_grafana ) }}
env:
CHART: ${{ inputs.chart_name }}
GRAFANA_URL: ${{ secrets.grafana_domain }}
GRAFANA_TOKEN: ${{ secrets.grafana_token }}
ENVIRONMENT: ${{ inputs.environment }}
run: |
curl -X POST "${GRAFANA_URL}/api/annotations" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${GRAFANA_TOKEN}" \
-d '{
"time": '$(date +%s%3N)',
"tags": ["deployment", "${{ env.CHART }}", "${{ env.ENVIRONMENT }}"],
"text": "Deploy of ${{ env.CHART }} to ${{ env.ENVIRONMENT }} from GitHub Actions: ${{ env.LAST_COMMIT_MSG }}"
}'
- name: Update notification
if: ${{ always() && fromJson( inputs.slack_notification_enabled ) }}
uses: slackapi/slack-github-action@45a88b9581bfab2566dc881e2cd66d334e621e2c #3.0.3
with:
method: 'chat.update'
token: ${{ secrets.slack_token }}
payload: |
channel: ${{ inputs.slack_channel_id }}
ts: ${{ steps.send_notification.outputs.ts }}
blocks:
- type: "header"
text:
type: "plain_text"
text: "${{ inputs.chart_name }} ${{ inputs.environment }} deployment finished (Completed)"
emoji: true
- type: "section"
fields:
- type: "mrkdwn"
text: "*Status:*\n${{ steps.deploy.outcome == 'success' && ':white_check_mark: Success' || ':x: Failure '}}"
- type: "mrkdwn"
text: "*Last Commit Message:*\n${{ env.LAST_COMMIT_MSG }}"
- type: "mrkdwn"
text: "*Workflow run:*\n<${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|View>"