-
Notifications
You must be signed in to change notification settings - Fork 3
199 lines (177 loc) · 7.05 KB
/
branch-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
name: Branch Based Deploy
on:
pull_request:
branches:
- main
# Environment variables available to all jobs and steps in this workflow
env:
AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }}
ECR_ENDPOINT: ${{ secrets.ECR_ENDPOINT }}
KUBE_CACERT: ${{ secrets.KUBE_CACERT }}
KUBE_CLUSTER: ${{ secrets.KUBE_CLUSTER }}
KUBE_NAME: ${{ secrets.KUBE_NAME }}
KUBE_NAMESPACE: ${{ secrets.KUBE_NAMESPACE }}
KUBE_TOKEN: ${{ secrets.KUBE_TOKEN }}
GITHUB_TEAM_NAME_SLUG: book-a-secure-move
jobs:
setup-build-publish-deploy:
name: Setup, Build, Publish, and Deploy
runs-on: ubuntu-latest
permissions:
deployments: write
id-token: write
contents: read
steps:
- id: create_deployment
name: Create deployment
uses: actions/github-script@v7
with:
github-token: ${{ secrets.BRANCH_BASED_DEPLOY_TOKEN }}
script: |
try {
return (await github.rest.repos.createDeployment({
owner: context.repo.owner,
repo: context.repo.repo,
ref: '${{ github.head_ref }}',
transient_environment: true,
required_contexts: [],
environment: "booksecuremove-pr-${{ github.event.number }}"
})).data.id
} catch (e) {
core.setFailed(e)
}
- name: Update deployment
uses: actions/github-script@v6
with:
github-token: ${{ secrets.BRANCH_BASED_DEPLOY_TOKEN }}
script: |
try {
github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: ${{ steps.create_deployment.outputs.result }},
state: 'in_progress'
});
} catch (e) {
core.setFailed(e)
}
- name: Delete deployments
uses: actions/github-script@v6
with:
github-token: ${{ secrets.BRANCH_BASED_DEPLOY_TOKEN }}
script: |
try {
let deployments = (
await github.rest.repos.listDeployments({
owner: context.repo.owner,
repo: context.repo.repo,
environment: "booksecuremove-pr-${{ github.event.number }}",
})
).data.filter(d => d.id != ${{ steps.create_deployment.outputs.result }});
deployments = (await Promise.all(
deployments.map(async (d) => {
const lastState = (
await github.rest.repos.listDeploymentStatuses({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: d.id,
})
).data[0].state;
if (lastState == "inactive") return;
return d;
})
)).filter(d => d);
await Promise.all(
deployments.map(d => {
return github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: d.id,
state: "inactive",
});
})
);
} catch (e) {
core.setFailed(e)
}
- name: Checkout
uses: actions/checkout@v4
- name: Set up NodeJS
uses: actions/setup-node@v4
with:
node-version: 20
- name: Compile typescript
run: |
npm install
npx tsc --project ./
- name: Build docker image
env:
PR_NUMBER: ${{ github.event.number }}
run: |
export BUILD_DATE=$(date -Is)
docker buildx build \
--pull \
--label build.git.sha=$GITHUB_SHA \
--label build.git.branch=$GITHUB_HEAD_REF \
--label build.date=$BUILD_DATE \
--build-arg APP_BUILD_DATE=$BUILD_DATE \
--build-arg APP_BUILD_TAG=$PR_NUMBER \
--build-arg APP_GIT_COMMIT=$GITHUB_SHA \
--build-arg APP_BUILD_BRANCH=$GITHUB_HEAD_REF \
-t app .
- name: Authenticate with cluster
run: |
echo -n $KUBE_CACERT | base64 -d > ./ca.crt
kubectl config set-cluster ${KUBE_CLUSTER} --certificate-authority=./ca.crt --server=https://${KUBE_CLUSTER}
kubectl config set-credentials circleci --token=${KUBE_TOKEN}
kubectl config set-context ${KUBE_CLUSTER} --cluster=${KUBE_CLUSTER} --user=circleci --namespace=${KUBE_NAMESPACE}
kubectl config use-context ${KUBE_CLUSTER}
# Assume role in Cloud Platform
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.STAGING_ECR_ROLE_TO_ASSUME }}
aws-region: ${{ vars.STAGING_ECR_REGION }}
# Login to container repository
- uses: aws-actions/amazon-ecr-login@v2
id: login-ecr
- name: Push docker image to ECR
env:
PR_NUMBER: ${{ github.event.number }}
REPOSITORY: ${{ vars.STAGING_ECR_REPOSITORY }}
REGISTRY: ${{ steps.login-ecr.outputs.registry }}
run: |
DOCKER_IMAGE_URL="${REGISTRY}/${REPOSITORY}:pr-${PR_NUMBER}.latest"
echo "DOCKER_IMAGE_URL=${DOCKER_IMAGE_URL}" >> $GITHUB_ENV
docker tag app $DOCKER_IMAGE_URL
docker push $DOCKER_IMAGE_URL
- name: Upload kube configs
env:
PR_NUMBER: ${{ github.event.number }}
DOCKER_IMAGE_URL: ${{ env.DOCKER_IMAGE_URL }}
run: |
sed -i "s/{PR_NUMBER}/$PR_NUMBER/g" .github/branch-deploy/config-map.yml
sed -i "s/{PR_NUMBER}/$PR_NUMBER/g" .github/branch-deploy/service.yml
sed -i "s/{PR_NUMBER}/$PR_NUMBER/g" .github/branch-deploy/deployment.yml
sed -i "s/{PR_NUMBER}/$PR_NUMBER/g" .github/branch-deploy/ingress.yml
sed -i "s>{DOCKER_IMAGE_URL}>$DOCKER_IMAGE_URL>g" .github/branch-deploy/deployment.yml
kubectl apply -f .github/branch-deploy/config-map.yml
kubectl apply -f .github/branch-deploy/service.yml
kubectl apply -f .github/branch-deploy/deployment.yml
kubectl apply -f .github/branch-deploy/ingress.yml
kubectl rollout restart deployment "hmpps-book-secure-move-frontend-deployment-pr-${PR_NUMBER}"
- name: Update deployment
uses: actions/github-script@v6
with:
github-token: ${{ secrets.BRANCH_BASED_DEPLOY_TOKEN }}
script: |
try {
github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: ${{ steps.create_deployment.outputs.result }},
state: 'success',
environment_url: 'https://hmpps-book-secure-move-frontend-pr-${{ github.event.number }}.apps.cloud-platform.service.justice.gov.uk'
});
} catch (e) {
core.setFailed(e)
}