30
30
aws-secret-access-key : ${{ secrets.AWS_SECRET_ACCESS_KEY }}
31
31
aws-region : ${{ vars.AWS_REGION }}
32
32
33
+ - name : Read destroy configuration
34
+ id : read-destroy-config
35
+ run : |
36
+ DESTROY_DEV="$(jq -r '.dev' ./infra/destroy_config.json)"
37
+ DESTROY_PROD="$(jq -r '.prod' ./infra/destroy_config.json)"
38
+ echo "::set-output name=destroy_dev::$DESTROY_DEV"
39
+ echo "::set-output name=destroy_prod::$DESTROY_PROD"
40
+
33
41
- name : Terraform Init
34
42
run : |
35
43
cd infra && terraform init \
@@ -41,41 +49,43 @@ jobs:
41
49
- name : Terraform Validate
42
50
run : terraform validate
43
51
44
- # - name: Terraform Create Dev Workspace
45
- # if: github.ref == 'refs/heads/develop' && github.event_name == 'push'
46
- # id: select-dev-workspace
47
- # run: |
48
- # terraform workspace list | grep -q "dev" || terraform workspace new dev
52
+ - name : Terraform Destroy for Dev
53
+ if : steps.read-destroy-config.outputs.destroy_dev == 'true' && github.ref == 'refs/heads/develop' && github.event_name == 'push'
54
+ id : terraform-destroy-dev
55
+ run : cd infra &&
56
+ terraform workspace select dev || terraform workspace new dev &&
57
+ terraform destroy -var-file="./envs/dev/terraform.tfvars" -auto-approve
49
58
50
59
- name : Terraform Plan for Dev
51
- if : github.ref == 'refs/heads/develop' && github.event_name == 'push'
60
+ if : steps.read-destroy-config.outputs.destroy_dev != 'true' && github.ref == 'refs/heads/develop' && github.event_name == 'push'
52
61
id : terraform-plan-dev
53
62
run : cd infra &&
54
63
terraform workspace select dev || terraform workspace new dev &&
55
64
terraform plan -var-file="./envs/dev/terraform.tfvars" -out=dev.plan
56
65
57
66
- name : Terraform Apply for Dev
58
67
id : terraform-apply-dev
59
- if : github.ref == 'refs/heads/develop' && github.event_name == 'push'
68
+ if : steps.read-destroy-config.outputs.destroy_dev != 'true' && github.ref == 'refs/heads/develop' && github.event_name == 'push'
60
69
run : cd infra &&
61
70
terraform workspace select dev || terraform workspace new dev &&
62
71
terraform apply "dev.plan"
63
72
64
- # - name: Terraform Create Prod Workspace
65
- # if: github.ref == 'refs/heads/main' && github.event_name == 'push'
66
- # id: select-prod-workspace
67
- # run: |
68
- # terraform workspace list | grep -q "prod" || terraform workspace new prod
73
+ - name : Terraform Destroy for Prod
74
+ if : steps.read-destroy-config.outputs.destroy_prod == 'true' && github.ref == 'refs/heads/main' && github.event_name == 'push'
75
+ id : terraform-destroy-prod
76
+ run : cd infra &&
77
+ terraform workspace select prod || terraform workspace new prod &&
78
+ terraform destroy -var-file="./envs/prod/terraform.tfvars" -auto-approve
69
79
70
80
- name : Terraform Plan for Prod
71
- if : github.ref == 'refs/heads/main' && github.event_name == 'push'
81
+ if : steps.read-destroy-config.outputs.destroy_prod != 'true' && github.ref == 'refs/heads/main' && github.event_name == 'push'
72
82
id : terraform-plan-prod
73
83
run : cd infra &&
74
84
terraform workspace select prod || terraform workspace new prod &&
75
85
terraform plan -var-file="./envs/prod/terraform.tfvars" -out=prod.plan
76
86
77
87
- name : Terraform Apply for Prod
78
- if : github.ref == 'refs/heads/main' && github.event_name == 'push'
88
+ if : steps.read-destroy-config.outputs.destroy_prod != 'true' && github.ref == 'refs/heads/main' && github.event_name == 'push'
79
89
id : terraform-apply-prod
80
90
run : cd infra &&
81
91
terraform workspace select prod || terraform workspace new prod &&
0 commit comments