Skip to content

Commit 65b027c

Browse files
committed
Add variables and var_files support for remote operations in terraform-check
1 parent e0300f8 commit 65b027c

File tree

8 files changed

+155
-121
lines changed

8 files changed

+155
-121
lines changed

.github/workflows/test-remote.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,42 @@ jobs:
6161
exit 1
6262
fi
6363
64+
- name: Check no changes
65+
uses: ./terraform-check
66+
with:
67+
path: tests/terraform-cloud
68+
workspace: ${{ github.head_ref }}-1
69+
backend_config: "token=${{ secrets.TF_API_TOKEN }}"
70+
var_file: |
71+
tests/terraform-cloud/my_variable.tfvars
72+
variables: |
73+
from_variables="from_variables"
74+
75+
- name: Check changes
76+
uses: ./terraform-check
77+
id: check
78+
continue-on-error: true
79+
with:
80+
path: tests/terraform-cloud
81+
workspace: ${{ github.head_ref }}-1
82+
backend_config: "token=${{ secrets.TF_API_TOKEN }}"
83+
var_file: |
84+
tests/terraform-cloud/my_variable.tfvars
85+
variables: |
86+
from_variables="Changed!"
87+
88+
- name: Verify changes detected
89+
run: |
90+
if [[ "${{ steps.check.outcome }}" != "failure" ]]; then
91+
echo "Check didn't fail correctly"
92+
exit 1
93+
fi
94+
95+
if [[ "${{ steps.check.outputs.failure-reason }}" != "changes-to-apply" ]]; then
96+
echo "failure-reason not set correctly"
97+
exit 1
98+
fi
99+
64100
- name: Destroy workspace
65101
uses: ./terraform-destroy-workspace
66102
with:

