-
Notifications
You must be signed in to change notification settings - Fork 1
269 lines (254 loc) · 11.7 KB
/
build.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
name: 🔨 Build on OpenShift
concurrency:
group: ${{ github.ref }}
cancel-in-progress: true
env:
APP: moodle
USER: ${{ github.actor }}
PROJECT: ${{ vars.OPENSHIFT_DEPLOY_PROJECT }}
APP_HOST_URL: moodle-${{ vars.OPENSHIFT_DEPLOY_PROJECT }}-${{ github.ref_name }}.apps.silver.devops.gov.bc.ca
BASE_IMAGE_TAG: ${{ github.ref_name }}
BUILD_NAMESPACE: ${{ vars.OPENSHIFT_DEPLOY_PROJECT }}-tools
DEPLOY_NAMESPACE: ${{ vars.OPENSHIFT_DEPLOY_PROJECT }}-${{ github.ref_name }}
BUILD_ID: ${{ github.event.number }}
BRANCH: ${{ github.ref_name }}
SOURCE_REPOSITORY_URL: "https://github.com/${{ github.repository }}"
INSPECT_JOB_URL: "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
SKIP_BUILD_ENV: ${{ vars.SKIP_BUILD }}
BUILD_ICON_URL: https://cdn-icons-png.flaticon.com/128/5110/5110294.png
SUCCESS_ICON_URL: https://cdn-icons-png.flaticon.com/128/1356/1356479.png
FAIL_ICON_URL: https://www.flaticon.com/free-icon/rocket_6818114
on:
push:
branches:
- dev
- test
- prod
paths:
- '**.Dockerfile'
- '**.conf'
- '**.ini'
- '**.php'
- '**.yml'
- '**.json'
- 'php-fpm-healthcheck.sh'
pull_request:
branches:
- dev
- test
- prod
paths:
- '**.Dockerfile'
- '**.conf'
- '**.ini'
- '**.php'
- '**.yml'
- 'php-fpm-healthcheck.sh'
jobs:
#Print variables for logging and debugging purposes
checkEnv:
name: 📋 Environment Check
runs-on: ubuntu-latest
steps:
- name: Print Env Vars
run: |
echo Deploying ${{ env.APP }} on ${{ env.PROJECT }}
echo "-----------------------"
echo Deploy to Namespace: ${{ env.DEPLOY_NAMESPACE }}
echo URL: https://${{ env.APP_HOST_URL }}
echo "CLEAN_BUILDS: ${{ env.CLEAN_BUILDS }}"
if [[ "${{ env.CLEAN_BUILDS }}" != "false" ]]; then
echo "CLEAN_BUILDS is not false, setting CLEAN_BUILDS_TEXT to TRUE"
echo CLEAN_BUILDS_TEXT="TRUE" >> $GITHUB_OUTPUT
else
echo "CLEAN_BUILDS is false, setting CLEAN_BUILDS_TEXT to FALSE"
echo CLEAN_BUILDS_TEXT="FALSE" >> $GITHUB_OUTPUT
fi
echo "SKIP_BUILD: $SKIP_BUILD OR ${{ env.SKIP_BUILD_ENV }} OR $SKIP_BUILD_ENV"
if [[ $SKIP_BUILD == "YES" ]]; then
echo "SKIP_BUILD is TRUE, setting SKIP_BUILD_TEXT to TRUE"
echo SKIP_BUILD_TEXT="TRUE" >> $GITHUB_OUTPUT
else
echo "SKIP_BUILD is FALSE, setting SKIP_BUILD_TEXT to FALSE"
echo SKIP_BUILD_TEXT="FALSE" >> $GITHUB_OUTPUT
fi
# Build PHP Image
build-images:
name: 🔨 Build Application Images (${{ github.ref_name }})
needs: [checkEnv]
runs-on: ubuntu-latest
if: ${{ needs.checkEnv.SKIP_BUILD_TEXT == 'FALSE' }} && (github.ref_name == 'dev' || github.ref_name == 'test' || github.ref_name == 'prod')
steps:
# Checkout the PR branch
- name: 📤 Checkout Target Branch
uses: actions/checkout@v2
# Log in to OpenShift.
# Note: The secrets needed to log in are NOT available if the PR comes from a FORK.
# PR's must originate from a branch off the original repo or else all openshift `oc` commands will fail.
- name: 🔑 Log in to OpenShift
run: |
oc login --token=${{ secrets.SA_TOKEN }} --server=https://api.silver.devops.gov.bc.ca:6443
# Get Environment Variables from file
- name: 📋 Setup Environment from File
id: dotenv
uses: falti/dotenv-action@v1
with:
path: example.env
export-variables: true
log-variables: true
keys-case: upper
# Build the PHP image
- name: 🚧 Build PHP (SKIP? ${{ needs.checkEnv.SKIP_BUILD_TEXT }})
id: build-php
if: success() && ${{ needs.checkEnv.SKIP_BUILD_TEXT == 'FALSE' }}
working-directory: "./"
run: |
echo "Building ${{ steps.dotenv.outputs.PHP_DEPLOYMENT_NAME }}:${{ env.BRANCH }}"
echo "Using ${{ steps.dotenv.outputs.PHP_IMAGE }}"
if [ ${{ env.SKIP_BUILD == false }} == true ]; then
echo "STARTING BUILD..."
else
echo "SKIPPING BUILD (Environment variable SKIP_BUILD=true)"
fi
sh ./openshift/scripts/build-docker-image.sh
env:
DOCKER_FROM_IMAGE: ${{ steps.dotenv.outputs.PHP_IMAGE }}
DOCKER_FILE_PATH: ${{ env.PHP_DOCKER_FILE_PATH }}
DEPLOYMENT_NAME: ${{ steps.dotenv.outputs.PHP_DEPLOYMENT_NAME }}
SOURCE_CONTEXT_DIR: ${{ env.PHP_SOURCE_CONTEXT_DIR }}
BRANCH: ${{ env.BRANCH }}
BUILD_NAMESPACE: ${{ env.BUILD_NAMESPACE }}
IMAGE_REPO: ${{ steps.dotenv.outputs.IMAGE_REPO }}
IMAGE_TAG: ${{ env.BASE_IMAGE_TAG }}
SOURCE_REPOSITORY_URL: ${{ env.SOURCE_REPOSITORY_URL }}
CLEAN_BUILDS_TEXT: ${{ needs.checkEnv.outputs.CLEAN_BUILDS_TEXT }}
# Build the Cron image
- name: 🚧 Build Cron (SKIP? ${{ needs.checkEnv.SKIP_BUILD_TEXT }})
id: build-cron
if: success() && ${{ needs.checkEnv.SKIP_BUILD_TEXT == 'FALSE' }}
working-directory: "./"
run: |
echo "Building ${{ steps.dotenv.outputs.CRON_DEPLOYMENT_NAME }}:${{ env.BRANCH }}"
echo "Using ${{ steps.dotenv.outputs.CRON_IMAGE }}"
if [ ${{ env.SKIP_BUILD == false }} == true ]; then
echo "STARTING BUILD..."
else
echo "SKIPPING BUILD (Environment variable SKIP_BUILD=true)"
fi
sh ./openshift/scripts/build-docker-image.sh
env:
DOCKER_FROM_IMAGE: ${{ steps.dotenv.outputs.CRON_IMAGE }}
DOCKER_FILE_PATH: ${{ env.CRON_DOCKER_FILE_PATH }}
DEPLOYMENT_NAME: ${{ steps.dotenv.outputs.CRON_DEPLOYMENT_NAME }}
SOURCE_CONTEXT_DIR: ${{ env.CRON_SOURCE_CONTEXT_DIR }}
BRANCH: ${{ env.BRANCH }}
BUILD_NAMESPACE: ${{ env.BUILD_NAMESPACE }}
IMAGE_REPO: ${{ steps.dotenv.outputs.IMAGE_REPO }}
IMAGE_TAG: ${{ env.BASE_IMAGE_TAG }}
SOURCE_REPOSITORY_URL: ${{ env.SOURCE_REPOSITORY_URL }}
outputs:
APP: ${{ steps.dotenv.outputs.APP }}
DB_NAME: ${{ steps.dotenv.outputs.DB_NAME }}
DB_USER: ${{ steps.dotenv.outputs.DB_USER }}
DB_PASSWORD: ${{ steps.dotenv.outputs.DB_PASSWORD }}
IMAGE_REPO: ${{ steps.dotenv.outputs.IMAGE_REPO }}
WEB_DEPLOYMENT_NAME: ${{ steps.dotenv.outputs.WEB_DEPLOYMENT_NAME }}
DB_DEPLOYMENT_NAME: ${{ steps.dotenv.outputs.DB_DEPLOYMENT_NAME }}
PHP_IMAGE: ${{ steps.dotenv.outputs.PHP_IMAGE }}
PHP_DEPLOYMENT_NAME: ${{ steps.dotenv.outputs.PHP_DEPLOYMENT_NAME }}
CRON_IMAGE: ${{ steps.dotenv.outputs.CRON_IMAGE }}
CRON_DEPLOYMENT_NAME: ${{ steps.dotenv.outputs.CRON_DEPLOYMENT_NAME }}
REDIS_REPO: ${{ steps.dotenv.outputs.REDIS_REPO }}
REDIS_DEPLOYMENT_NAME: ${{ steps.dotenv.outputs.REDIS_DEPLOYMENT_NAME }}
REDIS_IMAGE: ${{ steps.dotenv.outputs.REDIS_IMAGE }}
WEB_IMAGE: ${{ steps.dotenv.outputs.WEB_IMAGE }}
DB_IMAGE: ${{ steps.dotenv.outputs.DB_IMAGE }}
APP_HOST_URL: ${{ env.APP_HOST_URL }}
BUILD_NAMESPACE: ${{ env.BUILD_NAMESPACE }}
DEPLOY_NAMESPACE: ${{ env.DEPLOY_NAMESPACE }}
BRANCH: ${{ env.BRANCH }}
call-cleanup-workflow:
name: 🧹️ Clean Build ( ${{ needs.checkEnv.CLEAN_BUILDS_TEXT }} )
uses: ./.github/workflows/cleanup.yml
needs: [checkEnv, build-images]
if: success() && ${{ needs.checkEnv.CLEAN_BUILDS_TEXT != 'FALSE' }}
secrets: inherit
with:
APP: ${{ needs.build-images.outputs.APP }}
APP_HOST_URL: ${{ needs.build-images.outputs.APP_HOST_URL }}
BRANCH: ${{ needs.build-images.outputs.BRANCH }}
BUILD_NAMESPACE: ${{ needs.build-images.outputs.BUILD_NAMESPACE }}
DEPLOY_NAMESPACE: ${{ needs.build-images.outputs.DEPLOY_NAMESPACE }}
DB_DEPLOYMENT_NAME: ${{ needs.build-images.outputs.DB_DEPLOYMENT_NAME }}
WEB_DEPLOYMENT_NAME: ${{ needs.build-images.outputs.WEB_DEPLOYMENT_NAME }}
PHP_DEPLOYMENT_NAME: ${{ needs.build-images.outputs.PHP_DEPLOYMENT_NAME }}
CRON_DEPLOYMENT_NAME: ${{ needs.build-images.outputs.CRON_DEPLOYMENT_NAME }}
REDIS_DEPLOYMENT_NAME: ${{ needs.build-images.outputs.REDIS_DEPLOYMENT_NAME }}
call-deploy-workflow:
name: 🚀 Deploy to OpenShift
uses: ./.github/workflows/deploy.yml
needs: [checkEnv, build-images, call-cleanup-workflow]
if: needs.build-images.outcome == 'success' || needs.build-images.outcome == 'skipped' || ${{ needs.checckEnv.SKIP_BUILD_TEXT == 'FALSE' }}
secrets: inherit
with:
BASE_IMAGE_TAG: ${{ github.ref_name }}
APP: ${{ needs.build-images.outputs.APP }}
DB_NAME: ${{ needs.build-images.outputs.DB_NAME }}
DB_USER: ${{ needs.build-images.outputs.DB_USER }}
DB_PASSWORD: ${{ needs.build-images.outputs.DB_PASSWORD }}
WEB_DEPLOYMENT_NAME: ${{ needs.build-images.outputs.WEB_DEPLOYMENT_NAME }}
WEB_IMAGE: ${{ needs.build-images.outputs.WEB_IMAGE }}
PHP_DEPLOYMENT_NAME: ${{ needs.build-images.outputs.PHP_DEPLOYMENT_NAME }}
PHP_IMAGE: ${{ needs.build-images.outputs.PHP_IMAGE }}
DB_DEPLOYMENT_NAME: ${{ needs.build-images.outputs.DB_DEPLOYMENT_NAME }}
DB_IMAGE: ${{ needs.build-images.outputs.DB_IMAGE }}
CRON_IMAGE: ${{ needs.build-images.outputs.CRON_IMAGE }}
CRON_DEPLOYMENT_NAME: ${{ needs.build-images.outputs.CRON_DEPLOYMENT_NAME }}
REDIS_REPO: ${{ needs.build-images.outputs.REDIS_REPO }}
REDIS_DEPLOYMENT_NAME: ${{ needs.build-images.outputs.REDIS_DEPLOYMENT_NAME }}
REDIS_IMAGE: ${{ needs.build-images.outputs.REDIS_IMAGE }}
APP_HOST_URL: ${{ needs.build-images.outputs.APP_HOST_URL }}
BUILD_NAMESPACE: ${{ needs.build-images.outputs.BUILD_NAMESPACE }}
DEPLOY_NAMESPACE: ${{ needs.build-images.outputs.DEPLOY_NAMESPACE }}
IMAGE_REPO: ${{ needs.build-images.outputs.IMAGE_REPO }}
BRANCH: ${{ needs.build-images.outputs.BRANCH }}
notify:
name: 📫 Send Notifications (via Rocket.Chat)
needs: [build-images, call-deploy-workflow]
if: success() || failure()
runs-on: ubuntu-latest
steps:
- name: ⚠️ Failure
if: failure()
uses: muinmomin/[email protected]
with:
url: ${{ secrets.ROCKETCHAT_WEBHOOK_URL }}
data: '{
"alias":"FAILED GitHub Action by ${{ env.USER }}",
"text":"Job: ${{ github.job }} in
${{ github.workflow }} of
${{ github.repository }}:${{ github.ref_name }} >>
Deploy1: ${{ needs.call-deploy-workflow.outputs.deploy1 }}
Deploy2: ${{ needs.call-deploy-workflow.outputs.deploy2 }}",
"attachments":[{
"title":"${{ github.job }} ${{ job.status }} to ${{ github.ref_name }}",
"title_link": "${{ env.INSPECT_JOB_URL }}",
"text":"Failure detected in ${{ github.repository }}:${{ github.ref_name }}",
"image_url": "${{ env.FAIL_ICON_URL }}",
"color":"#e7e076"
}]}'
- name: ✅ Success
uses: muinmomin/[email protected]
with:
url: ${{ secrets.ROCKETCHAT_WEBHOOK_URL }}
data: '{
"alias":"Successful ${{ github.job }} by ${{ env.USER }}",
"text":"${{ github.job }} in ${{ github.workflow }}
of ${{ github.repository }}:${{ github.ref_name }}",
"attachments":[{
"title":"${{ github.job }} ${{ job.status }} to ${{ github.ref_name }}",
"title_link": "${{ env.INSPECT_JOB_URL }}",
"text":"Successful deployment of: ${{ github.repository }}:${{ github.ref_name }}",
"image_url": "${{ env.SUCCESS_ICON_URL }}",
"color":"#05a227"
}]}'