forked from bcgov/action-deployer-openshift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
255 lines (227 loc) · 9.31 KB
/
action.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
name: OpenShift Deployer
description: OpenShift deployer with route verification or penetration testing
branding:
icon: package
color: blue
inputs:
### Required
file:
description: Template file (e.g. frontend/openshift.deploy.yml)
required: true
oc_namespace:
description: OpenShift namespace (e.g. abc123-dev)
required: true
oc_server:
description: OpenShift server (e.g. https://api.silver.devops.gov.bc.ca:6443)
required: true
oc_token:
description: OpenShift access token
required: true
overwrite:
description: Replace existing objects/artifacts? (true|false)
required: true
### Typical / recommended
name:
description: Name for any penetration test issues or artifacts; e.g. frontend
default: "name_unset"
parameters:
description: Template parameters/variables to pass (e.g. -p ZONE=...)
penetration_test:
description: Run a ZAProxy penetration test against any routes? [true|false]
default: "false"
penetration_test_fail:
description: Allow ZAProxy alerts to fail the workflow? [true|false]
default: "false"
timeout:
description: Timeout for deployment. [default=15m]
default: "15m"
triggers:
description: Omit to always build, otherwise trigger by path; e.g. ('./backend/', './frontend/)
verification_path:
description: Sets the health endpoint to be used during check stage, does not require the '/' at the begining
default: ""
### Usually a bad idea / not recommended
delete_completed:
description: Delete completed pods
default: true
diff_branch:
description: Branch to diff against
default: ${{ github.event.repository.default_branch }}
repository:
description: Optionally, specify a different repo to clone
default: ${{ github.repository }}
penetration_test_create_issue:
description: Create an issue with penetration test results? [true|false]
default: "true"
penetration_test_token:
description: Specify token (GH or PAT), instead of inheriting one from the calling workflow
default: ${{ github.token }}
verification_retry_attempts:
description: Number of times to attempt deployment verification
default: "3"
verification_retry_seconds:
description: Seconds to wait between deployment verification attempts
default: "10"
### Deprecated
penetration_test_artifact:
description: Provide a name to attach ZAProxy scan artifacts to workflows; e.g. frontend, backend
penetration_test_issue:
description: Provide a name to enable ZAProxy issue creation; e.g. frontend, backend
runs:
using: composite
steps:
# Notify about bugs and deprecations
- shell: bash
run: |
# Notify about bugs and deprecations
set -eu
# Bug mitigation - OpenShift hates images with capitals in org/repo names
REPO=${{ inputs.repository }}
if [[ $REPO != ${REPO,,} ]]; then
echo -e "An OpenShift bug prevents capital letters in repo names.\n"
echo -e "Please handle that using the `repository` parameter.\n"
exit 1
fi
# Deprecation notices
if [ ! -z ${{ inputs.penetration_test_artifact }} ]||[ ! -z ${{ inputs.penetration_test_issue }} ]; then
echo -e "Params penetration_test_artifact and penetration_test_issue have been deprecated. \n"
echo -e "Please use param: name instead. Exiting.\n"
exit 1
fi
# Current/calling repo is required for diff/triggers
- uses: actions/checkout@v4
- id: triggers
shell: bash
run: |
# Process triggers
set -eu
# Triggers
TRIGGERS=${{ inputs.triggers }}
if [ -z "${TRIGGERS}" ]; then
echo "Triggers omitted, deployment required"
echo "triggered=true" >> $GITHUB_OUTPUT
exit 0
else
echo "Processing triggers"
git fetch origin "${{ inputs.diff_branch }}"
while read -r check; do
for t in "${TRIGGERS[@]}"; do
if [[ "${check}" =~ "${t}" ]]; then
echo -e "Triggered: ${t}\n --> ${check}"
echo "triggered=true" >> $GITHUB_OUTPUT
exit 0
fi
done
done < <(git diff origin/"${{ inputs.diff_branch }}" --name-only)
fi
echo "Triggers not matched, deployment skipped"
# Process variables and inputs
# Remote/override repo is required if one has been specified (input)
- name: Checkout remote/override repo
if: ${{ github.repository }} != ${{ inputs.repository }}
uses: actions/checkout@v4
with:
repository: ${{ inputs.repository }}
- id: vars
shell: bash
run: |
# Expand for inputs and variables
set -eu
# Process template, consuming variables/parameters
TEMPLATE="$(oc process -f ${{ inputs.file }} ${{ inputs.parameters }} --local)"
# ImageStream, DeploymentConfig and Route Host from template
DC=$(jq -rn "${TEMPLATE} | .items[] | select(.kind==\"DeploymentConfig\").metadata.name //empty")
IS=$(jq -rn "${TEMPLATE} | .items[] | select(.kind==\"ImageStream\").metadata.name //empty")
echo imageStream=${IS} >> $GITHUB_OUTPUT
echo deploymentConfig=${DC} >> $GITHUB_OUTPUT
# Output URL (host + path), but only if ROUTE_HOST is populated
ROUTE_HOST=$(jq -rn "${TEMPLATE} | .items[] | select(.kind==\"Route\").spec.host //empty")
if [ ! -z ${ROUTE_HOST} ]; then
# Path from inputs takes priority over template
ROUTE_PATH=${{ inputs.verification_path }}
[ ! -z ${ROUTE_PATH} ]|| \
ROUTE_PATH=$(jq -rn "${TEMPLATE} | .items[] | select(.kind==\"Route\").spec.path //empty")
# Remove any duplicate slashes and pass to GITHUB_OUTPUT
echo url="${ROUTE_HOST}/${ROUTE_PATH}" | sed 's // / g' >> $GITHUB_OUTPUT
fi
- name: Deploy
if: steps.triggers.outputs.triggered == 'true'
shell: bash
run: |
# Expand for deployment steps
# Allow pipefail, since we could be catching oc create errors
set +o pipefail
# Login to OpenShift (NOTE: project command is a safeguard)
oc login --token=${{ inputs.oc_token }} --server=${{ inputs.oc_server }}
oc project ${{ inputs.oc_namespace }}
# Clean previous image, if any
IFS=" " read -ra IS <<< "${{ steps.vars.outputs.imageStream }}"
for i in "${IS[@]}"
do
[ ! $(oc get is -o name | grep ^imagestream.image.openshift.io/${i}$) ]|| oc delete is/${i}
done
# Apply (overwrites) or create (does not overwrite) using processed template
TEMPLATE="$(oc process -f ${{ inputs.file }} ${{ inputs.parameters }} --local)"
if [ "${{ inputs.overwrite }}" == "true" ]; then
oc apply --timeout=${{ inputs.timeout }} -f - <<< "${TEMPLATE}"
else
# Suppress AlreadyExists errors and expected failure
oc create -f - 2>&1 <<< "${TEMPLATE}" | sed 's/.*: //'
fi
# Follow any active rollouts (see deploymentconfigs)
DC=${{ steps.vars.outputs.deploymentConfig }}
[ -z "${DC}" ]|| oc rollout status dc/${DC} -w
- name: Route Verification
if: steps.vars.outputs.url &&
( steps.triggers.outputs.triggered == 'true' )&&
( inputs.penetration_test != 'true' )
shell: bash
run: |
# Expand for route verification
# Check for URL (route + path)
URL_HOST_PATH=${{ steps.vars.outputs.url }}
if [ -z "${URL_HOST_PATH}" ]; then
echo "No route found. Skipping."
exit 0
fi
echo -e "URL: ${URL_HOST_PATH}"
# Curl and verify
TRIES="${{ inputs.verification_retry_attempts }}"
SECONDS="${{ inputs.verification_retry_seconds }}"
for (( i=0; i<"${TRIES}"; i++ )); do
HTTP_CODE=$(curl -Lso /dev/null -w "%{http_code}" "${URL_HOST_PATH}")
if [ "${HTTP_CODE}" -eq 200 ]; then
echo -e "Route verification successful!\n"
exit 0
fi
echo -e "HTTP_CODE:${HTTP_CODE}, Try: #${i}"
sleep "${SECONDS}"
done
echo -e "\nRoute verification failed"
exit 1
- name: Penetration Test
if: steps.vars.outputs.url &&
( steps.triggers.outputs.triggered == 'true' )&&
( inputs.penetration_test == 'true' )
uses: zaproxy/[email protected]
with:
allow_issue_writing: "${{ inputs.penetration_test_create_issue }}"
artifact_name: "zap_${{ inputs.name }}"
cmd_options: "-a"
fail_action: "${{ inputs.penetration_test_fail }}"
issue_title: "ZAP: ${{ inputs.name }}"
target: https://${{ steps.vars.outputs.url }}
token: "${{ inputs.penetration_test_token }}"
- if: inputs.delete_completed == 'true'
shell: bash
run: |
# Pod Cleanup
# Login to OpenShift (NOTE: project command is a safeguard)
oc login --token=${{ inputs.oc_token }} --server=${{ inputs.oc_server }}
oc project ${{ inputs.oc_namespace }}
# Cleanup completed pods
oc delete po --field-selector=status.phase==Succeeded
# Action repo needs to be present for cleanup/tests
- name: Checkout local repo to make sure action.yml is present
if: ${{ github.repository }} != ${{ inputs.repository }}
uses: actions/checkout@v4