generated from bcgov/quickstart-openshift
-
Notifications
You must be signed in to change notification settings - Fork 2
193 lines (174 loc) · 5.83 KB
/
deploy.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
name: Deploy
on:
workflow_call:
inputs:
### Required
release:
description: 'PR number, test or prod.'
required: true
type: string
repository:
description: 'repository name.'
required: false
type: string
default: "onroutebc"
### Typical / recommended
autoscaling:
description: 'Autoscaling enabled or not for the deployments'
required: false
type: boolean
default: true
environment:
description: "Deployment environment - dev/test/prod"
required: true
type: string
default: "dev"
tag:
description: 'Docker tag; e.g. PR number, tag, test or prod'
required: false
type: string
default: ${{ github.event.number }}
triggers:
description: Paths to trigger a deploy; omit=always; e.g. ('backend/' 'frontend/')
required: false
type: string
### Usually a bad idea / not recommended
directory:
description: 'Chart directory.'
default: 'charts/onroutebc'
required: false
type: string
timeout-minutes:
description: 'Timeout minutes'
default: 10
required: false
type: number
values:
description: 'Values file.'
default: 'values.yaml'
required: false
type: string
params:
description: 'Extra parameters to pass to helm upgrade'
default: ''
required: false
type: string
workflow_dispatch:
inputs:
### Required
release:
description: 'release name'
required: true
type: string
default: "demo"
repository:
description: 'repository name.'
required: false
type: string
default: "onroutebc"
### Typical / recommended
autoscaling:
description: 'Autoscaling enabled or not for the deployments'
required: false
type: boolean
default: true
environment:
description: "Deployment environment - dev/test/prod"
required: true
type: choice
options: ["dev","test","prod"]
default: "dev"
tag:
description: 'Docker tag; e.g. PR number, tag, test or prod'
required: false
type: string
default: "latest"
### Usually a bad idea / not recommended
directory:
description: 'Chart directory.'
default: 'charts/onroutebc'
required: false
type: string
values:
description: 'Values file.'
default: 'values.yaml'
required: false
type: string
params:
description: 'Extra parameters to pass to helm upgrade'
default: ''
required: false
type: string
env:
repo_release: ${{ inputs.repository }}-${{ inputs.release }}
package_tag: ${{ inputs.tag }}
jobs:
# https://github.com/bcgov-nr/action-deployer-openshift
deploys:
name: Helm
environment: ${{ inputs.environment }}
runs-on: ubuntu-22.04
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
- name: Check Deployment Triggers
id: triggers
run: |
# Expand for trigger processing
# Always deploy if no triggers are provided
if [ -z "${{ inputs.triggers }}" ]; then
echo "Always deploy when no triggers are provided"
echo "triggered=true" >> $GITHUB_OUTPUT
exit 0
fi
# Deploy if changed files (git diff) match triggers
TRIGGERS=${{ inputs.triggers }}
git fetch origin ${{ github.event.repository.default_branch }}
while read -r check; do
for t in "${TRIGGERS[@]}"; do
if [[ "${check}" =~ "${t}" ]]; then
echo "Build triggered based on git diff"
echo -e "${t}\n --> ${check}"
echo "triggered=true" >> $GITHUB_OUTPUT
exit 0
fi
done
done < <(git diff origin/${{ github.event.repository.default_branch }} --name-only)
# If here skip deployment
echo "No triggers have fired, deployment skipped"
- uses: redhat-actions/openshift-tools-installer@v1
if: ${{ steps.triggers.outputs.triggered == 'true' }}
with:
oc: "4"
- name: Deploy if Triggers Fired
if: ${{ steps.triggers.outputs.triggered == 'true' }}
working-directory: ${{ inputs.directory }}
shell: bash
run: |
oc login --token=${{ secrets.oc_token }} --server=${{ vars.oc_server }}
oc project ${{ vars.OC_NAMESPACE }} # Safeguard!
# Interrupt any previous jobs (status = pending-upgrade)
PREVIOUS=$(helm status ${{ env.repo_release }} -o json | jq .info.status || true)
if [[ ${PREVIOUS} =~ pending ]]; then
echo "Rollback triggered"
helm rollback ${{ env.repo_release }} || \
helm uninstall ${{ env.repo_release }}
fi
# Clean previous image, if any
if [[ ${{inputs.environment}} == 'dev' ]]; then
helm uninstall ${{ env.repo_release }} || true
fi
# Deploy Helm release
helm dependency update
helm upgrade \
--set global.autoscaling=${{ inputs.autoscaling }} \
--set-string global.repository=${{ github.repository }} \
--set-string global.tag=${{ inputs.tag }} \
--install --wait --atomic ${{ env.repo_release }} \
--timeout 10m --force \
--values ${{ inputs.values }} \
${{ inputs.params }} .
# print history
helm history ${{ env.repo_release }}
# Remove old build runs, build pods and deployment pods
oc delete po --field-selector=status.phase==Succeeded