generated from bcgov/quickstart-openshift
-
Notifications
You must be signed in to change notification settings - Fork 2
99 lines (89 loc) · 3.52 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
name: Deploy
on:
workflow_call:
inputs:
component:
type: string
required: true
description: Component (e.g. backend, frontend), also used as path
environment:
type: string
required: false
default: ""
description: GitHub environment containing required secrets
overwrite:
type: string
required: true
description: Replace existing objects/artifacts?
repository:
type: string
default: ${{ github.repository }}
required: false
description: Optionally, specify a different repo to clone
template_file:
type: string
required: true
description: Template file (e.g. .github/openshift/deploy.frontend.yml)
template_vars:
type: string
required: true
description: Variables to pass (e.g. -p ZONE=...)
secrets:
oc_namespace:
required: true
description: OpenShift namespace (e.g. abc123-dev)
oc_server:
required: true
description: OpenShift server (e.g. https://api.silver.devops.gov.bc.ca:6443)
oc_token:
required: true
description: OpenShift access token
jobs:
deploy:
name: ${{ inputs.component }}
runs-on: ubuntu-22.04
environment: ${{ inputs.environment }}
steps:
- uses: actions/checkout@v3
with:
repository: ${{ inputs.repository }}
- name: Deploy
run: |
# Login to OpenShift (NOTE: project command is a safeguard)
oc login --token=${{ secrets.oc_token }} --server=${{ secrets.oc_server }}
oc project ${{ secrets.oc_namespace }}
# Process template, consuming variables/parameters
TEMPLATE=$(oc process -f ${{ inputs.template_file }} ${{ inputs.template_vars }} --local)
# ImageStream and DeploymentConfig names
IS_LIST=$(jq -rn "${TEMPLATE} | .items[] | select(.kind==\"ImageStream\") | .metadata.name")
DC=$(jq -rn "${TEMPLATE} | .items[] | select(.kind==\"DeploymentConfig\") | .metadata.name")
# Clean previous images
# Loop through the ImageStream names and delete each one
for IS in $IS_LIST; do
oc delete is/"$IS" || true
done
# Apply (overwrites) or create (does not overwrite) using processed template
if [ "${{ inputs.overwrite }}" == "true" ]; then
oc apply -f - <<< "${TEMPLATE}"
else
# Suppress AlreadyExists errors and expected failure
oc create -f - 2>&1 <<< "${TEMPLATE}" | sed 's/.*: //'
fi
# Follow any active rollouts (see deploymentconfigs)
oc rollout status dc/${DC} -w
- name: Deployment Verification
run: |
# Pick out a url (host+path), if in template
URL=$( \
oc process -f ${{ inputs.template_file }} ${{ inputs.template_vars }} --local \
| jq -r '.items[] | select(.kind=="Route") | .spec | .host + "/" +.path' \
)
# If URL exists and is usable (e.g. at least a hostname), then test it
if [ ! -z "${URL%/*}" ] && [ $(curl -ILso /dev/null -w "%{http_code}" "${URL}") || $(curl -ILso /test/null -w "%{http_code}" "${URL}") -ne 200 ]
then
HTTP_CODE=$(curl -ILso /dev/null -w "%{http_code}" "${URL}")
echo -e "Failure! \n\tStatus = ${HTTP_CODE} \n\tURL: ${URL}\n"
exit 1
fi
echo -e "Deployment successful!"
[ -z "${URL}"] || echo "URL: ${URL}"