image/actions.sh

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,6 @@ function set-remote-plan-args() {
221221
PLAN_ARGS="$PLAN_ARGS -parallelism=$INPUT_PARALLELISM"
222222
fi
223223

224-
set -x
225-
226224
local AUTO_TFVARS_COUNTER=0
227225

228226
if [[ -n "$INPUT_VAR_FILE" ]]; then
@@ -271,6 +269,28 @@ function write_credentials() {
271269
debug_cmd git config --list
272270
}
273271

272+
function plan() {
273+
274+
local PLAN_OUT_ARG
275+
if [[ -n "$PLAN_OUT" ]]; then
276+
PLAN_OUT_ARG="-out=$PLAN_OUT"
277+
else
278+
PLAN_OUT_ARG=""
279+
fi
280+
281+
set +e
282+
# shellcheck disable=SC2086
283+
(cd "$INPUT_PATH" && terraform plan -input=false -no-color -detailed-exitcode -lock-timeout=300s $PLAN_OUT_ARG $PLAN_ARGS) \
284+
2>"$STEP_TMP_DIR/terraform_plan.stderr" \
285+
| $TFMASK \
286+
| tee /dev/fd/3 \
287+
| compact_plan \
288+
>"$STEP_TMP_DIR/plan.txt"
289+
290+
PLAN_EXIT=${PIPESTATUS[0]}
291+
set -e
292+
}
293+
274294
# Every file written to disk should use one of these directories
275295
readonly STEP_TMP_DIR="/tmp"
276296
readonly JOB_TMP_DIR="$HOME/.dflook-terraform-github-actions"

image/entrypoints/apply.sh

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,6 @@ fi
2323

2424
exec 3>&1
2525

26-
function plan() {
27-
28-
local PLAN_OUT_ARG
29-
if [[ -n "$PLAN_OUT" ]]; then
30-
PLAN_OUT_ARG="-out=$PLAN_OUT"
31-
else
32-
PLAN_OUT_ARG=""
33-
fi
34-
35-
set +e
36-
# shellcheck disable=SC2086
37-
(cd "$INPUT_PATH" && terraform plan -input=false -no-color -detailed-exitcode -lock-timeout=300s $PLAN_OUT_ARG $PLAN_ARGS) \
38-
2>"$STEP_TMP_DIR/terraform_plan.stderr" \
39-
| $TFMASK \
40-
| tee /dev/fd/3 \
41-
| compact_plan \
42-
>"$STEP_TMP_DIR/plan.txt"
43-
44-
PLAN_EXIT=${PIPESTATUS[0]}
45-
set -e
46-
}
47-
4826
function apply() {
4927

5028
set +e

image/entrypoints/check.sh

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,27 @@ init-backend
99
select-workspace
1010
set-plan-args
1111

12-
set +e
13-
# shellcheck disable=SC2086
14-
(cd "$INPUT_PATH" && terraform plan -input=false -detailed-exitcode -lock-timeout=300s $PLAN_ARGS) \
15-
| $TFMASK
12+
PLAN_OUT="$STEP_TMP_DIR/plan.out"
1613

17-
readonly TF_EXIT=${PIPESTATUS[0]}
18-
set -e
14+
exec 3>&1
1915

20-
if [[ $TF_EXIT -eq 1 ]]; then
16+
plan
17+
18+
if [[ $PLAN_EXIT -eq 1 ]]; then
19+
if grep -q "Saving a generated plan is currently not supported" "$STEP_TMP_DIR/terraform_plan.stderr"; then
20+
# This terraform module is using the remote backend, which is deficient.
21+
set-remote-plan-args
22+
PLAN_OUT=""
23+
plan
24+
find "$INPUT_PATH" -regex '.*/zzzz-dflook-terraform-github-actions-[0-9]+\.auto\.tfvars' -delete
25+
fi
26+
fi
27+
28+
if [[ $PLAN_EXIT -eq 1 ]]; then
2129
echo "Error running terraform"
2230
exit 1
2331

24-
elif [[ $TF_EXIT -eq 2 ]]; then
32+
elif [[ $PLAN_EXIT -eq 2 ]]; then
2533

2634
echo "Changes detected!"
2735
set_output failure-reason changes-to-apply

image/entrypoints/plan.sh

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,6 @@ PLAN_OUT="$STEP_TMP_DIR/plan.out"
1313

1414
exec 3>&1
1515

16-
function plan() {
17-
18-
local PLAN_OUT_ARG
19-
if [[ -n "$PLAN_OUT" ]]; then
20-
PLAN_OUT_ARG="-out=$PLAN_OUT"
21-
else
22-
PLAN_OUT_ARG=""
23-
fi
24-
25-
set +e
26-
# shellcheck disable=SC2086
27-
(cd "$INPUT_PATH" && terraform plan -input=false -no-color -detailed-exitcode -lock-timeout=300s $PLAN_OUT_ARG $PLAN_ARGS) \
28-
2>"$STEP_TMP_DIR/terraform_plan.stderr" \
29-
| $TFMASK \
30-
| tee /dev/fd/3 \
31-
| compact_plan \
32-
>"$STEP_TMP_DIR/plan.txt"
33-
34-
PLAN_EXIT=${PIPESTATUS[0]}
35-
set -e
36-
}
37-
3816
### Generate a plan
3917

4018
plan

terraform-apply/README.md

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -74,35 +74,7 @@ These input values must be the same as any `terraform-plan` for the same configu
7474
```
7575
7676
Variables set here override any given in `var_file`s.
77-
78-
- Type: string
79-
- Optional
80-
81-
* ~~`var`~~
82-
83-
> :warning: **Deprecated**: Use the `variables` input instead.
84-
85-
Comma separated list of terraform vars to set.
86-
87-
This is deprecated due to the following limitations:
88-
- Only primitive types can be set with `var` - number, bool and string.
89-
- String values may not contain a comma.
90-
- Values set with `var` will be overridden by values contained in `var_file`s
91-
92-
You can change from `var` to `variables` by putting each variable on a separate line and ensuring each string value is quoted.
93-
94-
For example:
95-
```yaml
96-
with:
97-
var: instance_type=m5.xlarge,nat_type=instance
98-
```
99-
Becomes:
100-
```yaml
101-
with:
102-
variables: |
103-
instance_type="m5.xlarge"
104-
nat_type="instance"
105-
```
77+
This **can** be used with remote backends such as Terraform Cloud/Enterprise, with variables set in the remote workspace having precedence.
10678

10779
- Type: string
10880
- Optional
@@ -119,6 +91,8 @@ These input values must be the same as any `terraform-plan` for the same configu
11991
prod.tfvars
12092
```
12193

94+
This **can** be used with remote backends such as Terraform Cloud/Enterprise, with variables set in the remote workspace having precedence.
95+
12296
- Type: string
12397
- Optional
12498

@@ -181,6 +155,36 @@ These input values must be the same as any `terraform-plan` for the same configu
181155
- Optional
182156
- Default: false
183157

158+
* ~~`var`~~
159+
160+
> :warning: **Deprecated**: Use the `variables` input instead.
161+
162+
Comma separated list of terraform vars to set.
163+
164+
This is deprecated due to the following limitations:
165+
- Only primitive types can be set with `var` - number, bool and string.
166+
- String values may not contain a comma.
167+
- Values set with `var` will be overridden by values contained in `var_file`s
168+
- Does not work with the `remote` backend
169+
170+
You can change from `var` to `variables` by putting each variable on a separate line and ensuring each string value is quoted.
171+
172+
For example:
173+
```yaml
174+
with:
175+
var: instance_type=m5.xlarge,nat_type=instance
176+
```
177+
Becomes:
178+
```yaml
179+
with:
180+
variables: |
181+
instance_type="m5.xlarge"
182+
nat_type="instance"
183+
```
184+
185+
- Type: string
186+
- Optional
187+
184188
## Outputs
185189

186190
* Terraform Outputs

terraform-check/README.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,7 @@ This is intended to run on a schedule to notify if manual changes to your infras
3939
```
4040
4141
Variables set here override any given in `var_file`s.
42-
43-
- Type: string
44-
- Optional
45-
46-
* ~~`var`~~
47-
48-
> :warning: **Deprecated**: Use the `variables` input instead.
49-
50-
Comma separated list of terraform vars to set
42+
This **can** be used with remote backends such as Terraform Cloud/Enterprise, with variables set in the remote workspace having precedence.
5143

5244
- Type: string
5345
- Optional
@@ -64,6 +56,11 @@ This is intended to run on a schedule to notify if manual changes to your infras
6456
prod.tfvars
6557
```
6658

59+
This **can** be used with remote backends such as Terraform Cloud/Enterprise, with variables set in the remote workspace having precedence.
60+
61+
- Type: string
62+
- Optional
63+
6764
* `backend_config`
6865

6966
List of terraform backend config values, one per line.
@@ -97,6 +94,15 @@ This is intended to run on a schedule to notify if manual changes to your infras
9794
- Optional
9895
- Default: 10
9996

97+
* ~~`var`~~
98+
99+
> :warning: **Deprecated**: Use the `variables` input instead.
100+
101+
Comma separated list of terraform vars to set
102+
103+
- Type: string
104+
- Optional
105+
100106
## Outputs
101107

102108
* `failure-reason`

terraform-plan/README.md

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -57,41 +57,13 @@ The [dflook/terraform-apply](https://github.com/dflook/terraform-github-actions/
5757
```
5858
5959
Variables set here override any given in `var_file`s.
60+
This **can** be used with remote backends such as Terraform Cloud/Enterprise, with variables set in the remote workspace having precedence.
6061

6162
> :warning: Secret values are not masked in the PR comment. Set a `label` to avoid revealing the variables in the PR.
6263

6364
- Type: string
6465
- Optional
6566

66-
* ~~`var`~~
67-
68-
> :warning: **Deprecated**: Use the `variables` input instead.
69-
70-
Comma separated list of terraform vars to set.
71-
72-
This is deprecated due to the following limitations:
73-
- Only primitive types can be set with `var` - number, bool and string.
74-
- String values may not contain a comma.
75-
- Values set with `var` will be overridden by values contained in `var_file`s
76-
77-
You can change from `var` to `variables` by putting each variable on a separate line and ensuring each string value is quoted.
78-
79-
For example:
80-
```yaml
81-
with:
82-
var: instance_type=m5.xlarge,nat_type=instance
83-
```
84-
Becomes:
85-
```yaml
86-
with:
87-
variables: |
88-
instance_type="m5.xlarge"
89-
nat_type="instance"
90-
```
91-
92-
- Type: string
93-
- Optional
94-
9567
* `var_file`
9668

9769
List of tfvars files to use, one per line.
@@ -104,6 +76,8 @@ The [dflook/terraform-apply](https://github.com/dflook/terraform-github-actions/
10476
prod.tfvars
10577
```
10678

79+
This **can** be used with remote backends such as Terraform Cloud/Enterprise, with variables set in the remote workspace having precedence.
80+
10781
- Type: string
10882
- Optional
10983

@@ -150,6 +124,36 @@ The [dflook/terraform-apply](https://github.com/dflook/terraform-github-actions/
150124
- Optional
151125
- Default: true
152126

127+
* ~~`var`~~
128+
129+
> :warning: **Deprecated**: Use the `variables` input instead.
130+
131+
Comma separated list of terraform vars to set.
132+
133+
This is deprecated due to the following limitations:
134+
- Only primitive types can be set with `var` - number, bool and string.
135+
- String values may not contain a comma.
136+
- Values set with `var` will be overridden by values contained in `var_file`s
137+
- Does not work with the `remote` backend
138+
139+
You can change from `var` to `variables` by putting each variable on a separate line and ensuring each string value is quoted.
140+
141+
For example:
142+
```yaml
143+
with:
144+
var: instance_type=m5.xlarge,nat_type=instance
145+
```
146+
Becomes:
147+
```yaml
148+
with:
149+
variables: |
150+
instance_type="m5.xlarge"
151+
nat_type="instance"
152+
```
153+
154+
- Type: string
155+
- Optional
156+
153157
## Environment Variables
154158

155159
* `GITHUB_TOKEN`

0 commit comments

Comments
 (0)