-
Notifications
You must be signed in to change notification settings - Fork 0
266 lines (231 loc) · 8.34 KB
/
deploy-aws.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
name: Deploy to staging (aws)
on:
workflow_dispatch:
push:
branches:
- main
env:
TERRAFORM_ROOT: terraform
DOCKER_ENV: production
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
TF_VAR_env: staging
TF_VAR_redis_url: ${{ secrets.REDIS_URL }}
TF_VAR_api_base_url: 'pets-staging.lhowsam.com'
TF_VAR_session_secret: ${{ secrets.SESSION_SECRET }}
TF_VAR_database_url: ${{ secrets.DATABASE_URL }}
TF_VAR_deployed_by: ${{ github.actor }}
TF_VAR_private_key: ${{ secrets.STG_PRIVATE_KEY }}
TF_VAR_certificate_body: ${{ secrets.STG_CERT_BODY }}
TF_VAR_certificate_chain: ${{ secrets.CERTIFICATE_CHAIN }}
TF_VAR_zone_id: ${{ secrets.ZONE_ID }}
TF_VAR_docker_image_tag: ${{ github.sha }}
TF_VAR_s3_assets_bucket: pets-api-staging-assets
TF_VAR_s3_assets_region: eu-west-2
TF_VAR_s3_assets_access_key_id: ${{ secrets.ASSETS_KEY }}
TF_VAR_s3_assets_secret_access_key: ${{ secrets.ASSETS_SECRET }}
TF_VAR_git_sha: ${{ github.sha }}
TF_VAR_session_domain: pets-staging.lhowsam.com
# needed for tests
S3_ASSETS_BUCKET: pets-api-staging-assets
S3_ASSETS_REGION: eu-west-2
S3_ASSETS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
S3_ASSETS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
DATABASE_URL: postgres://pets:pets@localhost:5432/pets
REDIS_URL: redis://localhost:6379
API_BASE_URL: http://localhost:8000
jobs:
provision-ecr:
name: Provision ECR repository if needed
runs-on: ubuntu-latest
concurrency:
group: deploy-group
cancel-in-progress: true
timeout-minutes: 10
steps:
- name: Install moreutils
run: sudo apt install moreutils
# https://github.com/actions/virtual-environments/issues/1187
- name: tune linux network
run: sudo ethtool -K eth0 tx off rx off
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.head_ref }}
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v3
with:
aws-access-key-id: ${{ env.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ env.AWS_SECRET_ACCESS_KEY }}
aws-region: eu-west-2
mask-aws-account-id: true
- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
- name: Terraform init
id: init
working-directory: ${{ env.TERRAFORM_ROOT }}
run: |
terraform init -backend-config="key=vpc/${{ env.TF_VAR_env }}.tfstate" -backend-config="bucket=pets-api-${{ env.TF_VAR_env }}-terraform-state" -input=false
- name: Terraform fmt -check
id: fmt
run: terraform fmt -check
working-directory: ${{ env.TERRAFORM_ROOT }}
- name: Terraform validate
id: validate
run: terraform validate
working-directory: ${{ env.TERRAFORM_ROOT }}
- name: Terraform plan (ECR repo)
id: plan
run: |
terraform plan \
-target=aws_ecr_repository.ecr_repo \
-out ./app.out
working-directory: ${{ env.TERRAFORM_ROOT }}
- name: Terraform apply (ECR repo)
id: apply
run: terraform apply -auto-approve ./app.out
working-directory: ${{ env.TERRAFORM_ROOT }}
build:
name: Build server
runs-on: ubuntu-latest
timeout-minutes: 10
needs: provision-ecr
concurrency:
group: deploy-build-group
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.head_ref }}
- name: install
uses: ./.github/actions/install
- name: build
uses: ./.github/actions/build
build-and-push-docker:
name: Build and push Docker image
needs: [provision-ecr, build]
runs-on: ubuntu-latest
timeout-minutes: 15
concurrency:
group: deploy-docker-group
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.head_ref }}
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v3
with:
aws-access-key-id: ${{ env.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ env.AWS_SECRET_ACCESS_KEY }}
aws-region: eu-west-2
mask-aws-account-id: true
- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_version: 1.1.8
terraform_wrapper: false
- name: Terraform init
id: init
working-directory: ${{ env.TERRAFORM_ROOT }}
run: |
terraform init -backend-config="key=vpc/${{ env.TF_VAR_env }}.tfstate" -backend-config="bucket=pets-api-${{ env.TF_VAR_env }}-terraform-state" -input=false
- name: terraform plan
id: plan
run: terraform plan -out ./app.out
working-directory: ${{ env.TERRAFORM_ROOT }}
- name: Output terraform variables to file
id: output
working-directory: ${{ env.TERRAFORM_ROOT }}
run: terraform output -json > ../terraform-outputs.json
- name: Set variables from terraform outputs
run: |
outputs=$(cat terraform-outputs.json)
ecr_repo_name=$(echo $outputs | jq -r .ecr_repo_name.value)
echo "ECR_REPO_NAME=$ecr_repo_name" >> $GITHUB_ENV
- name: Login to ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: build, tag and push image to ECR
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ env.ECR_REPO_NAME }}
run: |
docker build \
--build-arg NODE_ENV=${{ env.DOCKER_ENV }} \
-t $ECR_REGISTRY/$ECR_REPOSITORY:${{ env.TF_VAR_docker_image_tag }} -f Dockerfile .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:${{ env.TF_VAR_docker_image_tag }}
migrate-db:
name: Migrate database
needs: [build-and-push-docker]
runs-on: ubuntu-latest
timeout-minutes: 10
concurrency:
group: deploy-migratedb-group
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.head_ref }}
- name: install
uses: ./.github/actions/install
- name: Migrate database
run: DATABASE_URL=${{ secrets.DATABASE_URL }} pnpm db:migrate-prod
deploy-infra:
name: Deploy infrastructure
needs: [build-and-push-docker, migrate-db]
runs-on: ubuntu-latest
timeout-minutes: 15
concurrency:
group: deploy-infra-group
steps:
- name: Install moreutils
run: sudo apt install moreutils
# https://github.com/actions/virtual-environments/issues/1187
- name: tune linux network
run: sudo ethtool -K eth0 tx off rx off
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.head_ref }}
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v3
with:
aws-access-key-id: ${{ env.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ env.AWS_SECRET_ACCESS_KEY }}
aws-region: eu-west-2
- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
- name: Terraform Init
id: init
working-directory: ${{ env.TERRAFORM_ROOT }}
run: terraform init -backend-config="key=vpc/${{ env.TF_VAR_env }}.tfstate" -backend-config="bucket=pets-api-${{ env.TF_VAR_env }}-terraform-state" -input=false
- name: Terraform Validate
id: validate
run: terraform validate
working-directory: ${{ env.TERRAFORM_ROOT }}
- name: Terraform Plan
id: plan
working-directory: ${{ env.TERRAFORM_ROOT }}
run: |
terraform plan \
-out ./app.out
- name: Terraform Apply
id: apply
run: terraform apply ./app.out
working-directory: ${{ env.TERRAFORM_ROOT }}
# TODO: add a script that polls /version
# and looks for the GIT_SHA to match what we have in ci
# so we know when the containers
# have finished swapping over during
# blue green deployment rollouts
# otherwise we'll be testing the old container :)
# - name: e2e-staging
# uses: ./.github/actions/e2e
# with:
# target: e2e-staging