-
Notifications
You must be signed in to change notification settings - Fork 2
235 lines (194 loc) · 7.62 KB
/
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
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
name: Deploy Workflow
on:
workflow_call:
env:
PREFIX: "ct-public"
SHA: ${{ github.event.pull_request.head.sha || github.sha }}
KUBE_NAMESPACE: ${{ secrets.KUBE_NAMESPACE }}
KUBE_CERT: ${{ secrets.KUBE_CERT }}
KUBE_TOKEN: ${{ secrets.KUBE_TOKEN }}
KUBE_CLUSTER: ${{ secrets.KUBE_CLUSTER }}
concurrency:
group: deploy-${{ github.ref }}
cancel-in-progress: true
jobs:
build:
runs-on: ubuntu-latest
outputs:
build_tag: ${{ steps.vars.outputs.build_tag }}
permissions:
id-token: write # This is required for requesting the JWT
contents: read # This is required for actions/checkout
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Assume role in Cloud Platform
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.ECR_ROLE_TO_ASSUME }}
aws-region: ${{ vars.ECR_REGION }}
- name: Login to container repository
uses: aws-actions/amazon-ecr-login@v2
id: login-ecr
- name: Store current date
run: echo "BUILD_DATE=$(date +%Y-%m-%dT%H:%M:%S%z)" >> $GITHUB_ENV
- name: Store build tag
id: vars
run: |
branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}
short_sha=$(git rev-parse --short $SHA)
build_tag=$PREFIX-$branch-$short_sha
echo "build_tag=$build_tag" >> $GITHUB_OUTPUT
- name: Build
run: |
docker build \
--build-arg APP_BUILD_DATE=${{ env.BUILD_DATE }} \
--build-arg APP_BUILD_TAG=${{ steps.vars.outputs.build_tag }} \
--build-arg APP_GIT_COMMIT=$SHA \
-t ${{ vars.ECR_URL }}:$SHA .
- name: Push to ECR
run: docker push ${{ vars.ECR_URL }}:$SHA
deploy-development:
runs-on: ubuntu-latest
needs: build
environment: development
permissions:
id-token: write # This is required for requesting the JWT
contents: read # This is required for actions/checkout
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Assume role in Cloud Platform
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.ECR_ROLE_TO_ASSUME }}
aws-region: ${{ vars.ECR_REGION }}
- name: Login to container repository
uses: aws-actions/amazon-ecr-login@v2
id: login-ec
- name: Tag build and push to ECR
run: |
docker pull ${{ vars.ECR_URL }}:$SHA
docker tag ${{ vars.ECR_URL }}:$SHA ${{ vars.ECR_URL }}:development.latest
docker push ${{ vars.ECR_URL }}:development.latest
- name: Authenticate to the cluster
run: |
echo "${KUBE_CERT}" > ca.crt
kubectl config set-cluster ${KUBE_CLUSTER} --certificate-authority=./ca.crt --server=https://${KUBE_CLUSTER}
kubectl config set-credentials deploy-user --token=${KUBE_TOKEN}
kubectl config set-context ${KUBE_CLUSTER} --cluster=${KUBE_CLUSTER} --user=deploy-user --namespace=${KUBE_NAMESPACE}
kubectl config use-context ${KUBE_CLUSTER}
- name: Rollout restart deployment
run: |
kubectl set image -n ${KUBE_NAMESPACE} \
deployment/contact-moj \
webapp="${{ vars.ECR_URL }}:$SHA" \
jobs="${{ vars.ECR_URL }}:$SHA" \
metrics="${{ vars.ECR_URL }}:$SHA"
notify-development:
needs: [build, deploy-development]
uses: ./.github/workflows/notification.yml
secrets:
webhook_url: ${{ secrets.SLACK_WEBHOOK_URL }}
with:
build_tag: ${{ needs.build.outputs.build_tag }}
environment: Staging
deploy-staging:
runs-on: ubuntu-latest
needs: build
environment: staging
permissions:
id-token: write # This is required for requesting the JWT
contents: read # This is required for actions/checkout
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Assume role in Cloud Platform
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.ECR_ROLE_TO_ASSUME }}
aws-region: ${{ vars.ECR_REGION }}
- name: Login to container repository
uses: aws-actions/amazon-ecr-login@v2
id: login-ec
- name: Tag build and push to ECR
run: |
docker pull ${{ vars.ECR_URL }}:$SHA
docker tag ${{ vars.ECR_URL }}:$SHA ${{ vars.ECR_URL }}:staging.latest
docker push ${{ vars.ECR_URL }}:staging.latest
- name: Authenticate to the cluster
run: |
echo "${KUBE_CERT}" > ca.crt
kubectl config set-cluster ${KUBE_CLUSTER} --certificate-authority=./ca.crt --server=https://${KUBE_CLUSTER}
kubectl config set-credentials deploy-user --token=${KUBE_TOKEN}
kubectl config set-context ${KUBE_CLUSTER} --cluster=${KUBE_CLUSTER} --user=deploy-user --namespace=${KUBE_NAMESPACE}
kubectl config use-context ${KUBE_CLUSTER}
- name: Rollout restart deployment
run: |
kubectl set image -n ${KUBE_NAMESPACE} \
deployment/contact-moj \
webapp="${{ vars.ECR_URL }}:$SHA" \
jobs="${{ vars.ECR_URL }}:$SHA" \
metrics="${{ vars.ECR_URL }}:$SHA"
notify-staging:
needs: [build, deploy-staging]
uses: ./.github/workflows/notification.yml
secrets:
webhook_url: ${{ secrets.SLACK_WEBHOOK_URL }}
with:
build_tag: ${{ needs.build.outputs.build_tag }}
environment: Staging
deploy-production:
runs-on: ubuntu-latest
needs: deploy-staging
if: ${{ github.ref == 'refs/heads/main' }}
environment: production
permissions:
id-token: write # This is required for requesting the JWT
contents: read # This is required for actions/checkout
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Assume role in Cloud Platform
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.ECR_ROLE_TO_ASSUME }}
aws-region: ${{ vars.ECR_REGION }}
- name: Login to container repository
uses: aws-actions/amazon-ecr-login@v2
id: login-ec
- name: Tag build and push to ECR
run: |
docker pull ${{ vars.ECR_URL }}:$SHA
docker tag ${{ vars.ECR_URL }}:$SHA ${{ vars.ECR_URL }}:production.latest
docker push ${{ vars.ECR_URL }}:production.latest
- name: Authenticate to the cluster
run: |
echo "${KUBE_CERT}" > ca.crt
kubectl config set-cluster ${KUBE_CLUSTER} --certificate-authority=./ca.crt --server=https://${KUBE_CLUSTER}
kubectl config set-credentials deploy-user --token=${KUBE_TOKEN}
kubectl config set-context ${KUBE_CLUSTER} --cluster=${KUBE_CLUSTER} --user=deploy-user --namespace=${KUBE_NAMESPACE}
kubectl config use-context ${KUBE_CLUSTER}
- name: Rollout restart deployment
run: |
kubectl set image -n ${KUBE_NAMESPACE} \
deployment/contact-moj \
webapp="${{ vars.ECR_URL }}:$SHA" \
jobs="${{ vars.ECR_URL }}:$SHA" \
metrics="${{ vars.ECR_URL }}:$SHA"
notify-production:
needs: [build, deploy-production]
uses: ./.github/workflows/notification.yml
secrets:
webhook_url: ${{ secrets.SLACK_WEBHOOK_URL }}
with:
build_tag: ${{ needs.build.outputs.build_tag }}
environment: Production
notify-production-2:
needs: [build, deploy-production]
uses: ./.github/workflows/notification.yml
secrets:
webhook_url: ${{ secrets.PROD_SLACK_WEBHOOK_URL }}
with:
build_tag: ${{ needs.build.outputs.build_tag }}
environment: Production