forked from bcgov/supreme-court-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
207 lines (178 loc) · 7.32 KB
/
publish-infra.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
name: Publish Infra
on:
# PR targeting master is created or new commits are pushed
push:
branches:
- master
paths:
- "infrastructure/**"
# Manual trigger via GH Actions UI
workflow_dispatch:
inputs:
environment:
description: "Select target environment"
required: true
default: "dev"
type: choice
options:
- dev
- test
- prod
env:
WORKING_DIRECTORY: "./infrastructure/cloud/environments/${{ inputs.environment }}"
DUMMY_IMAGE_NAME: dummy-image
GITHUB_IMAGE_REPO: ghcr.io/bcgov/jasper
APP_ECR_REPO_URL: ${{ vars.AWS_ACCOUNT }}.dkr.ecr.${{ vars.AWS_REGION }}.amazonaws.com/${{ vars.APP_NAME }}-app-repo-${{ inputs.environment }}
LAMBDA_ECR_REPO_URL: ${{ vars.AWS_ACCOUNT }}.dkr.ecr.${{ vars.AWS_REGION }}.amazonaws.com/${{ vars.APP_NAME }}-lambda-repo-${{ inputs.environment }}
jobs:
deploy:
runs-on: ubuntu-latest
environment:
name: ${{ inputs.environment || 'dev' }}
env:
TF_VAR_app_name: ${{ vars.APP_NAME }}
TF_VAR_environment: ${{ vars.ENVIRONMENT_NAME }}
TF_VAR_kms_key_name: ${{ vars.KMS_KEY_NAME }}
TF_VAR_vpc_id: ${{ vars.VPC_ID }}
TF_VAR_lambda_memory_size: ${{ vars.LAMBDA_MEMORY_SIZE }}
permissions:
id-token: write
actions: read
contents: read
security-events: write
packages: write
steps:
- name: Determine environment
id: determine-environment
run: |
if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
echo "environment=${{ github.event.inputs.environment }}" >> $GITHUB_ENV
else
echo "environment=dev" >> $GITHUB_ENV
echo "WORKING_DIRECTORY=${{ env.WORKING_DIRECTORY }}/dev" >> $GITHUB_ENV
fi
- name: Checkout repository
uses: actions/checkout@v4
- name: tfsec
uses: aquasecurity/[email protected]
with:
sarif_file: tfsec.sarif
working_directory: ${{ env.WORKING_DIRECTORY }}
tfsec_args: "--tfvars-file=${{ env.WORKING_DIRECTORY }}/${{ inputs.environment }}.tfvars"
- name: Upload SARIF file
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: tfsec.sarif
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-skip-session-tagging: true
aws-region: ${{ vars.AWS_REGION }}
role-to-assume: ${{ vars.AWS_ROLE_ARN }}
role-duration-seconds: 1800
role-session-name: ci-deployment
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.10.2
- name: Terraform Init
id: init
run: |
terraform init -input=false -backend-config=backend.tfvars -var-file=${{ inputs.environment }}.tfvars
working-directory: ${{ env.WORKING_DIRECTORY }}
- name: Terraform Plan (Initial Stack)
id: plan-initial
run: |
terraform plan -target=module.initial -no-color -input=false -var-file=${{ inputs.environment }}.tfvars
continue-on-error: true
working-directory: ${{ env.WORKING_DIRECTORY }}
- name: Terraform Plan (Initial) Status
if: steps.plan-initial.outcome == 'failure'
run: exit 1
- name: Terraform Apply (Initial Stack)
run: |
terraform apply -target=module.initial --auto-approve -input=false -var-file=${{ inputs.environment }}.tfvars
working-directory: ${{ env.WORKING_DIRECTORY }}
- name: Log in to the GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Setup Image Metadata
id: meta
uses: docker/metadata-action@v5
with:
images: |
${{ env.GITHUB_IMAGE_REPO }}/${{ env.DUMMY_IMAGE_NAME }}
tags: |
type=raw,value=latest
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
driver: docker
- name: Build ${{ env.DUMMY_IMAGE_NAME }} image
uses: docker/build-push-action@v6
with:
push: true
file: ./docker/dummy-image/Dockerfile.release
context: .
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
- name: Login to Amazon ECR
uses: aws-actions/amazon-ecr-login@v2
- name: Check dummy-image in App ECR exists
id: app-ecr-check
shell: bash
run: |
IMAGE_TAG=${{ env.DUMMY_IMAGE_NAME }}
REPOSITORY_NAME=${{ vars.APP_NAME }}-app-repo-${{ inputs.environment }}
IMAGE_EXISTS=$(aws ecr describe-images --repository-name $REPOSITORY_NAME --query "imageDetails[?contains(imageTags, '$IMAGE_TAG')]" --output text)
if [ -z "$IMAGE_EXISTS" ]; then
echo "Image with tag $IMAGE_TAG does not exist."
echo "exists=false" >> $GITHUB_OUTPUT
else
echo "Image with tag $IMAGE_TAG already exists."
echo "exists=true" >> $GITHUB_OUTPUT
fi
- name: Push dummy image to App ECR
if: steps.app-ecr-check.outputs.exists == 'false'
shell: bash
run: |
echo 'Deploying ${{ env.DUMMY_IMAGE_NAME }} to App ECR'
docker tag ${{ env.GITHUB_IMAGE_REPO }}/${{ env.DUMMY_IMAGE_NAME }}:latest ${{ env.APP_ECR_REPO_URL }}:${{ env.DUMMY_IMAGE_NAME }}
docker push ${{ env.APP_ECR_REPO_URL }}:${{ env.DUMMY_IMAGE_NAME }}
- name: Check dummy-image in Lambda ECR exists
id: lambda-ecr-check
shell: bash
run: |
IMAGE_TAG=${{ env.DUMMY_IMAGE_NAME }}
REPOSITORY_NAME=${{ vars.APP_NAME }}-lambda-repo-${{ inputs.environment }}
IMAGE_EXISTS=$(aws ecr describe-images --repository-name $REPOSITORY_NAME --query "imageDetails[?contains(imageTags, '$IMAGE_TAG')]" --output text)
if [ -z "$IMAGE_EXISTS" ]; then
echo "Image with tag $IMAGE_TAG does not exist."
echo "exists=false" >> $GITHUB_OUTPUT
else
echo "Image with tag $IMAGE_TAG already exists."
echo "exists=true" >> $GITHUB_OUTPUT
fi
- name: Push dummy images to Lambda ECR
if: steps.lambda-ecr-check.outputs.exists == 'false'
shell: bash
run: |
echo 'Deploying ${{ env.env.DUMMY_IMAGE_NAME }} to Lambda ECR'
docker tag ${{ env.GITHUB_IMAGE_REPO }}/${{ env.DUMMY_IMAGE_NAME }}:latest ${{ env.LAMBDA_ECR_REPO_URL }}:${{ env.DUMMY_IMAGE_NAME }}
docker push ${{ env.LAMBDA_ECR_REPO_URL }}:${{ env.DUMMY_IMAGE_NAME }}
- name: Terraform Plan (Main Stack)
id: plan-main
run: |
terraform plan -no-color -input=false -var-file=${{ inputs.environment }}.tfvars
continue-on-error: true
working-directory: ${{ env.WORKING_DIRECTORY }}
- name: Terraform Plan (Main) Status
if: steps.plan-main.outcome == 'failure'
run: exit 1
- name: Terraform Apply (Main Stack)
run: |
terraform apply --auto-approve -input=false -var-file=${{ inputs.environment }}.tfvars
working-directory: ${{ env.WORKING_DIRECTORY }}