-
Notifications
You must be signed in to change notification settings - Fork 2
164 lines (134 loc) · 7.3 KB
/
Copy pathcontainer-app.yaml
File metadata and controls
164 lines (134 loc) · 7.3 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
name: ContainerApp
on:
push:
branches: [main]
paths:
- "Application/MicroService/**"
- ".github/workflows/container-app.yaml"
pull_request:
paths:
- "Application/MicroService/**"
- ".github/workflows/container-app.yaml"
branches:
- main
workflow_dispatch:
jobs:
package-app:
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
- uses: actions/checkout@v3
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to Github Packages
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push
id: docker_build
uses: docker/build-push-action@v3
with:
context: ./Application/MicroService/
push: true
tags: |
"ghcr.io/${{ github.actor }}/sample-microservice:latest"
"ghcr.io/${{ github.actor }}/sample-microservice:${{github.run_number}}"
deploy-to-test:
needs: package-app
runs-on: ubuntu-latest
environment: test
concurrency: containerapp-test
steps:
- uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: "Fetch ContainerApp Metadata"
id: "fetchContainerAppMetadata"
run: |
echo "containerAppName=$(az deployment sub show -n "${{ vars.RESOURCE_PREFIX }}-test-apps" --query "properties.outputs.containerAppName.value" -o tsv)" >> $GITHUB_OUTPUT
echo "containerAppResourceGroup=$(az deployment sub show -n "${{ vars.RESOURCE_PREFIX }}-test-apps" --query "properties.outputs.containerAppResourceGroup.value" -o tsv)" >> $GITHUB_OUTPUT
- run: az config set extension.use_dynamic_install=yes_without_prompt
- name: "Deploy new revision to test"
# Uses the az cli to update the latest revision of the container-app. This will direct all traffic to the new revision
run: |
OLD_REVISION_NAME=$(az containerapp show --name '${{ steps.fetchContainerAppMetadata.outputs.containerAppName }}' --resource-group '${{ vars.RESOURCE_PREFIX }}-test-rg' --query 'properties.latestRevisionName' -o tsv | tr -d '\r')
echo "Current revision: $OLD_REVISION_NAME"
az containerapp update \
--name '${{ steps.fetchContainerAppMetadata.outputs.containerAppName }}' \
--resource-group '${{ steps.fetchContainerAppMetadata.outputs.containerAppResourceGroup }}' \
--image "ghcr.io/${{ github.actor }}/sample-microservice:${{github.run_number}}"
az containerapp ingress traffic set \
--name '${{ steps.fetchContainerAppMetadata.outputs.containerAppName }}' \
--resource-group '${{ steps.fetchContainerAppMetadata.outputs.containerAppResourceGroup }}' \
--revision-weight latest=100
az containerapp revision deactivate \
--revision "$OLD_REVISION_NAME" \
--resource-group '${{ steps.fetchContainerAppMetadata.outputs.containerAppResourceGroup }}'
deploy-to-production-green:
needs: deploy-to-test
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
environment: prod
concurrency: containerapp-prod
steps:
- uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: "Fetch ContainerApp Metadata"
id: "fetchContainerAppMetadata"
run: |
echo "containerAppName=$(az deployment sub show -n "${{ vars.RESOURCE_PREFIX }}-prod-apps" --query "properties.outputs.containerAppName.value" -o tsv)" >> $GITHUB_OUTPUT
echo "containerAppResourceGroup=$(az deployment sub show -n "${{ vars.RESOURCE_PREFIX }}-prod-apps" --query "properties.outputs.containerAppResourceGroup.value" -o tsv)" >> $GITHUB_OUTPUT
- run: az config set extension.use_dynamic_install=yes_without_prompt
- name: "Deploy new revision to production green slot"
id: "deploy-new-revision"
# Uses the az cli to update the latest revision of the container-app.
# We need the name of the current revision so we can use it for the blue/green swap after happy testing
run: |
OLD_REVISION_NAME=$(az containerapp show --name '${{ steps.fetchContainerAppMetadata.outputs.containerAppName }}' --resource-group '${{ vars.RESOURCE_PREFIX }}-prod-rg' --query 'properties.latestRevisionName' -o tsv | tr -d '\r')
#Explicitly set old revision traffic to 100% (instead of referring to it as latest)
#This will stop the new revision getting any traffic
az containerapp ingress traffic set \
--name '${{ steps.fetchContainerAppMetadata.outputs.containerAppName }}' \
--resource-group '${{ steps.fetchContainerAppMetadata.outputs.containerAppResourceGroup }}' \
--revision-weight $OLD_REVISION_NAME=100
az containerapp update \
--name '${{ steps.fetchContainerAppMetadata.outputs.containerAppName }}' \
--resource-group '${{ steps.fetchContainerAppMetadata.outputs.containerAppResourceGroup }}' \
--image "ghcr.io/${{ github.actor }}/sample-microservice:${{github.run_number}}"
NEW_REVISION_FQDN=$(az containerapp show --name '${{ steps.fetchContainerAppMetadata.outputs.containerAppName }}' --resource-group '${{ vars.RESOURCE_PREFIX }}-prod-rg' --query 'properties.latestRevisionFqdn' -o tsv | tr -d '\r')
echo "oldRevisionName=${OLD_REVISION_NAME}" >> $GITHUB_OUTPUT
echo "New revision can be tested at https://${NEW_REVISION_FQDN}"
outputs:
oldRevisionName: ${{ steps.deploy-new-revision.outputs.oldRevisionName }}
blue-green-swap:
needs:
- deploy-to-production-green
runs-on: ubuntu-latest
concurrency: containerapp-prod
if: github.ref == 'refs/heads/main'
environment: prod
steps:
- uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- run: az config set extension.use_dynamic_install=yes_without_prompt
- name: "Fetch ContainerApp Metadata"
id: "fetchContainerAppMetadata"
run: |
echo "containerAppName=$(az deployment sub show -n "${{ vars.RESOURCE_PREFIX }}-prod-apps" --query "properties.outputs.containerAppName.value" -o tsv)" >> $GITHUB_OUTPUT
echo "containerAppResourceGroup=$(az deployment sub show -n "${{ vars.RESOURCE_PREFIX }}-prod-apps" --query "properties.outputs.containerAppResourceGroup.value" -o tsv)" >> $GITHUB_OUTPUT
- name: "Activate new revision"
# Change the weights so the new revision takes all the traffic and the previous none (a class blue/green switch)
run: |
az containerapp ingress traffic set \
--name '${{ steps.fetchContainerAppMetadata.outputs.containerAppName }}' \
--resource-group '${{ steps.fetchContainerAppMetadata.outputs.containerAppResourceGroup }}' \
--revision-weight latest=100
az containerapp revision deactivate \
--revision '${{ needs.deploy-to-production-green.outputs.oldRevisionName }}' \
--resource-group '${{ steps.fetchContainerAppMetadata.outputs.containerAppResourceGroup }}'