-
Notifications
You must be signed in to change notification settings - Fork 1
294 lines (247 loc) · 10.4 KB
/
Copy pathpush.yml
File metadata and controls
294 lines (247 loc) · 10.4 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
name: Push
on:
push:
branches:
- "main"
env:
AZURE_CONTAINER_REGISTRY: ${{ secrets.AZURE_CONTAINER_REGISTRY }}
RESOURCE_GROUP: ${{ secrets.RESOURCE_GROUP }}
STAGING_CLUSTER_NAME: ${{ secrets.STAGING_CLUSTER_NAME }}
PRODUCTION_CLUSTER_NAME: ${{ secrets.PRODUCTION_CLUSTER_NAME }}
IMAGE_PULL_SECRET_NAME: ${{ secrets.IMAGE_PULL_SECRET_NAME }}
STAGING_DEPLOYMENT_MANIFEST_PATH: "deployment/deployment-staging.yml"
PRODUCTION_DEPLOYMENT_MANIFEST_PATH: "deployment/deployment-canary.yml"
jobs:
run-linters:
name: Run linters
runs-on: ubuntu-latest
steps:
- name: Check out Git repository
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v1
with:
python-version: 3.8
- name: Install Python dependencies
run: pip install black
- name: Run linters
uses: wearerequired/lint-action@v2
with:
black: true
run-tests:
name: Run tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.8"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Test with pytest
run: |
pip install pytest
pip install pytest-cov
pip install mock
pip install pytest-httpserver
python -m pytest --cov-config=.coveragerc --cov=. --exitfirst --verbose --failed-first --cov-fail-under=70
build:
runs-on: ubuntu-latest
if: "! contains(toJSON(github.event.commits.*.message), '[skip-ci]')"
needs: [run-tests, run-linters]
outputs:
imageTag: ${{ steps.generate_tag.outputs.new_tag }}
imageRepository: ${{ steps.image_repository_op.outputs.image_repository }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Generate Tag
id: generate_tag
uses: anothrNick/github-tag-action@1.52.0
env:
GITHUB_TOKEN: ${{ secrets.ECM3440_GITHUB_TOKEN }}
WITH_V: true
DEFAULT_BUMP: patch
DRY_RUN: true
- name: Login to Docker Hub
uses: azure/docker-login@v1
with:
login-server: ${{ secrets.REGISTRY_LOGIN_SERVER }}
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}
- name: Generate image repository path
id: image_repository_op
run: |
echo IMAGE_REPOSITORY=$(echo ${{ secrets.REGISTRY_LOGIN_SERVER }}/${{ github.repository }} | tr '[:upper:]' '[:lower:]') >> $GITHUB_ENV
echo "image_repository=$IMAGE_REPOSITORY" >> $GITHUB_OUTPUT
- name: Build and push image
run: |
docker build . -t ${{ env.IMAGE_REPOSITORY }}:${{ steps.generate_tag.outputs.new_tag }} \
--build-arg SERVICEBUS_CONNECTION_STR="${{ secrets.SERVICEBUS_CONNECTION_STR }}" \
--build-arg SERVICEBUS_TOPIC_NAME="${{ secrets.SERVICEBUS_TOPIC_NAME }}" \
--build-arg SERVICEBUS_SUBSCRIPTION_NAME="${{ secrets.SERVICEBUS_SUBSCRIPTION_NAME }}" \
--label org.opencontainers.image.source=${{ github.event.repository.clone_url }} \
--label org.opencontainers.image.revision=${{ github.sha }}
docker push ${{ env.IMAGE_REPOSITORY }}:${{ steps.generate_tag.outputs.new_tag }}
- name: Comment on PR
uses: mshick/add-pr-comment@v1
env:
GITHUB_TOKEN: ${{ secrets.ECM3440_GITHUB_TOKEN }}
with:
message: "@${{ github.actor }} Image is available for testing. `docker pull ${{ env.IMAGE_REPOSITORY }}:${{ steps.generate_tag.outputs.new_tag }}`"
allow-repeats: false
- name: Push Latest Tag
uses: anothrNick/github-tag-action@1.52.0
env:
GITHUB_TOKEN: ${{ secrets.ECM3440_GITHUB_TOKEN }}
WITH_V: true
DEFAULT_BUMP: patch
- name: Notify Failure
if: failure()
uses: mshick/add-pr-comment@v1
env:
GITHUB_TOKEN: ${{ secrets.ECM3440_GITHUB_TOKEN }}
with:
message: "@${{ github.actor }} Yikes! You better fix it before anyone else finds out! [Build](https://github.com/${{ github.repository }}/commit/${{ github.event.pull_request.head.sha }}/checks) has Failed!"
allow-repeats: false
create-secret:
runs-on: ubuntu-latest
if: "! contains(toJSON(github.event.commits.*.message), '[pre-release]')"
needs: [run-tests, run-linters]
permissions:
actions: read
contents: read
id-token: write
steps:
# Logs in with your Azure credentials
- name: Azure login
uses: azure/login@v1.4.3
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
# Retrieves your Azure Kubernetes Service cluster's kubeconfig file
- name: Get K8s context
uses: azure/aks-set-context@v2.0
with:
resource-group: ${{ env.RESOURCE_GROUP }}
cluster-name: ${{ env.STAGING_CLUSTER_NAME }}
# Retrieves the credentials for pulling images from your Azure Container Registry
- name: Get ACR credentials
id: get-acr-creds
run: |
az acr update -n ${{ env.AZURE_CONTAINER_REGISTRY }} -g ${{ env.RESOURCE_GROUP }} --admin-enabled true
ACR_USERNAME=$(az acr credential show -g ${{ env.RESOURCE_GROUP }} -n ${{ env.AZURE_CONTAINER_REGISTRY }} --query username -o tsv)
ACR_PASSWORD=$(az acr credential show -g ${{ env.RESOURCE_GROUP }} -n ${{ env.AZURE_CONTAINER_REGISTRY }} --query passwords[0].value -o tsv)
echo "::add-mask::${ACR_USERNAME}"
echo "::set-output name=username::${ACR_USERNAME}"
echo "::add-mask::${ACR_PASSWORD}"
echo "::set-output name=password::${ACR_PASSWORD}"
# Creates a kubernetes secret on your Azure Kubernetes Service cluster that matches up to the credentials from the last step
- name: Create K8s secret for pulling image from ACR
uses: Azure/k8s-create-secret@v1.1
with:
container-registry-url: ${{ env.AZURE_CONTAINER_REGISTRY }}.azurecr.io
container-registry-username: ${{ steps.get-acr-creds.outputs.username }}
container-registry-password: ${{ steps.get-acr-creds.outputs.password }}
secret-name: ${{ env.IMAGE_PULL_SECRET_NAME }}
deploy-staging:
runs-on: ubuntu-latest
if: "! contains(toJSON(github.event.commits.*.message), '[pre-release]')"
outputs:
imageTag: ${{ steps.generate_tag.outputs.image_tag }}
imageRepository: ${{ steps.generate_image_repository.image_repository }}
permissions:
actions: read
contents: read
id-token: write
needs: [build, create-secret]
steps:
# Checks out the repository this file is in
- name: Checkout
uses: actions/checkout@v3
# Logs in with your Azure credentials
- name: Azure login
uses: azure/login@v1.4.3
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
# Retrieves your Azure Kubernetes Service cluster's kubeconfig file
- name: Get K8s context
uses: azure/aks-set-context@v2.0
with:
resource-group: ${{ env.RESOURCE_GROUP }}
cluster-name: ${{ env.STAGING_CLUSTER_NAME }}
- name: Inject environment variables to deployment
uses: swdotcom/update-and-apply-kubernetes-configs@v1
with:
k8-config-file-paths: ${{ env.STAGING_DEPLOYMENT_MANIFEST_PATH }}
replacement-method: defined
env:
SERVICE_NAME: ${{ github.event.repository.name }}
IMAGE_REPOSITORY: ${{ needs.build.outputs.imageRepository }}
IMAGE_TAG: ${{ needs.build.outputs.imageTag }}
# Deploys application based on given manifest file
- name: Deploys application
uses: Azure/k8s-deploy@v4
with:
manifests: ${{ env.STAGING_DEPLOYMENT_MANIFEST_PATH }}
images: |
${{ env.AZURE_CONTAINER_REGISTRY }}.azurecr.io/ecm3440/event-processor:${{ needs.build.outputs.imageTag }}
imagepullsecrets: |
${{ env.IMAGE_PULL_SECRET_NAME }}
- name: Set image tag output
id: generate_tag
run: |
echo "image_tag=${{ needs.build.outputs.imageTag }}" >> $GITHUB_OUTPUT
- name: Set image repository output
id: generate_image_repository
run: |
echo "image_repository=${{ needs.build.outputs.imageRepository }}" >> $GITHUB_OUTPUT
deploy-production:
runs-on: ubuntu-latest
if: "! contains(toJSON(github.event.commits.*.message), '[pre-release]')"
permissions:
actions: read
contents: read
id-token: write
needs: [deploy-staging]
steps:
- uses: trstringer/manual-approval@v1
with:
secret: ${{ secrets.ECM3440_GITHUB_TOKEN }}
approvers: charlie2clarke,Ben-Newman-Exeter
minimum-approvals: 1
issue-title: "Deploying ${{ needs.deploy-staging.outputs.imageTag }} to prod from staging"
# Checks out the repository this file is in
- name: Checkout
uses: actions/checkout@v3
# Logs in with your Azure credentials
- name: Azure login
uses: azure/login@v1.4.3
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
# Retrieves your Azure Kubernetes Service cluster's kubeconfig file
- name: Get K8s context
uses: azure/aks-set-context@v2.0
with:
resource-group: ${{ env.RESOURCE_GROUP }}
cluster-name: ${{ env.PRODUCTION_CLUSTER_NAME }}
- name: Inject environment variables to deployment
uses: swdotcom/update-and-apply-kubernetes-configs@v1
with:
k8-config-file-paths: ${{ env.PRODUCTION_DEPLOYMENT_MANIFEST_PATH }}
replacement-method: defined
env:
SERVICE_NAME: ${{ github.event.repository.name }}
IMAGE_REPOSITORY: ${{ needs.deploy-staging.outputs.imageRepository }}
IMAGE_TAG: ${{ needs.deploy-staging.outputs.imageTag }}
# Deploys application based on given manifest file
- name: Deploys application
uses: Azure/k8s-deploy@v4
with:
manifests: ${{ env.PRODUCTION_DEPLOYMENT_MANIFEST_PATH }}
images: |
${{ env.AZURE_CONTAINER_REGISTRY }}.azurecr.io/ecm3440/event-processor:${{ needs.deploy-staging.outputs.imageTag }}
imagepullsecrets: |
${{ env.IMAGE_PULL_SECRET_NAME }}