-
Notifications
You must be signed in to change notification settings - Fork 21
216 lines (205 loc) · 7.8 KB
/
build-and-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
name: Build and deploy GOST
on:
push:
branches:
- _staging
- main
concurrency:
group: ${{ github.workflow_ref }}
permissions:
contents: read
id-token: write
packages: write
jobs:
changes:
name: Detect changes
runs-on: ubuntu-latest
outputs:
api: ${{ steps.filter.outputs.api }}
website: ${{ steps.filter.outputs.website }}
terraform: ${{ steps.filter.outputs.terraform }}
steps:
- uses: actions/checkout@v3
- uses: dorny/paths-filter@v2
id: filter
with:
base: ${{ github.ref }}
filters: |
api:
- ".github/workflows/build-api.yml"
- '.github/workflows/build-and-deploy.yml'
- 'packages/server/**'
- 'docker/production-api.Dockerfile'
- '.nvmrc'
- 'yarn.lock'
website:
- ".github/workflows/build-website.yml"
- '.github/workflows/build-and-deploy.yml'
- 'packages/client/**'
- '.node-version'
- '.nvmrc'
- 'yarn.lock'
terraform:
- 'terraform/**'
build_api:
name: Build and push GOST API Docker image
needs:
- changes
if: needs.changes.outputs.api == 'true'
uses: "./.github/workflows/build-api.yml"
permissions:
contents: read
packages: write
build_website:
name: Build website deployment artifact
needs:
- changes
if: needs.changes.outputs.website == 'true'
uses: "./.github/workflows/build-website.yml"
permissions:
contents: read
select_target_environment:
name: Select target environment
uses: "./.github/workflows/select-target-environment.yml"
with:
ref_name: "${{ github.ref_name }}"
deploy_terraform:
name: Deploy terraform
runs-on: ubuntu-latest
needs:
- select_target_environment
- changes
if: needs.changes.outputs.terraform == 'true'
concurrency:
group: run_terraform-${{ needs.select_target_environment.outputs.selected }}
cancel-in-progress: false
environment: ${{ needs.select_target_environment.outputs.selected }}
env:
TF_PLUGIN_CACHE_DIR: ~/.terraform.d/plugin-cache
TF_VAR_version_identifier: ${{ github.sha }}
TF_VAR_datadog_api_key: ${{ secrets.DATADOG_API_KEY }}
TF_VAR_datadog_app_key: ${{ secrets.DATADOG_APP_KEY }}
steps:
- uses: actions/checkout@v3
- name: Get project TF version
id: get_version
run: echo "TF_VERSION=$(cat .terraform-version | tr -d '[:space:]')" | tee -a $GITHUB_OUTPUT
working-directory: terraform
- uses: hashicorp/setup-terraform@v2
with:
terraform_version: ${{ steps.get_version.outputs.TF_VERSION }}
- name: Ensure Terraform plugin cache exists
run: mkdir -p $TF_PLUGIN_CACHE_DIR
- name: Save/Restore Terraform plugin cache
uses: actions/cache@v3
with:
path: ${{ env.TF_PLUGIN_CACHE_DIR }}
key: ${{ runner.os }}-terraform-${{ hashFiles('**/.terraform.lock.hcl') }}
restore-keys: |
${{ runner.os }}-terraform-
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-region: us-west-2
role-to-assume: "${{ secrets.AWS_ROLE_TO_ASSUME }}"
- name: Terraform Init
id: init
run: terraform init -backend-config="${{ needs.select_target_environment.outputs.selected }}.s3.tfbackend"
working-directory: terraform
- name: Terraform Validate
id: validate
run: terraform validate -no-color
working-directory: terraform
- name: Terraform Apply
if: steps.validate.outcome == 'success'
id: apply
run: terraform apply -auto-approve -input=false -no-color -var-file="${{ needs.select_target_environment.outputs.selected }}.tfvars"
working-directory: terraform
deploy_website:
name: Deploy website
runs-on: ubuntu-latest
needs:
- build_website
- select_target_environment
- deploy_terraform
if: |
always() &&
needs.build_website.result == 'success' &&
(needs.deploy_terraform.result == 'success' || needs.deploy_terraform.result == 'skipped')
environment: ${{ needs.select_target_environment.outputs.selected }}
steps:
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-region: us-west-2
role-to-assume: "${{ secrets.AWS_ROLE_TO_ASSUME }}"
- name: Get deployment parameters
uses: dkershner6/aws-ssm-getparameters-action@v1
with:
withDecryption: "true"
parameterPairs: |
/gost/${{ needs.select_target_environment.outputs.selected }}/deploy-config/website/s3-uri = S3_DEPLOYMENT_URI,
/gost/${{ needs.select_target_environment.outputs.selected }}/deploy-config/website/distribution-id = CLOUDFRONT_DISTRIBUTION_ID
- name: Download build
uses: actions/download-artifact@v3
with:
name: ${{ needs.build_website.outputs.artifact }}
path: dist
- name: Upload artifact to S3
run: aws s3 sync ./dist ${{ env.S3_DEPLOYMENT_URI }} --sse --delete --no-progress
- name: Invalidate CloudFront cache
run: aws cloudfront create-invalidation --paths "/*" --distribution-id ${{ env.CLOUDFRONT_DISTRIBUTION_ID }}
deploy_api:
name: Deploy API
runs-on: ubuntu-latest
needs:
- build_api
- select_target_environment
- deploy_terraform
if: |
always() &&
needs.build_api.result == 'success' &&
(needs.deploy_terraform.result == 'success' || needs.deploy_terraform.result == 'skipped')
environment: ${{ needs.select_target_environment.outputs.selected }}
steps:
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-region: us-west-2
role-to-assume: "${{ secrets.AWS_ROLE_TO_ASSUME }}"
- name: Get deployment parameters
uses: dkershner6/aws-ssm-getparameters-action@v1
with:
withDecryption: "true"
parameterPairs: |
/gost/${{ needs.select_target_environment.outputs.selected }}/deploy-config/api/cluster-name = ECS_CLUSTER_NAME,
/gost/${{ needs.select_target_environment.outputs.selected }}/deploy-config/api/service-name = ECS_SERVICE_NAME
- name: Update ECS service
run: aws ecs update-service --cluster ${{ env.ECS_CLUSTER_NAME }} --service ${{ env.ECS_SERVICE_NAME }} --force-new-deployment > /dev/null
deploy_grants_consumer:
name: Deploy Grants Consumer
runs-on: ubuntu-latest
needs:
- build_api
- select_target_environment
- deploy_terraform
if: |
always() &&
needs.build_api.result == 'success' &&
(needs.deploy_terraform.result == 'success' || needs.deploy_terraform.result == 'skipped')
environment: ${{ needs.select_target_environment.outputs.selected }}
steps:
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-region: us-west-2
role-to-assume: "${{ secrets.AWS_ROLE_TO_ASSUME }}"
- name: Get deployment parameters
uses: dkershner6/aws-ssm-getparameters-action@v1
with:
withDecryption: "true"
parameterPairs: |
/gost/${{ needs.select_target_environment.outputs.selected }}/deploy-config/consume-grants/cluster-name = ECS_CLUSTER_NAME,
/gost/${{ needs.select_target_environment.outputs.selected }}/deploy-config/consume-grants/service-name = ECS_SERVICE_NAME
- name: Update ECS service
run: aws ecs update-service --cluster ${{ env.ECS_CLUSTER_NAME }} --service ${{ env.ECS_SERVICE_NAME }} --force-new-deployment > /dev/null