-
Notifications
You must be signed in to change notification settings - Fork 2
171 lines (154 loc) · 6.26 KB
/
.tools-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
name: Tools deployment
on:
pull_request:
concurrency:
# PR open and close use the same group, allowing only one at a time
group: tools-${{ github.event.number }}
cancel-in-progress: true
jobs:
builds:
name: Builds
runs-on: ubuntu-24.04
permissions:
packages: write
steps:
- uses: bcgov-nr/[email protected]
name: Build (legacydb)
with:
package: legacydb
tag: ${{ github.event.number }}
tag_fallback: latest
triggers: ('legacydb/')
build_args: |
APP_VERSION=${{ github.event.number }}
pre-tools:
name: Pre Deploy Tools
needs: [builds]
environment: dev
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- name: Install CLI tools from OpenShift Mirror
uses: redhat-actions/openshift-tools-installer@v1
with:
oc: "4.13"
- name: Scale down legacy
continue-on-error: true
run: |
oc login --token=${{ secrets.OC_TOKEN }} --server=${{ secrets.OC_SERVER }}
oc project ${{ secrets.OC_NAMESPACE }} # Safeguard!
oc scale deployment/nr-forest-client-${{ github.event.number }}-legacy --replicas=0 -n ${{ secrets.OC_NAMESPACE }}
undesired_replicas=0
while true; do
available_replicas=$(oc get deployment/nr-forest-client-${{ github.event.number }}-legacy -n ${{ secrets.OC_NAMESPACE }} -o jsonpath='{.status.availableReplicas}')
if [[ "$available_replicas" -ge "$undesired_replicas" ]]; then
echo "DeploymentConfig ${{ secrets.OC_NAMESPACE }}-${{ github.event.number }}-legacy is now available with $available_replicas replicas."
break
fi
echo "Waiting... ($available_replicas pods available)"
sleep 5
done
deploy-oracle:
name: Deploy Oracle Database
needs: [pre-tools, builds]
environment: tools
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- name: Initializing Deployment
uses: bcgov-nr/[email protected]
with:
file: legacydb/openshift.deploy.yml
oc_namespace: ${{ secrets.OC_NAMESPACE }}
oc_server: ${{ secrets.OC_SERVER }}
oc_token: ${{ secrets.OC_TOKEN }}
oc_version: "4.13"
overwrite: false
parameters:
-p ZONE=tools
-p ORACLEDB_USER_W=THE
-p ORACLEDB_PASSWORD_W=${{ secrets.ORACLEDB_PASSWORD_W }}
-p TAG=latest
remove-tools:
name: Remove database and user
needs: [deploy-oracle]
secrets: inherit
uses: ./.github/workflows/.tools-cleanup.yml
deploy-tools:
name: Deploy Tools
needs: [remove-tools, deploy-oracle]
environment: tools
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- name: Install CLI tools from OpenShift Mirror
uses: redhat-actions/openshift-tools-installer@v1
with:
oc: "4.13"
- name: Create the PR database
continue-on-error: true
run: |
oc login --token=${{ secrets.OC_TOKEN }} --server=${{ secrets.OC_SERVER }}
oc project ${{ secrets.OC_NAMESPACE }} # Safeguard!
# This creates a new pluggable database for the PR
for i in {1..5}; do
POD_NAME=$(oc get pods -l app=nr-forest-client-tools -l deployment=nr-forest-client-tools-legacydb -o jsonpath='{.items[0].metadata.name}' 2>/dev/null)
if [ -n "$POD_NAME" ]; then
echo "Pod found: $POD_NAME"
oc exec $POD_NAME -- /opt/oracle/createDatabase PR_${{ github.event.number }}
break
else
echo "Pod not found, retrying in 10 seconds... ($i/5)"
sleep 10
fi
done
if [ -z "$POD_NAME" ]; then
echo "Failed to find the pod after 5 attempts."
fi
- name: Create the PR user
continue-on-error: true
run: |
oc login --token=${{ secrets.OC_TOKEN }} --server=${{ secrets.OC_SERVER }}
oc project ${{ secrets.OC_NAMESPACE }} # Safeguard!
# This creates a new pluggable database for the PR
for i in {1..5}; do
POD_NAME=$(oc get pods -l app=nr-forest-client-tools -l deployment=nr-forest-client-tools-legacydb -o jsonpath='{.items[0].metadata.name}' 2>/dev/null)
if [ -n "$POD_NAME" ]; then
echo "Pod found: $POD_NAME"
oc exec $POD_NAME -- /opt/oracle/createAppUser "THE" "${{ secrets.ORACLEDB_PASSWORD_W }}_${{ github.event.number }}" "PR_${{ github.event.number }}"
break
else
echo "Pod not found, retrying in 10 seconds... ($i/5)"
sleep 10
fi
done
if [ -z "$POD_NAME" ]; then
echo "Failed to find the pod after 5 attempts."
fi
- name: Migrate the PR database
continue-on-error: true
run: |
BRANCH_NAME="${{ github.head_ref }}"
# Escape slashes and other special characters
ESCAPED_BRANCH_NAME=$(echo "$BRANCH_NAME" | sed 's/[\/&]/\\&/g')
oc login --token=${{ secrets.OC_TOKEN }} --server=${{ secrets.OC_SERVER }}
oc project ${{ secrets.OC_NAMESPACE }} # Safeguard!
oc create job --from=cronjob/nr-forest-client-tools-migratedb migrate-pr${{ github.event.number }}-${{ github.run_attempt }}-$(date +%s) --dry-run=client -o yaml | sed "s/value: main/value: ${ESCAPED_BRANCH_NAME}/" | sed "s/value: \"0\"/value: \"${{ github.event.number }}\"/" | oc apply -f -
scale-up-legacy:
name: Scale up legacy
needs: [deploy-tools]
environment: dev
if: always()
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- name: Install CLI tools from OpenShift Mirror
uses: redhat-actions/openshift-tools-installer@v1
with:
oc: "4.13"
- name: Start the Legacy Service
continue-on-error: true
run: |
oc login --token=${{ secrets.OC_TOKEN }} --server=${{ secrets.OC_SERVER }}
oc project ${{ secrets.OC_NAMESPACE }} # Safeguard!
oc scale deployment/nr-forest-client-${{ github.event.number }}-legacy --replicas=1