-
Notifications
You must be signed in to change notification settings - Fork 0
182 lines (160 loc) · 6.77 KB
/
.deployer.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
name: .Helm Deployer
on:
workflow_call:
inputs:
### Required
# Only secrets!
### Typical / recommended
atomic:
description: Atomic deployment? That means fail all or nothing
default: true
required: false
type: string
directory:
description: Chart directory
default: 'charts/app'
required: false
type: string
environment:
description: Environment name; omit for PRs
required: false
type: string
oc_server:
default: https://api.silver.devops.gov.bc.ca:6443
description: 'OpenShift server'
required: false
type: string
params:
description: 'Extra parameters to pass to helm upgrade'
default: ''
required: false
type: string
tag:
description: Specify a tag to deploy; defaults to PR number
required: false
type: string
triggers:
description: Paths used to trigger a deployment; e.g. ('./backend/' './frontend/)
required: false
type: string
db_user:
description: The database user
required: false
type: string
default: 'site'
### Usually a bad idea / not recommended
timeout-minutes:
description: 'Timeout minutes'
default: 20
required: false
type: number
values:
description: 'Values file'
default: 'values.yaml'
required: false
type: string
outputs:
triggered:
description: 'Has a deployment has been triggered?'
value: ${{ jobs.deployer.outputs.triggered }}
jobs:
deployer:
timeout-minutes: ${{ inputs.timeout-minutes }}
name: Helm
environment: ${{ inputs.environment }}
runs-on: ubuntu-24.04
outputs:
triggered: ${{ steps.triggers.outputs.triggered }}
steps:
### Triggers, tag and release
# Check triggers (omitted or matched) for deployment
- uses: bcgov-nr/action-diff-triggers@v0.2.0
id: triggers
with:
triggers: ${{ inputs.triggers }}
# Variables
- if: ${{ steps.triggers.outputs.triggered == 'true' && inputs.tag == '' }}
id: pr
uses: bcgov-nr/action-get-pr@v0.0.1
- if: steps.triggers.outputs.triggered == 'true'
id: vars
run: |
# Vars: tag and release
# Tag defaults to PR number, but can be overridden by inputs.tag
tag=${{ inputs.tag || steps.pr.outputs.pr }}
# Release name includes run numbers to ensure uniqueness
release=${{ github.event.repository.name }}-${{ inputs.environment || steps.pr.outputs.pr || inputs.tag }}
# version, to support helm packaging for non-pr based releases (workflow_dispatch). default to 1.0.0+github run number
version=1.0.0+${{ github.run_number }}
# Summary
echo "tag=${tag}"
echo "release=${release}"
echo "version=${version}"
# Output
echo "tag=${tag}" >> $GITHUB_OUTPUT
echo "release=${release}" >> $GITHUB_OUTPUT
echo "version=${version}" >> $GITHUB_OUTPUT
### Deploy
- name: Install CLI tools from OpenShift Mirror
if: steps.triggers.outputs.triggered == 'true'
uses: redhat-actions/openshift-tools-installer@v1
with:
oc: "4"
# OC login and acquire short lived token
- if: steps.triggers.outputs.triggered == 'true'
shell: bash
run: |
# OC Login
TOKEN_EXPIRE_IN_SECONDS=$((${{ inputs.timeout-minutes }} * 60))
OC_TEMP_TOKEN=$(curl -k -X POST ${{ inputs.oc_server }}/api/v1/namespaces/${{ secrets.oc_namespace }}/serviceaccounts/pipeline/token --header "Authorization: Bearer ${{ secrets.oc_token }}" -d "{\"spec\": {\"expirationSeconds\": $TOKEN_EXPIRE_IN_SECONDS }}" -H 'Content-Type: application/json; charset=utf-8' | jq -r '.status.token' )
oc login --token=$OC_TEMP_TOKEN --server=${{ inputs.oc_server }}
oc project ${{ secrets.oc_namespace }} # Safeguard!
# Only stop pre-existing deployments on PRs (status = pending-upgrade)
- if: steps.triggers.outputs.triggered == 'true' && github.event_name == 'pull_request'
run: |
# Interrupt any previous deployments (PR only)
PREVIOUS=$(helm status ${{ steps.vars.outputs.release }} -o json | jq .info.status || true)
if [[ ${PREVIOUS} =~ pending ]]; then
echo "Rollback triggered"
helm rollback ${{ steps.vars.outputs.release }} || \
helm uninstall ${{ steps.vars.outputs.release }}
fi
# Package Helm chart
- if: steps.triggers.outputs.triggered == 'true'
uses: actions/checkout@v4
- if: steps.triggers.outputs.triggered == 'true'
working-directory: ${{ inputs.directory }}
run: |
# Helm package
helm package -u . --app-version="tag-${{ steps.vars.outputs.tag }}_run-${{ github.run_number }}" --version=${{ steps.pr.outputs.pr || steps.vars.outputs.version }}
# Deploy Helm chart as atomic, with timeout
- if: steps.triggers.outputs.triggered == 'true' && inputs.atomic != 'false'
working-directory: ${{ inputs.directory }}
run: |
# 1 minute less for HELM to do atomic deployment and rollback if not completed within the time.
DEPLOY_TIMEOUT=$((${{ inputs.timeout-minutes }}-1))
# Helm upgrade/rollout - atomic, timeout
helm upgrade \
--set-string global.repository=${{ github.repository }} \
--set-string global.tag=${{ steps.vars.outputs.tag }} \
--set-string global.secrets.keycloakSecret="${{ secrets.KEYCLOAK_SECRET }}" \
--set-string global.secrets.keycloakRealm="${{ secrets.KEYCLOAK_REALM }}" \
--set-string global.secrets.keycloakClientID="${{ secrets.KEYCLOAK_CLIENTID }}" \
--set-string global.secrets.keycloakAuthURL="${{ secrets.KEYCLOAK_AUTH_URL }}" \
--set-string global.config.dbUser=${{ inputs.db_user }} \
${{ inputs.params }} \
--install --wait --atomic ${{ steps.vars.outputs.release }} --debug \
--timeout "$DEPLOY_TIMEOUT"m \
--values ${{ inputs.values }} \
./${{ github.event.repository.name }}-${{ steps.pr.outputs.pr || steps.vars.outputs.version }}.tgz
# Helm release history
- if: steps.triggers.outputs.triggered == 'true'
run: |
# Helm release history
helm history ${{ steps.vars.outputs.release }}
### Cleanup
# Completed pod cleanup
- if: steps.triggers.outputs.triggered == 'true'
run: |
# Completed pod cleanup
oc delete po --field-selector=status.phase==Succeeded || true