1- name : Terraform Apply
2- on :
3- push :
4- branches :
5- - main
6-
7- jobs :
8- Terraform-Plan :
9- runs-on : ubuntu-latest
10- environment : pre-prod
11- outputs :
12- plan_exitcode : ${{ steps.plan.outputs.exitcode }}
13- tag : ${{ steps.bump.outputs.new_tag }}
14- env :
15- ARM_CLIENT_ID : ${{ secrets.ARM_CLIENT_ID }}
16- ARM_CLIENT_SECRET : ${{ secrets.ARM_CLIENT_SECRET }}
17- ARM_TENANT_ID : ${{ secrets.ARM_TENANT_ID }}
18- ARM_SUBSCRIPTION_ID : ${{ secrets.ARM_SUBSCRIPTION_ID }}
19- steps :
20- - name : Checkout code
21- uses : actions/checkout@v4
22-
23- - name : Bump version and push tag
24- id : bump
25- uses : anothrNick/github-tag-action@1.73.0
26- env :
27- GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
28- WITH_V : true
29-
30- - name : Versão criada
31- run : |
32- echo "Tag criada: ${{ steps.bump.outputs.new_tag }}"
33- # echo "version_tag=${{ steps.bump.outputs.new_tag }}" >> $GITHUB_OUTPUT
34-
35- - name : ls files
36- run : ls -ltr
37-
38- - name : Install Terraform CLI
39- uses : hashicorp/setup-terraform@v3
40- with :
41- terraform_version : " 1.12.2"
42-
43- - name : Terraform Init
44- run : terraform init
45-
46- - name : Terraform Validate
47- run : terraform validate
48-
49- - name : Terraform Plan
50- id : plan
51- run : terraform plan -detailed-exitcode -out=state_file-${{ steps.bump.outputs.new_tag }} || export exitcode=$? >> $GITHUB_OUTPUT
52- continue-on-error : true # Allow the step to continue even if -detailed-exitcode returns 2
53-
54- - name : get value of terraform plan
55- run : |
56- echo "The value is: ${{ steps.plan.outputs.exitcode }}"
57-
58- - name : Check Plan Exit Code
59- id : check_plan
60- if : steps.plan.outcome == 'success' && steps.plan.outputs.exitcode == '2'
61- run : |
62- echo "Terraform plan detected changes. Proceeding with apply (or manual approval)."
63- # Add logic for manual approval or direct apply here
64-
65- - name : No Changes Detected
66- if : steps.plan.outcome == 'success' && steps.plan.outputs.exitcode == '0'
67- run : |
68- echo "No changes detected by Terraform plan. Skipping apply."
69-
70- - name : Terraform Plan Failed
71- if : steps.plan.outcome == 'failure' || steps.plan.outputs.exitcode == '1'
72- run : |
73- echo "Terraform plan failed. Review the logs for errors."
74- exit 1 # Fail the workflow
75-
76- - name : Send plan via email
77- if : steps.plan.outcome == 'success' && steps.plan.outputs.exitcode == '2'
78- uses : dawidd6/action-send-mail@v6
79- with :
80- server_address : smtp.gmail.com
81- server_port : 587
82- username : ${{ secrets.SMTP_USER }}
83- password : ${{ secrets.SMTP_PASS }}
84- subject : " O ${{ github.repository }} Inforrrrrma"
85- to : ${{ secrets.SMTP_USER }}
86- from : " Terraform <${{ secrets.SMTP_USER }}>"
87- body : |
88- Foram detectadas alterações a serem feitas em sua infraestrutura.
89-
90- Aprove ou rejeite aqui:
91- https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
92-
93- - name : Remove unnecessary folders
94- run : |
95- rm -rf some_examples
96- rm -rf .github
97- rm -rf .gitignore
98- rm -rf .git
99-
100- - name : ls files
101- run : ls -ltra
102-
103- - name : Upload artifacts
104- if : steps.plan.outcome == 'success' && steps.plan.outputs.exitcode == '2'
105-
106- uses : actions/upload-artifact@v4
107- with :
108- name : ${{ steps.bump.outputs.new_tag }}
109- path : .
110-
111- Terraform-Apply :
112- runs-on : ubuntu-latest
113- needs : Terraform-Plan
114- environment : pre-prod-apply
115- if : needs.Terraform-Plan.outputs.plan_exitcode == '2'
116- env :
117- ARM_CLIENT_ID : ${{ secrets.ARM_CLIENT_ID }}
118- ARM_CLIENT_SECRET : ${{ secrets.ARM_CLIENT_SECRET }}
119- ARM_TENANT_ID : ${{ secrets.ARM_TENANT_ID }}
120- ARM_SUBSCRIPTION_ID : ${{ secrets.ARM_SUBSCRIPTION_ID }}
121- steps :
122- - name : Get build artifacts
123- uses : actions/download-artifact@v4
124- with :
125- name : ${{ needs.Terraform-Plan.outputs.tag }}
126- path : terraform_plan_artifacts
127-
128- - name : Install Terraform CLI
129- uses : hashicorp/setup-terraform@v3
130- with :
131- terraform_version : " 1.12.2"
132-
133- - name : Terraform Init
134- run : terraform init
135- working-directory : terraform_plan_artifacts
136-
137- - name : Terraform Apply
138- id : apply
139- working-directory : terraform_plan_artifacts
140- run : |
1+ name : Terraform Apply
2+ on :
3+ push :
4+ branches :
5+ - main
6+
7+ jobs :
8+ Terraform-Plan :
9+ runs-on : ubuntu-latest
10+ environment : pre-prod
11+ outputs :
12+ plan_exitcode : ${{ steps.plan.outputs.exitcode }}
13+ tag : ${{ steps.bump.outputs.new_tag }}
14+ env :
15+ ARM_CLIENT_ID : ${{ secrets.ARM_CLIENT_ID }}
16+ ARM_CLIENT_SECRET : ${{ secrets.ARM_CLIENT_SECRET }}
17+ ARM_TENANT_ID : ${{ secrets.ARM_TENANT_ID }}
18+ ARM_SUBSCRIPTION_ID : ${{ secrets.ARM_SUBSCRIPTION_ID }}
19+ steps :
20+ - name : Checkout code
21+ uses : actions/checkout@v4
22+
23+ - name : Bump version and push tag
24+ id : bump
25+ uses : anothrNick/github-tag-action@1.73.0
26+ env :
27+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
28+ WITH_V : true
29+
30+ - name : Versão criada
31+ run : |
32+ echo "Tag criada: ${{ steps.bump.outputs.new_tag }}"
33+ # echo "version_tag=${{ steps.bump.outputs.new_tag }}" >> $GITHUB_OUTPUT
34+
35+ - name : ls files
36+ run : ls -ltr
37+
38+ - name : Install Terraform CLI
39+ uses : hashicorp/setup-terraform@v3
40+ with :
41+ terraform_version : " 1.12.2"
42+
43+ - name : Terraform Init
44+ run : terraform init
45+
46+ - name : Terraform Validate
47+ run : terraform validate
48+
49+ - name : Terraform Plan
50+ id : plan
51+ run : terraform plan -detailed-exitcode -out=state_file-${{ steps.bump.outputs.new_tag }} || export exitcode=$? >> $GITHUB_OUTPUT
52+ continue-on-error : true # Allow the step to continue even if -detailed-exitcode returns 2
53+
54+ - name : get value of terraform plan
55+ run : |
56+ echo "The value is: ${{ steps.plan.outputs.exitcode }}"
57+
58+ - name : Check Plan Exit Code
59+ id : check_plan
60+ if : steps.plan.outcome == 'success' && steps.plan.outputs.exitcode == '2'
61+ run : |
62+ echo "Terraform plan detected changes. Proceeding with apply (or manual approval)."
63+ # Add logic for manual approval or direct apply here
64+
65+ - name : No Changes Detected
66+ if : steps.plan.outcome == 'success' && steps.plan.outputs.exitcode == '0'
67+ run : |
68+ echo "No changes detected by Terraform plan. Skipping apply."
69+
70+ - name : Terraform Plan Failed
71+ if : steps.plan.outcome == 'failure' || steps.plan.outputs.exitcode == '1'
72+ run : |
73+ echo "Terraform plan failed. Review the logs for errors."
74+ exit 1 # Fail the workflow
75+
76+ - name : Send plan via email
77+ if : steps.plan.outcome == 'success' && steps.plan.outputs.exitcode == '2'
78+ uses : dawidd6/action-send-mail@v6
79+ with :
80+ server_address : smtp.gmail.com
81+ server_port : 587
82+ username : ${{ secrets.SMTP_USER }}
83+ password : ${{ secrets.SMTP_PASS }}
84+ subject : " O ${{ github.repository }} Inforrrrrma"
85+ to : ${{ secrets.SMTP_USER }}
86+ from : " Terraform <${{ secrets.SMTP_USER }}>"
87+ body : |
88+ Foram detectadas alterações a serem feitas em sua infraestrutura.
89+
90+ Aprove ou rejeite aqui:
91+ https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
92+
93+ - name : Remove unnecessary folders
94+ run : |
95+ rm -rf some_examples
96+ rm -rf .github
97+ rm -rf .gitignore
98+ rm -rf .git
99+
100+ - name : ls files
101+ run : ls -ltra
102+
103+ - name : Upload artifacts
104+ if : steps.plan.outcome == 'success' && steps.plan.outputs.exitcode == '2'
105+
106+ uses : actions/upload-artifact@v4
107+ with :
108+ name : ${{ steps.bump.outputs.new_tag }}
109+ path : .
110+
111+ Terraform-Apply :
112+ runs-on : ubuntu-latest
113+ needs : Terraform-Plan
114+ environment : pre-prod-apply
115+ if : needs.Terraform-Plan.outputs.plan_exitcode == '2'
116+ env :
117+ ARM_CLIENT_ID : ${{ secrets.ARM_CLIENT_ID }}
118+ ARM_CLIENT_SECRET : ${{ secrets.ARM_CLIENT_SECRET }}
119+ ARM_TENANT_ID : ${{ secrets.ARM_TENANT_ID }}
120+ ARM_SUBSCRIPTION_ID : ${{ secrets.ARM_SUBSCRIPTION_ID }}
121+ steps :
122+ - name : Get build artifacts
123+ uses : actions/download-artifact@v4
124+ with :
125+ name : ${{ needs.Terraform-Plan.outputs.tag }}
126+ path : terraform_plan_artifacts
127+
128+ - name : Install Terraform CLI
129+ uses : hashicorp/setup-terraform@v3
130+ with :
131+ terraform_version : " 1.12.2"
132+
133+ - name : Terraform Init
134+ run : terraform init
135+ working-directory : terraform_plan_artifacts
136+
137+ - name : Terraform Apply
138+ id : apply
139+ working-directory : terraform_plan_artifacts
140+ run : |
141141 terraform apply -auto-approve state_file-${{ needs.Terraform-Plan.outputs.tag }}
0 commit comments