-
Notifications
You must be signed in to change notification settings - Fork 0
209 lines (184 loc) · 8.45 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
name: Deploy
on:
push:
branches: [terraform]
paths-ignore:
- LICENSE
- README.md
concurrency: ${{ github.ref }}
env:
tf_version: "1.4.5" # must match value in terraform-iac/*/app/main.tf
node_version: "16.x" # must match value in log-analyzer/sorter-lambda/package.json
jobs:
env:
name: Set Env Vars
runs-on: ubuntu-latest
steps:
- name: Set up DEV Environment Variables
if: github.ref == 'refs/heads/terraform'
run: |
matrix='{
"env":[
{
"aws_account":"637423550675",
"aws_gha_role":"web-cdn-dev-gha",
"rfc_key_name":"standard_change_sandbox_client_key",
"rfc_secret_name":"standard_change_sandbox_client_secret",
"rfc_template_id":"Codepipeline-Standard-Change",
"assembler_ecr_repo_name" : "web-cdn-dev-assembler",
"webhooks_ecr_repo_name" : "web-cdn-dev-webhooks",
"log_sorter_ecr_repo_name" : "web-cdn-dev-log-sorter",
"tf_working_dir":"./iac/dev/app"
}
]
}'
echo matrix=`echo $matrix | jq -c .` >> $GITHUB_ENV
# TODO: Update for prd
- name: Set up PRD Environment Variables
if: github.ref == 'refs/heads/master'
run: |
matrix='{
"env":[
{
"aws_account":"204581410681",
"aws_gha_role":"web-cdn-prd-gha",
"rfc_key_name":"standard_change_production_client_key",
"rfc_secret_name":"standard_change_production_client_secret",
"rfc_template_id":"Codepipeline-Standard-Change",
"assembler_ecr_repo_name" : "web-cdn-prd-assembler",
"webhooks_ecr_repo_name" : "web-cdn-prd-webhooks",
"log_sorter_ecr_repo_name" : "web-cdn-prd-log-sorter"
"tf_working_dir":"./iac/prd/app"
}
]
}'
echo matrix=`echo $matrix | jq -c .` >> $GITHUB_ENV
outputs:
matrix: ${{ env.matrix }}
build_and_deploy:
name: Build CDN pipeline
runs-on: ubuntu-latest
needs: env
strategy:
matrix: ${{ fromJson(needs.env.outputs.matrix) }}
permissions: write-all
steps:
- name: Check out
uses: actions/checkout@v3
- name: Disallow Concurrent Runs
uses: byu-oit/github-action-disallow-concurrent-runs@v2
with:
token: ${{ github.token }}
- name: Configure AWS Credentials
id: awscreds
uses: aws-actions/configure-aws-credentials@v2
with:
role-to-assume: "arn:aws:iam::${{ matrix.env.aws_account }}:role/${{ matrix.env.aws_gha_role }}"
role-session-name: ${{ github.sha }}
aws-region: us-east-1
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: ${{ env.node_version }}
# this is necessary because Lambda@Edge functions cannot be pulled from AWS ECR, so we cannot use a Dockerfile to deploy :(
# this step installs the node_modules for the lambda, the terraform apply zips it up
# see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-at-edge-function-restrictions.html
- name: npm install in enhanced-headers
working-directory: edge-lambdas/enhanced-headers
run: npm ci --prefer-offline
# this is necessary because Lambda@Edge functions cannot be pulled from AWS ECR, so we cannot use a Dockerfile to deploy :(
# this step installs the node_modules for the lambda, the terraform apply zips it up
# see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-at-edge-function-restrictions.html
- name: npm install in eager-redirect
working-directory: edge-lambdas/eager-redirect
run: npm ci --prefer-offline
# both the webhook (assembler trigger) lambda and the assembler need this file, but in order to keep the context of our docker builds simple, we copy it into both places here
# a better implementation of the webhook lambda might not need the main-config.yml file if we authenticated the github repos calling the lambda in a better way, but that is beyond the scope of the current work
- name: Copy main-config.yml to /webhooks and /assembler
run: cp ./main-config.yml ./assembler/main-config.yml && cp ./main-config.yml ./webhooks/main-config.yml
- name: Log into Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Get Current Timestamp
id: date
run: echo "timestamp=$(date +'%Y-%m-%d_%H-%M-%S')" >> $GITHUB_OUTPUT
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Build and push the Assembler Docker image
env:
ASSEMBLER_ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ASSEMBLER_ECR_REPO: ${{ matrix.env.assembler_ecr_repo_name }}
ASSEMBLER_IMAGE_TAG: ${{ steps.date.outputs.timestamp }}
uses: docker/build-push-action@v3
with:
context: assembler
push: true
tags: ${{ env.ASSEMBLER_ECR_REGISTRY }}/${{ env.ASSEMBLER_ECR_REPO }}:${{ env.ASSEMBLER_IMAGE_TAG }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Build and push the Log Sorter Docker image
env:
ANALYZER_ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ANALYZER_ECR_REPO: ${{ matrix.env.log_sorter_ecr_repo_name }}
ANALYZER_IMAGE_TAG: ${{ steps.date.outputs.timestamp }}
uses: docker/build-push-action@v3
with:
context: log-analyzer/sorter-lambda
provenance: false # see https://github.com/orgs/byu-oit/discussions/56
push: true
tags: ${{ env.ANALYZER_ECR_REGISTRY }}/${{ env.ANALYZER_ECR_REPO }}:${{ env.ANALYZER_IMAGE_TAG }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Build and push the Webhook Docker image
env:
WEBHOOKS_ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
WEBHOOKS_ECR_REPO: ${{ matrix.env.webhooks_ecr_repo_name }}
WEBHOOKS_IMAGE_TAG: ${{ steps.date.outputs.timestamp }}
uses: docker/build-push-action@v3
with:
context: webhooks
provenance: false # see https://github.com/orgs/byu-oit/discussions/56
push: true
tags: ${{ env.WEBHOOKS_ECR_REGISTRY }}/${{ env.WEBHOOKS_ECR_REPO }}:${{ env.WEBHOOKS_IMAGE_TAG }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Terraform Setup
uses: hashicorp/setup-terraform@v2
with:
terraform_version: ${{ env.tf_version }}
terraform_wrapper: false
- name: Terraform Init
working-directory: ${{ matrix.env.tf_working_dir }}
run: terraform init
- name: Terraform Format
working-directory: "./"
run: terraform fmt -check -recursive
- name: Terraform Plan
working-directory: ${{ matrix.env.tf_working_dir }}
run: terraform plan -var 'image_tag=${{ steps.date.outputs.timestamp }}' -input=false -out=plan
- name: Analyze Terraform Plan
uses: byu-oit/github-action-tf-plan-analyzer@v2
with:
working-directory: ${{ matrix.env.tf_working_dir }}
terraform-plan-file: plan
divvycloud-username: ${{ secrets.DIVVYCLOUD_USERNAME }}
divvycloud-password: ${{ secrets.DIVVYCLOUD_PASSWORD }}
- name: Start Standard Change
uses: byu-oit/github-action-start-standard-change@v1
id: start-standard-change
with:
client-key: ${{ secrets[matrix.env.rfc_key_name] }}
client-secret: ${{ secrets[matrix.env.rfc_secret_name] }}
template-id: ${{ matrix.env.rfc_template_id }}
- name: Terraform Apply
working-directory: ${{ matrix.env.tf_working_dir }}
run: terraform apply plan
- name: End Standard Change
uses: byu-oit/github-action-end-standard-change@v1
if: always() && steps.start-standard-change.outcome == 'success' # Run if RFC started, even if the deploy failed
with:
client-key: ${{ secrets[matrix.env.rfc_key_name] }}
client-secret: ${{ secrets[matrix.env.rfc_secret_name] }}
change-sys-id: ${{ steps.start-standard-change.outputs.change-sys-id }}
work-start: ${{ steps.start-standard-change.outputs.work-start }}
success: ${{ job.status == 'success' }}