Skip to content

Commit 6b14cb1

Browse files
feat: 🚀 Added a feature to pass environment variables in Terraform workflows and TFDrift workflows. (#89)
1 parent b31b178 commit 6b14cb1

File tree

3 files changed

+43
-19
lines changed

3 files changed

+43
-19
lines changed

‎.github/workflows/terraform_workflow.yml‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,21 @@ on:
7676
jobs:
7777
terraform-workflow:
7878
runs-on: ubuntu-latest
79-
env: ${{ fromJSON(secrets.env-vars) }}
8079
outputs:
8180
tfplanExitCode: ${{ steps.tf-plan.outputs.exitcode }}
8281

8382
steps:
8483
- name: Checkout
8584
uses: actions/checkout@v4
8685

86+
- name: Set environment variables
87+
run: |
88+
(
89+
cat <<'_EOT'
90+
${{ secrets.env-vars }}
91+
_EOT
92+
) >> "$GITHUB_ENV"
93+
8794
- name: Install AWS CLI
8895
if: ${{ inputs.provider == 'aws' }}
8996
uses: aws-actions/configure-aws-credentials@v4
@@ -139,7 +146,7 @@ jobs:
139146
id: validate
140147
uses: dflook/terraform-validate@v1
141148
with:
142-
tf_actions_working_dir: ${{ inputs.working_directory }}
149+
path: ${{ inputs.working_directory }}
143150

144151
- name: Terraform Plan
145152
id: tf-plan

‎.github/workflows/tfdrift.yml‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ on:
4949
TF_API_TOKEN:
5050
required: false
5151
description: 'Terraform cloud token if your backend is terraform cloud.'
52+
env-vars:
53+
required: false
54+
description: 'Pass required environment variables'
55+
5256
jobs:
5357
terraform-plan:
5458
name: 'Terraform Plan'
@@ -64,6 +68,14 @@ jobs:
6468
- name: Checkout
6569
uses: actions/checkout@v4
6670

71+
- name: Set environment variables
72+
run: |
73+
(
74+
cat <<'_EOT'
75+
${{ secrets.env-vars }}
76+
_EOT
77+
) >> "$GITHUB_ENV"
78+
6779
- name: Install AWS CLI
6880
if: ${{ inputs.provider == 'aws' }}
6981
uses: aws-actions/configure-aws-credentials@v4
@@ -102,20 +114,12 @@ jobs:
102114
- name: Terraform Plan
103115
id: tf-plan
104116
run: |
105-
export exitcode=0
106117
cd ${{ inputs.working_directory }}
107118
if [ -n "${{ inputs.var_file }}" ]; then
108119
terraform plan -detailed-exitcode -no-color -out tfplan --var-file=${{ inputs.var_file }} || export exitcode=$?
109120
else
110121
terraform plan -detailed-exitcode -no-color -out tfplan || export exitcode=$?
111122
fi
112-
echo "exitcode=$exitcode" >> $GITHUB_OUTPUT
113-
if [ $exitcode -eq 1 ]; then
114-
echo Terraform Plan Failed!
115-
exit 1
116-
else
117-
exit 0
118-
fi
119123
120124
# Save plan to artifacts
121125
- name: Publish Terraform Plan
@@ -149,7 +153,7 @@ jobs:
149153
echo "$SUMMARY" >> $GITHUB_STEP_SUMMARY
150154
151155
# If changes are detected, create a new issue
152-
- name: Publish Drift Report
156+
- name: Publish Drift Report and create new issue
153157
if: steps.tf-plan.outputs.exitcode == 2
154158
uses: actions/github-script@v6
155159
env:
@@ -161,18 +165,20 @@ jobs:
161165
const title = 'Terraform Configuration Drift Detected';
162166
const creator = 'github-actions[bot]'
163167
164-
# Look to see if there is an existing drift issue
168+
// Look to see if there is an existing drift issue
165169
const issues = await github.rest.issues.listForRepo({
166170
owner: context.repo.owner,
167171
repo: context.repo.repo,
168172
state: 'open',
169173
creator: creator,
170174
title: title
171175
})
176+
172177
if( issues.data.length > 0 ) {
173178
// We assume there shouldn't be more than 1 open issue, since we update any issue we find
174179
const issue = issues.data[0]
175-
if ( issue.body == body ) {
180+
181+
if ( issue.body == body ) {
176182
console.log('Drift Detected: Found matching issue with duplicate content')
177183
} else {
178184
console.log('Drift Detected: Found matching issue, updating body')
@@ -185,6 +191,7 @@ jobs:
185191
}
186192
} else {
187193
console.log('Drift Detected: Creating new issue')
194+
188195
github.rest.issues.create({
189196
owner: context.repo.owner,
190197
repo: context.repo.repo,
@@ -203,16 +210,18 @@ jobs:
203210
const title = 'Terraform Configuration Drift Detected';
204211
const creator = 'github-actions[bot]'
205212
206-
# Look to see if there is an existing drift issue
213+
// Look to see if there is an existing drift issue
207214
const issues = await github.rest.issues.listForRepo({
208215
owner: context.repo.owner,
209216
repo: context.repo.repo,
210217
state: 'open',
211218
creator: creator,
212219
title: title
213220
})
214-
if( issues.data.length > 0 ){
221+
222+
if( issues.data.length > 0 ) {
215223
const issue = issues.data[0]
224+
216225
github.rest.issues.update({
217226
owner: context.repo.owner,
218227
repo: context.repo.repo,

‎docs/terraform_workflow.md‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ jobs:
3030
AWS_ACCESS_KEY_ID: # Specify AWS Access key ID
3131
AWS_SECRET_ACCESS_KEY: # Specify AWS Secret Access key ID
3232
AWS_SESSION_TOKEN: # Specify Session ID
33-
env-vars: # Specify env variables. ex. '{"KEY1":"VALUE1", "KEY2":" ${{ secrets.VALUE2 }}"}'
33+
env-vars: | # Specify env variables in following format
34+
key1=value1
35+
key2=value2
3436
3537
```
3638
@@ -56,7 +58,9 @@ jobs:
5658
destroy: # If the value is set to true, the workflow proceeds to the destroy step. However, the default value is false
5759
secrets:
5860
AZURE_CREDENTIALS: # Specify Azure credentilas
59-
env-vars: # Specify env variables. ex. '{"KEY1":"VALUE1", "KEY2":" ${{ secrets.VALUE2 }}"}'
61+
env-vars: | # Specify env variables in following format
62+
key1=value1
63+
key2=value2
6064
```
6165
6266
#### Example of a Terraform workflow for a Digitalocean cloud provider
@@ -81,7 +85,9 @@ jobs:
8185
destroy: # If the value is set to true, the workflow proceeds to the destroy step. However, the default value is false
8286
secrets:
8387
DIGITALOCEAN_ACCESS_TOKEN: # Digitalocean token
84-
env-vars: # Specify env variables. ex. '{"KEY1":"VALUE1", "KEY2":" ${{ secrets.VALUE2 }}"}'
88+
env-vars: | # Specify env variables in following format
89+
key1=value1
90+
key2=value2
8591
```
8692
8793
#### Example of a Terraform workflow for a GCP cloud provider
@@ -106,5 +112,7 @@ jobs:
106112
destroy: # If the value is set to true, the workflow proceeds to the destroy step. However, the default value is false
107113
secrets:
108114
GCP_SA_KEY: # GCP service account Secret access key
109-
env-vars: # Specify env variables. ex. '{"KEY1":"VALUE1", "KEY2":" ${{ secrets.VALUE2 }}"}'
115+
env-vars: | # Specify env variables in following format
116+
key1=value1
117+
key2=value2
110118
```

0 commit comments

Comments
 (0)