forked from bcgov/supreme-court-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
120 lines (112 loc) · 3.6 KB
/
aws-template-terraform.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
name: AWS Template Workflow
on:
workflow_call:
inputs:
CONTEXT_FOLDER:
required: true
type: string
ENVIRONMENT_NAME:
required: true
type: string
CHANGE_FOLDER_NAME:
required: true
type: string
TEST_BUCKET_NAME:
required: true
type: string
permissions:
id-token: write
contents: read
jobs:
check_changes:
name: Check Changes
runs-on: ubuntu-20.04
outputs:
infra_changed: ${{ steps.check_changes.outputs.infra_changed }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2
- name: Check modified folders
id: check_changes
env:
CONTEXT_FOLDER: ${{ inputs.CONTEXT_FOLDER }}
CHANGE_FOLDER_NAME: ${{ inputs.CHANGE_FOLDER_NAME }}
run: |
echo "=============== list modified files ==============="
git diff --name-only HEAD^ HEAD
echo "========== check paths of modified files =========="
git diff --name-only HEAD^ HEAD >> files.txt
infra_changed=false
while IFS= read -r file
do
echo $file
if [[ $file == $CHANGE_FOLDER_NAME/* ]]; then
infra_changed=true
break
fi
done < files.txt
echo "infra_changed=$infra_changed" >> "$GITHUB_OUTPUT"
scan:
name: Scan TF Code
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Run tfsec
uses: aquasecurity/tfsec-action
with:
working_directory: ${{ inputs.CONTEXT_FOLDER }}
needs: [check_changes]
deploy_infra:
name: Deploy Infra
runs-on: ubuntu-latest
environment:
name: ${{ inputs.ENVIRONMENT_NAME }}
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 }}
needs: [scan]
steps:
- name: Checkout repository
uses: actions/checkout@v4
- 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.9.0
- name: Terraform Init
id: init
env:
CONTEXT_FOLDER: ${{ inputs.CONTEXT_FOLDER }}
run: |
terraform init -input=false -backend-config=backend.tfvars -var-file=${{ inputs.ENVIRONMENT_NAME }}.tfvars
working-directory: ${{ inputs.CONTEXT_FOLDER }}
- name: Terraform Plan
id: plan
env:
CONTEXT_FOLDER: ${{ inputs.CONTEXT_FOLDER }}
#TF_VAR_test_s3_bucket_name: ${{ inputs.TEST_BUCKET_NAME }}
run: |
terraform plan -no-color -input=false -var-file=${{ inputs.ENVIRONMENT_NAME }}.tfvars
continue-on-error: true
working-directory: ${{ inputs.CONTEXT_FOLDER }}
- name: Terraform Plan Status
if: steps.plan.outcome == 'failure'
run: exit 1
- name: Terraform Apply
env:
CONTEXT_FOLDER: ${{ inputs.CONTEXT_FOLDER }}
#TF_VAR_test_s3_bucket_name: ${{ inputs.TEST_BUCKET_NAME }}
run: |
terraform apply --auto-approve -input=false -var-file=${{ inputs.ENVIRONMENT_NAME }}.tfvars
working-directory: ${{ inputs.CONTEXT_FOLDER }}