-
Notifications
You must be signed in to change notification settings - Fork 0
222 lines (196 loc) · 8.63 KB
/
.pr-close.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
name: .PR Close
on:
workflow_call:
inputs:
### Typical / recommended
cleanup:
description: 'Type of cleanup; e.g. helm or label'
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
packages:
description: 'Space-separated list of packages to promote'
required: false
type: string
remove_pvc:
required: false
default: data-${{ github.event.repository.name }}-${{ github.event.number }}-bitnami-pg-0
type: string
description: 'Comma separated list of PVCs to remove'
### Usually a bad idea / not recommended
merge_branch:
description: 'Merge target branch'
required: false
type: string
default: ${{ github.event.repository.default_branch }}
organization:
description: 'GitHub organization; e.g. bcgov'
default: ${{ github.repository_owner }}
required: false
type: string
repository:
description: 'GitHub repository, e.g. quickstart-openshift'
default: ${{ github.event.repository.name }}
required: false
type: string
tag_promote:
description: 'Tag to promote to; e.g. latest, test, demo or prod'
default: test
required: false
type: string
target:
description: 'Target; e.g. PR number, test or prod'
default: ${{ github.event.number }}
required: false
type: string
secrets:
oc_namespace:
description: 'OpenShift namespace'
required: false
oc_token:
description: 'OpenShift token'
required: false
jobs:
vars:
if: inputs.packages != ''
runs-on: ubuntu-24.04
outputs:
packages: ${{ steps.packages.outputs.packages }}
steps:
- id: packages
run: |
# Convert packages to JSON array for job matrix
echo "packages=$(jq -cn '$ARGS.positional' --args ${{ inputs.packages }})" >> $GITHUB_OUTPUT
- run: |
# Diagnostic vars
echo "packages=$(jq -cn '$ARGS.positional' --args ${{ inputs.packages }})"
echo "github.event.pull_request.merged=${{ github.event.pull_request.merged }}"
echo "github.event.pull_request.base.ref=${{ github.event.pull_request.base.ref }}"
echo "inputs.merge_branch=${{ inputs.merge_branch }}"
echo "retag check:"
echo " github.event.pull_request.merged == true && github.event.pull_request.base.ref == inputs.merge_branch"
echo " => ${{ github.event.pull_request.merged == true }} && ${{ github.event.pull_request.base.ref == inputs.merge_branch }}"
echo " => ${{ github.event.pull_request.merged == true && github.event.pull_request.base.ref == inputs.merge_branch }}"
retags:
name: Retags
if: ${{ github.event.pull_request.merged == true && github.event.pull_request.base.ref == inputs.merge_branch }}
needs: vars
runs-on: ubuntu-24.04
permissions:
packages: write
strategy:
matrix:
package: ${{ fromJson(needs.vars.outputs.packages) }}
timeout-minutes: 1
steps:
- uses: shrink/actions-docker-registry-tag@v4
with:
registry: ghcr.io
repository: ${{ inputs.organization }}/${{ inputs.repository }}/${{ matrix.package }}
target: ${{ inputs.target }}
tags: ${{ inputs.tag_promote }}
- run: |
# Verify tagging
INSPECT="docker manifest inspect ghcr.io/${{ inputs.organization }}/${{ inputs.repository }}/${{ matrix.package }}"
SOURCE=$(${INSPECT}:${{ inputs.tag_promote }} | jq -r '.manifests[] | select(.platform.architecture=="amd64") | .digest')
TARGET=$(${INSPECT}:${{ inputs.target }} | jq -r '.manifests[] | select(.platform.architecture=="amd64") | .digest')
echo "SOURCE: ${SOURCE}"
echo "TARGET: ${TARGET}"
if [ "${SOURCE}" != "${TARGET}" ]; then
echo "ERROR: Tagging failed!"
echo "RETRY=true" >> $GITHUB_ENV
else
echo "ERROR: Tagging success!"
echo "RETRY=false" >> $GITHUB_ENV
fi
- uses: shrink/actions-docker-registry-tag@v4
if: env.RETRY == 'true'
with:
registry: ghcr.io
repository: ${{ inputs.organization }}/${{ inputs.repository }}/${{ matrix.package }}
target: ${{ inputs.target }}
tags: ${{ inputs.tag_promote }}
- if: env.RETRY == 'true'
run: |
# Verify tagging after retry
INSPECT="docker manifest inspect ghcr.io/${{ inputs.organization }}/${{ inputs.repository }}/${{ matrix.package }}"
SOURCE=$(${INSPECT}:${{ inputs.tag_promote }} | jq -r '.manifests[] | select(.platform.architecture=="amd64") | .digest')
TARGET=$(${INSPECT}:${{ inputs.target }} | jq -r '.manifests[] | select(.platform.architecture=="amd64") | .digest')
if [ "${SOURCE}" != "${TARGET}" ]; then
echo "ERROR: Tagging failed!"
exit 1
fi
# Clean up OpenShift when PR closed, no conditions
cleanup:
name: OpenShift
if: inputs.cleanup == 'helm' || inputs.cleanup == 'label'
environment: ${{ inputs.environment }}
runs-on: ubuntu-24.04
timeout-minutes: 10
steps:
# OC setup
- uses: redhat-actions/openshift-tools-installer@v1
with:
oc: "4"
# OC Login
- run: |
# OC Login
oc login --token=${{ secrets.oc_token }} --server=${{ inputs.oc_server }}
oc project ${{ secrets.oc_namespace }} # Safeguard!
# Cleanup for Helm deployments
- name: Helm
if: inputs.cleanup == 'helm'
env:
release: ${{ inputs.repository }}-${{ inputs.target }}
run: |
# Helm cleanup steps
# If found, then remove
helm status ${{ env.release }} && helm uninstall --no-hooks ${{ env.release }} || \
echo "Not found: ${{ env.release }}"
# Cleanup for OpenShift template deployments, uses labels
- name: Label
if: inputs.cleanup == 'label'
run: |
# OC Template cleanup steps
# Remove old build runs, build pods and deployment pods
oc delete all,cm,pvc,secret -l app=${{ inputs.repository }}-${{ inputs.target }}
# Remove PVCs from OpenShift when PR closed
- name: Remove PVCs
if: inputs.remove_pvc
run: |
# Remove PVCs
echo "${{ inputs.remove_pvc }}" | while IFS= read -r pvc; do
oc delete pvc ${pvc//,/} || echo "Not found: ${pvc//,/}"
done
- name: Remove PR user and database from crunchy.
shell: bash
run: |
# check if postgres-crunchy exists or else exit
oc get PostgresCluster/postgres-crunchy || exit 0
# Remove the user from the crunchy cluster yaml and apply the changes
USER_TO_REMOVE='{"databases":["site-${{ github.event.number }}"],"name":"site-${{ github.event.number }}"}'
echo 'getting current users from crunchy'
CURRENT_USERS=$(oc get PostgresCluster/postgres-crunchy -o json | jq '.spec.users')
echo "${CURRENT_USERS}"
# Remove the user from the list,
UPDATED_USERS=$(echo "$CURRENT_USERS" | jq --argjson user "$USER_TO_REMOVE" 'map(select(. != $user))')
PATCH_JSON=$(jq -n --argjson users "$UPDATED_USERS" '{"spec": {"users": $users}}')
oc patch PostgresCluster/postgres-crunchy --type=merge -p "$PATCH_JSON"
# get primary crunchy pod and remove the role and db
CRUNCHY_PG_PRIMARY_POD_NAME=$(oc get pods -l postgres-operator.crunchydata.com/role=master -o json | jq -r '.items[0].metadata.name')
echo "${CRUNCHY_PG_PRIMARY_POD_NAME}"
# Terminate all connections to the database before trying terminate
oc exec "${CRUNCHY_PG_PRIMARY_POD_NAME}" -- psql -c "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'site-${{ github.event.number }}' AND pid <> pg_backend_pid();"
# Drop the database and role
oc exec "${CRUNCHY_PG_PRIMARY_POD_NAME}" -- psql -c "DROP DATABASE \"site-${{ github.event.number }}\" --cascade"
oc exec "${CRUNCHY_PG_PRIMARY_POD_NAME}" -- psql -c "DROP ROLE \"site-${{ github.event.number }}\" --cascade"
echo "Database and Role for PR is cleaned."
exit 0