Skip to content

Commit 955d828

Browse files
committed
Code Update
1 parent 1d3a8df commit 955d828

File tree

1 file changed

+235
-0
lines changed

1 file changed

+235
-0
lines changed
Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
name: terraform-multi
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
environment:
7+
required: true
8+
type: string
9+
tfvars_file:
10+
required: true
11+
type: string
12+
rgname:
13+
required: true
14+
type: string
15+
saname:
16+
required: true
17+
type: string
18+
scname:
19+
required: true
20+
type: string
21+
key:
22+
required: true
23+
type: string
24+
runInit:
25+
type: boolean
26+
default: false
27+
runFmt:
28+
type: boolean
29+
default: false
30+
runValidate:
31+
type: boolean
32+
default: false
33+
runPlan:
34+
type: boolean
35+
default: false
36+
runApply:
37+
type: boolean
38+
default: false
39+
runDestroy:
40+
type: boolean
41+
default: false
42+
secrets:
43+
AZURE_CLIENT_ID:
44+
required: true
45+
AZURE_TENANT_ID:
46+
required: true
47+
AZURE_SUBSCRIPTION_ID:
48+
required: true
49+
50+
permissions:
51+
id-token: write
52+
contents: read
53+
54+
jobs:
55+
init:
56+
if: ${{ inputs.runInit }}
57+
runs-on: self-hosted
58+
# environment: ${{ inputs.environment }}
59+
defaults:
60+
run:
61+
working-directory: infra
62+
steps:
63+
- name: Checkout
64+
uses: actions/checkout@v5.0.0
65+
66+
- name: Azure Login
67+
uses: Azure/login@v2.3.0
68+
with:
69+
client-id: ${{ secrets.AZURE_CLIENT_ID }}
70+
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
71+
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
72+
73+
74+
# - name: Setup Terraform
75+
# uses: hashicorp/setup-terraform@v3
76+
# with:
77+
# terraform_version: 1.6.6
78+
79+
80+
- name: Terraform Init (remote backend)
81+
run: terraform init -input=false -backend-config="resource_group_name=${{ inputs.rgname }}" -backend-config="storage_account_name=${{ inputs.saname }}" -backend-config="container_name=${{ inputs.scname }}" -backend-config="key=${{ inputs.key }}"
82+
83+
84+
# - name: Upload providers dir
85+
# uses: actions/upload-artifact@v4
86+
# with:
87+
# name: tf-providers
88+
# path: infra/.terraform/*
89+
90+
# - name: Upload lockfile
91+
# uses: actions/upload-artifact@v4
92+
# with:
93+
# name: tf-lockfile
94+
# path: infra/.terraform.lock.hcl
95+
96+
fmt:
97+
needs: [init]
98+
if: ${{ always() && inputs.runFmt && needs.init.result == 'success' }}
99+
runs-on: self-hosted
100+
defaults:
101+
run:
102+
working-directory: infra
103+
steps:
104+
- name: Terraform fmt
105+
run: terraform fmt
106+
107+
validate:
108+
needs: [fmt, init]
109+
if: ${{ always()
110+
&& inputs.runValidate
111+
&& (
112+
( inputs.runFmt && needs.fmt.result == 'success' ) ||
113+
( !inputs.runFmt && needs.init.result == 'success' )
114+
)
115+
}}
116+
runs-on: self-hosted
117+
defaults:
118+
run:
119+
working-directory: infra
120+
steps:
121+
- name: Terraform validate
122+
run: terraform validate
123+
124+
plan:
125+
needs: [validate, fmt, init]
126+
if: ${{ always()
127+
&& inputs.runPlan
128+
&& (
129+
( inputs.runValidate && needs.validate.result == 'success' ) ||
130+
( !inputs.runValidate && inputs.runFmt && needs.fmt.result == 'success' ) ||
131+
( !inputs.runValidate && !inputs.runFmt && needs.init.result == 'success' )
132+
)
133+
}}
134+
runs-on: self-hosted
135+
# environment: ${{ inputs.environment }}
136+
defaults:
137+
run:
138+
working-directory: infra
139+
steps:
140+
- name: Azure Login (OIDC)
141+
uses: azure/login@v2
142+
with:
143+
client-id: ${{ secrets.AZURE_CLIENT_ID }}
144+
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
145+
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
146+
147+
- name: Terraform plan
148+
run: terraform plan -var-file="../${{ inputs.tfvars_file }}" -out="plan-${{ inputs.environment }}.tfplan"
149+
150+
- name: Upload plan
151+
uses: actions/upload-artifact@v4
152+
with:
153+
name: tf-plan-${{ inputs.environment }}
154+
path: infra/plan-${{ inputs.environment }}.tfplan
155+
if-no-files-found: error
156+
157+
158+
apply:
159+
needs: [plan, validate, fmt, init]
160+
if: ${{ always()
161+
&& inputs.runApply
162+
&& (
163+
( inputs.runPlan && needs.plan.result == 'success' ) ||
164+
( !inputs.runPlan && inputs.runValidate && needs.validate.result == 'success' ) ||
165+
( !inputs.runPlan && !inputs.runValidate && inputs.runFmt && needs.fmt.result == 'success' ) ||
166+
( !inputs.runPlan && !inputs.runValidate && !inputs.runFmt && needs.init.result == 'success' )
167+
)
168+
}}
169+
runs-on: self-hosted
170+
environment: ${{ inputs.environment }}
171+
defaults:
172+
run:
173+
working-directory: infra
174+
steps:
175+
- name: Checkout
176+
uses: actions/checkout@v4
177+
178+
179+
- name: Azure Login (OIDC)
180+
uses: azure/login@v2
181+
with:
182+
client-id: ${{ secrets.AZURE_CLIENT_ID }}
183+
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
184+
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
185+
186+
187+
- name: Terraform Init (remote backend)
188+
run: terraform init -input=false -backend-config="resource_group_name=${{ inputs.rgname }}" -backend-config="storage_account_name=${{ inputs.saname }}" -backend-config="container_name=${{ inputs.scname }}" -backend-config="key=${{ inputs.key }}"
189+
190+
191+
- name: Download plan
192+
uses: actions/download-artifact@v4
193+
with:
194+
name: tf-plan-${{ inputs.environment }}
195+
path: infra
196+
197+
198+
- name: Terraform apply
199+
run: terraform apply -auto-approve "plan-${{ inputs.environment }}.tfplan"
200+
201+
202+
destroy:
203+
needs: [apply, plan, validate, fmt, init]
204+
if: ${{ always()
205+
&& inputs.runDestroy
206+
&& (
207+
( inputs.runApply && needs.apply.result == 'success' ) ||
208+
( !inputs.runApply && inputs.runPlan && needs.plan.result == 'success' ) ||
209+
( !inputs.runApply && !inputs.runPlan && inputs.runValidate && needs.validate.result == 'success' ) ||
210+
( !inputs.runApply && !inputs.runPlan && !inputs.runValidate && inputs.runFmt && needs.fmt.result == 'success' ) ||
211+
( !inputs.runApply && !inputs.runPlan && !inputs.runValidate && !inputs.runFmt && needs.init.result == 'success' )
212+
)
213+
}}
214+
runs-on: self-hosted
215+
environment: ${{ inputs.environment }}
216+
defaults:
217+
run:
218+
working-directory: infra
219+
steps:
220+
- uses: actions/checkout@v4
221+
222+
- name: Azure Login (OIDC)
223+
uses: azure/login@v2
224+
with:
225+
client-id: ${{ secrets.AZURE_CLIENT_ID }}
226+
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
227+
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
228+
229+
- name: Terraform Init (remote backend)
230+
run: terraform init -input=false -backend-config="resource_group_name=${{ inputs.rgname }}" -backend-config="storage_account_name=${{ inputs.saname }}" -backend-config="container_name=${{ inputs.scname }}" -backend-config="key=${{ inputs.key }}"
231+
232+
233+
234+
- name: Terraform destroy
235+
run: terraform destroy -auto-approve -var-file="../${{ inputs.tfvars_file }}"

0 commit comments

Comments
 (0)