1
+ name : " [DEV] - Terraform Deployment"
2
+
3
+ on :
4
+ push :
5
+ branches :
6
+ - develop
7
+
8
+ permissions :
9
+ id-token : write
10
+ contents : read
11
+
12
+ jobs :
13
+ terraform :
14
+ runs-on : ubuntu-latest
15
+
16
+ defaults :
17
+ run :
18
+ shell : bash
19
+
20
+ steps :
21
+ - name : Checkout code
22
+ uses : actions/checkout@v4
23
+
24
+ - name : Setup Terraform
25
+ uses : hashicorp/setup-terraform@v3
26
+ with :
27
+ terraform_version : 1.8.3
28
+
29
+ - name : Configure AWS credentials
30
+ uses : aws-actions/configure-aws-credentials@v4
31
+ with :
32
+ role-to-assume : arn:aws:iam::179916804929:role/BuildRun-GithubActions-Role
33
+ role-session-name : GitHub_to_AWS_via_FederatedOIDC
34
+ aws-region : ${{ vars.AWS_REGION }}
35
+
36
+ - name : Read destroy configuration
37
+ id : read-destroy-config
38
+ run : |
39
+ DESTROY_DEV="$(jq -r '.dev' ./infra/destroy_config.json)"
40
+ echo "destroy_dev=$(echo $DESTROY_DEV)" >> $GITHUB_OUTPUT
41
+
42
+ - name : Terraform Init
43
+ run : |
44
+ cd infra && terraform init \
45
+ -backend-config="bucket=${{ vars.TERRAFORM_S3_STATEFILE_BUCKET }}" \
46
+ -backend-config="key=${{ github.event.repository.name }}" \
47
+ -backend-config="region=${{ env.AWS_REGION }}" \
48
+ -backend-config="dynamodb_table=${{ vars.TERRAFORM_DYNAMODB_LOCK_TABLE }}"
49
+
50
+ - name : Terraform Validate
51
+ run : terraform validate
52
+
53
+ - name : Terraform Destroy for Dev
54
+ if : steps.read-destroy-config.outputs.destroy_dev == 'true' && github.ref == 'refs/heads/develop' && github.event_name == 'push'
55
+ id : terraform-destroy-dev
56
+ run : cd infra &&
57
+ terraform workspace select dev || terraform workspace new dev &&
58
+ terraform destroy -var-file="./envs/dev/terraform.tfvars" -auto-approve
59
+
60
+ - name : Terraform Plan for Dev
61
+ if : steps.read-destroy-config.outputs.destroy_dev != 'true' && github.ref == 'refs/heads/develop' && github.event_name == 'push'
62
+ id : terraform-plan-dev
63
+ run : cd infra &&
64
+ terraform workspace select dev || terraform workspace new dev &&
65
+ terraform plan -var-file="./envs/dev/terraform.tfvars" -out=dev.plan
66
+
67
+ - name : Terraform Apply for Dev
68
+ id : terraform-apply-dev
69
+ if : steps.read-destroy-config.outputs.destroy_dev != 'true' && github.ref == 'refs/heads/develop' && github.event_name == 'push'
70
+ run : cd infra &&
71
+ terraform workspace select dev || terraform workspace new dev &&
72
+ terraform apply "dev.plan"
0 commit comments