1
+ name : Terraform Deployment
2
+
3
+ on :
4
+ push :
5
+ branches :
6
+ - develop
7
+ - main
8
+
9
+ jobs :
10
+ terraform :
11
+ runs-on : ubuntu-latest
12
+
13
+ defaults :
14
+ run :
15
+ shell : bash
16
+
17
+ steps :
18
+ - name : Checkout code
19
+ uses : actions/checkout@v2
20
+
21
+ - name : Setup Terraform
22
+ uses : hashicorp/setup-terraform@v2
23
+ with :
24
+ terraform_version : 1.8.3
25
+
26
+ - name : Terraform Init
27
+ run : |
28
+ cd infra && terraform init \
29
+ -backend-config="bucket=${{ env.TERRAFORM_S3_STATEFILE_BUCKET }}" \
30
+ -backend-config="${{ github.event.repository.name }}" \
31
+ -backend-config="region=${{ env.AWS_REGION }}" \
32
+ -backend-config="dynamodb_table=${{ env.TERRAFORM_DYNAMODB_LOCK_TABLE }}"
33
+
34
+ - name : Terraform Validate
35
+ run : terraform validate
36
+
37
+ - name : Terraform Create/Select Dev Workspace
38
+ if : github.ref == 'refs/heads/develop' && github.event_name == 'push'
39
+ id : select-dev-workspace
40
+ run : |
41
+ terraform workspace list | grep -q "dev" || terraform workspace new dev
42
+ terraform workspace select dev
43
+
44
+ - name : Terraform Plan for Dev
45
+ if : github.ref == 'refs/heads/develop' && github.event_name == 'push'
46
+ id : terraform-plan-dev
47
+ run : terraform plan -var-file="envs/dev/terraform.tfvars" -out=dev.plan
48
+
49
+ - name : Terraform Apply for Dev
50
+ id : terraform-apply-dev
51
+ if : github.ref == 'refs/heads/develop' && github.event_name == 'push'
52
+ run : terraform apply dev.plan --auto-approve
53
+
54
+ - name : Terraform Create/Select Prod Workspace
55
+ if : github.ref == 'refs/heads/main' && github.event_name == 'push'
56
+ id : select-prod-workspace
57
+ run : |
58
+ terraform workspace list | grep -q "prod" || terraform workspace new prod
59
+ terraform workspace select prod
60
+
61
+ - name : Terraform Plan for Prod
62
+ id : terraform-plan-prod
63
+ run : terraform plan -var-file="envs/prod/terraform.tfvars" -out=prod.plan
64
+
65
+ - name : Terraform Apply for Prod
66
+ id : terraform-apply-prod
67
+ if : github.ref == 'refs/heads/main' && github.event_name == 'push'
68
+ run : terraform apply prod.plan --auto-approve
0 commit comments