Skip to content

Commit 818ca16

Browse files
committed
Use early variables for additional commands
Use early variables for: - tofu workspace - tofu show - tofu validate - tofu init, even when the backend is not needed
1 parent 25e6930 commit 818ca16

File tree

10 files changed

+92
-38
lines changed

10 files changed

+92
-38
lines changed

.github/workflows/test-early-eval.yaml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ jobs:
1010
s3-backend:
1111
runs-on: ubuntu-24.04
1212
name: Plan with early eval
13+
permissions:
14+
contents: read
15+
pull-requests: write
1316
env:
1417
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
1518
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
19+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1620
steps:
1721
- name: Checkout
1822
uses: actions/checkout@v4
@@ -21,9 +25,25 @@ jobs:
2125

2226
- name: tofu plan
2327
uses: ./tofu-plan
28+
id: plan
29+
with:
30+
path: tests/workflows/test-early-eval/s3
31+
variables: |
32+
passphrase = "tofuqwertyuiopasdfgh"
33+
34+
- name: Verify outputs
35+
env:
36+
JSON_PLAN_PATH: ${{ steps.plan.outputs.json_plan_path }}
37+
run: |
38+
if [[ ! -f "$JSON_PLAN_PATH" ]]; then
39+
echo "::error:: json_plan_path not set correctly"
40+
exit 1
41+
fi
42+
43+
- name: tofu apply
44+
uses: ./tofu-apply
2445
with:
2546
path: tests/workflows/test-early-eval/s3
26-
add_github_comment: false
2747
variables: |
2848
passphrase = "tofuqwertyuiopasdfgh"
2949

docs-gen/actions/fmt.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import dataclasses
22

3-
from action import Action
3+
from action import Action, OpenTofu
44
from environment_variables.GITHUB_DOT_COM_TOKEN import GITHUB_DOT_COM_TOKEN
55
from environment_variables.TERRAFORM_CLOUD_TOKENS import TERRAFORM_CLOUD_TOKENS
66
from inputs.backend_config import backend_config
77
from inputs.backend_config_file import backend_config_file
88
from inputs.path import path
9+
from inputs.var_file import var_file
10+
from inputs.variables import variables
911
from inputs.workspace import workspace
1012

1113
fmt = Action(
@@ -20,6 +22,11 @@
2022
$ProductName workspace to inspect when discovering the $ProductName version to use, if the version is not otherwise specified.
2123
See [dflook/$ToolName-version](https://github.com/dflook/terraform-github-actions/tree/main/$ToolName-version#$ToolName-version-action) for details.
2224
'''),
25+
dataclasses.replace(variables, available_in=[OpenTofu], description='''
26+
Variables to set when initializing $ProductName. This should be valid $ProductName syntax - like a [variable definition file]($VariableDefinitionUrl).
27+
Variables set here override any given in `var_file`s.
28+
'''),
29+
dataclasses.replace(var_file, available_in=[OpenTofu]),
2330
dataclasses.replace(backend_config, description='''
2431
List of $ProductName backend config values, one per line. This is used for discovering the $ProductName version to use, if the version is not otherwise specified.
2532
See [dflook/$ToolName-version](https://github.com/dflook/terraform-github-actions/tree/main/$ToolName-version#$ToolName-version-action) for details.
@@ -70,4 +77,4 @@
7077
branch: automated-$ToolName-fmt
7178
```
7279
'''
73-
)
80+
)

docs-gen/actions/fmt_check.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import dataclasses
22

3-
from action import Action
3+
from action import Action, OpenTofu
44
from environment_variables.GITHUB_DOT_COM_TOKEN import GITHUB_DOT_COM_TOKEN
55
from environment_variables.TERRAFORM_CLOUD_TOKENS import TERRAFORM_CLOUD_TOKENS
66
from inputs.backend_config import backend_config
77
from inputs.backend_config_file import backend_config_file
88
from inputs.path import path
9+
from inputs.var_file import var_file
10+
from inputs.variables import variables
911
from inputs.workspace import workspace
1012
from outputs.failure_reason import failure_reason
1113

@@ -24,6 +26,11 @@
2426
$ProductName workspace to inspect when discovering the $ProductName version to use, if the version is not otherwise specified.
2527
See [dflook/$ToolName-version](https://github.com/dflook/terraform-github-actions/tree/main/$ToolName-version#$ToolName-version-action) for details.
2628
'''),
29+
dataclasses.replace(variables, available_in=[OpenTofu], description='''
30+
Variables to set when initializing $ProductName. This should be valid $ProductName syntax - like a [variable definition file]($VariableDefinitionUrl).
31+
Variables set here override any given in `var_file`s.
32+
'''),
33+
dataclasses.replace(var_file, available_in=[OpenTofu]),
2734
dataclasses.replace(backend_config, description='''
2835
List of $ProductName backend config values, one per line. This is used for discovering the $ProductName version to use, if the version is not otherwise specified.
2936
See [dflook/$ToolName-version](https://github.com/dflook/terraform-github-actions/tree/main/$ToolName-version#$ToolName-version-action) for details.
@@ -96,4 +103,4 @@
96103
run: echo "formatting check failed"
97104
```
98105
'''
99-
)
106+
)

docs-gen/actions/new_workspace.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
inputs=[
2222
path,
2323
dataclasses.replace(workspace, description='The name of the $ProductName workspace to create.', required=True, default=None),
24-
dataclasses.replace(variables, description='''
24+
dataclasses.replace(variables, available_in=[OpenTofu], description='''
2525
Variables to set when initializing $ProductName. This should be valid $ProductName syntax - like a [variable definition file]($VariableDefinitionUrl).
2626
2727
Variables set here override any given in `var_file`s.
28-
''', available_in=[OpenTofu]),
28+
'''),
2929
dataclasses.replace(var_file, available_in=[OpenTofu]),
3030
backend_config,
3131
backend_config_file,

docs-gen/actions/output.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import dataclasses
22

3-
from action import Action
3+
from action import Action, OpenTofu
44
from environment_variables.GITHUB_DOT_COM_TOKEN import GITHUB_DOT_COM_TOKEN
55
from environment_variables.TERRAFORM_CLOUD_TOKENS import TERRAFORM_CLOUD_TOKENS
66
from environment_variables.TERRAFORM_HTTP_CREDENTIALS import TERRAFORM_HTTP_CREDENTIALS
@@ -9,6 +9,8 @@
99
from inputs.backend_config import backend_config
1010
from inputs.backend_config_file import backend_config_file
1111
from inputs.path import path
12+
from inputs.var_file import var_file
13+
from inputs.variables import variables
1214
from inputs.workspace import workspace
1315
from outputs.terraform_outputs import terraform_outputs
1416

@@ -20,8 +22,14 @@
2022
inputs=[
2123
path,
2224
dataclasses.replace(workspace, description='$ProductName workspace to get outputs from'),
25+
dataclasses.replace(variables, available_in=[OpenTofu], description='''
26+
Variables to set when initializing $ProductName. This should be valid $ProductName syntax - like a [variable definition file]($VariableDefinitionUrl).
27+
28+
Variables set here override any given in `var_file`s.
29+
'''),
30+
dataclasses.replace(var_file, available_in=[OpenTofu]),
2331
backend_config,
24-
backend_config_file,
32+
backend_config_file
2533
],
2634
environment_variables=[
2735
GITHUB_DOT_COM_TOKEN,
@@ -106,4 +114,4 @@
106114
The subnet-ids are subnet-053008016a2c1768c,subnet-07d4ce437c43eba2f,subnet-0a5f8c3a20023b8c0
107115
```
108116
'''
109-
)
117+
)

image/actions.sh

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,10 @@ function init() {
171171
start_group "Initializing $TOOL_PRODUCT_NAME"
172172

173173
rm -rf "$TF_DATA_DIR"
174-
debug_log "$TOOL_COMMAND_NAME" init -input=false -backend=false
175-
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME init -input=false -backend=false)
174+
# shellcheck disable=SC2086
175+
debug_log "$TOOL_COMMAND_NAME" init -input=false -backend=false $EARLY_VARIABLE_ARGS
176+
# shellcheck disable=SC2086
177+
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME init -input=false -backend=false $EARLY_VARIABLE_ARGS)
176178

177179
end_group
178180
}
@@ -187,11 +189,15 @@ function init-test() {
187189
rm -rf "$TF_DATA_DIR"
188190

189191
if [[ -n "$INPUT_TEST_DIRECTORY" ]]; then
190-
debug_log "$TOOL_COMMAND_NAME" init -input=false -backend=false -test-directory "$INPUT_TEST_DIRECTORY"
191-
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME init -input=false -backend=false -test-directory "$INPUT_TEST_DIRECTORY")
192+
# shellcheck disable=SC2086
193+
debug_log "$TOOL_COMMAND_NAME" init -input=false -backend=false $EARLY_VARIABLE_ARGS -test-directory "$INPUT_TEST_DIRECTORY"
194+
# shellcheck disable=SC2086
195+
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME init -input=false -backend=false $EARLY_VARIABLE_ARGS -test-directory "$INPUT_TEST_DIRECTORY")
192196
else
193-
debug_log "$TOOL_COMMAND_NAME" init -input=false -backend=false
194-
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME init -input=false -backend=false)
197+
# shellcheck disable=SC2086
198+
debug_log "$TOOL_COMMAND_NAME" init -input=false -backend=false $EARLY_VARIABLE_ARGS
199+
# shellcheck disable=SC2086
200+
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME init -input=false -backend=false $EARLY_VARIABLE_ARGS)
195201
fi
196202

197203
end_group
@@ -221,9 +227,9 @@ function set-init-args() {
221227
if [[ -v OPENTOFU && $TERRAFORM_VER_MINOR -ge 8 ]]; then
222228
debug_log "Preparing variables for early evaluation"
223229
set-variable-args
224-
INIT_ARGS="$INIT_ARGS $VARIABLE_ARGS"
230+
EARLY_VARIABLE_ARGS=$VARIABLE_ARGS
225231
else
226-
VARIABLE_ARGS=""
232+
EARLY_VARIABLE_ARGS=""
227233
fi
228234

229235
export INIT_ARGS
@@ -240,12 +246,12 @@ function init-backend-workspace() {
240246

241247
rm -rf "$TF_DATA_DIR"
242248

243-
# shellcheck disable=SC2016
244-
debug_log TF_WORKSPACE="$INPUT_WORKSPACE" "$TOOL_COMMAND_NAME" init -input=false '$INIT_ARGS' # don't expand INIT_ARGS
249+
# shellcheck disable=SC2016,SC2086
250+
debug_log TF_WORKSPACE="$INPUT_WORKSPACE" "$TOOL_COMMAND_NAME" init -input=false '$INIT_ARGS' $EARLY_VARIABLE_ARGS # don't expand INIT_ARGS
245251

246252
set +e
247253
# shellcheck disable=SC2086
248-
(cd "$INPUT_PATH" && TF_WORKSPACE=$INPUT_WORKSPACE $TOOL_COMMAND_NAME init -input=false $INIT_ARGS \
254+
(cd "$INPUT_PATH" && TF_WORKSPACE=$INPUT_WORKSPACE $TOOL_COMMAND_NAME init -input=false $INIT_ARGS $EARLY_VARIABLE_ARGS \
249255
2>"$STEP_TMP_DIR/terraform_init.stderr")
250256

251257
local INIT_EXIT=$?
@@ -281,11 +287,11 @@ function init-backend-default-workspace() {
281287

282288
rm -rf "$TF_DATA_DIR"
283289

284-
# shellcheck disable=SC2016
285-
debug_log "$TOOL_COMMAND_NAME" init -input=false '$INIT_ARGS' # don't expand INIT_ARGS
290+
# shellcheck disable=SC2016,SC2086
291+
debug_log "$TOOL_COMMAND_NAME" init -input=false '$INIT_ARGS' $EARLY_VARIABLE_ARGS # don't expand INIT_ARGS
286292
set +e
287293
# shellcheck disable=SC2086
288-
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME init -input=false $INIT_ARGS \
294+
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME init -input=false $INIT_ARGS $EARLY_VARIABLE_ARGS \
289295
2>"$STEP_TMP_DIR/terraform_init.stderr")
290296

291297
local INIT_EXIT=$?
@@ -311,11 +317,11 @@ function select-workspace() {
311317
local WORKSPACE_EXIT
312318

313319
# shellcheck disable=SC2086
314-
debug_log "$TOOL_COMMAND_NAME" workspace select $VARIABLE_ARGS "$INPUT_WORKSPACE"
320+
debug_log "$TOOL_COMMAND_NAME" workspace select $EARLY_VARIABLE_ARGS "$INPUT_WORKSPACE"
315321

316322
set +e
317323
# shellcheck disable=SC2086
318-
(cd "$INPUT_PATH" && "$TOOL_COMMAND_NAME" workspace select $VARIABLE_ARGS "$INPUT_WORKSPACE") >"$STEP_TMP_DIR/workspace_select" 2>&1
324+
(cd "$INPUT_PATH" && "$TOOL_COMMAND_NAME" workspace select $EARLY_VARIABLE_ARGS "$INPUT_WORKSPACE") >"$STEP_TMP_DIR/workspace_select" 2>&1
319325
WORKSPACE_EXIT=$?
320326
set -e
321327

@@ -449,8 +455,10 @@ function set-remote-plan-args() {
449455
}
450456

451457
function output() {
452-
debug_log "$TOOL_COMMAND_NAME" output -json
453-
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME output -json | tee "$STEP_TMP_DIR/terraform_output.json" | convert_output)
458+
# shellcheck disable=SC2086
459+
debug_log "$TOOL_COMMAND_NAME" output -json $EARLY_VARIABLE_ARGS
460+
# shellcheck disable=SC2086
461+
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME output -json $EARLY_VARIABLE_ARGS | tee "$STEP_TMP_DIR/terraform_output.json" | convert_output)
454462
}
455463

456464
function random_string() {
@@ -546,8 +554,10 @@ function destroy() {
546554

547555
function force_unlock() {
548556
echo "Unlocking state with ID: $INPUT_LOCK_ID"
549-
debug_log "$TOOL_COMMAND_NAME" force-unlock -force "$INPUT_LOCK_ID"
550-
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME force-unlock -force "$INPUT_LOCK_ID")
557+
# shellcheck disable=SC2086
558+
debug_log "$TOOL_COMMAND_NAME" force-unlock -force $EARLY_VARIABLE_ARGS "$INPUT_LOCK_ID"
559+
# shellcheck disable=SC2086
560+
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME force-unlock -force $EARLY_VARIABLE_ARGS "$INPUT_LOCK_ID")
551561
}
552562

553563
# Every file written to disk should use one of these directories

image/entrypoints/destroy-workspace.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ else
3636
init-backend-default-workspace
3737

3838
# shellcheck disable=SC2086
39-
debug_log $TOOL_COMMAND_NAME workspace delete $VARIABLE_ARGS -no-color -lock-timeout=300s "$INPUT_WORKSPACE"
39+
debug_log $TOOL_COMMAND_NAME workspace delete $EARLY_VARIABLE_ARGS -no-color -lock-timeout=300s "$INPUT_WORKSPACE"
4040
# shellcheck disable=SC2086
41-
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME workspace delete $VARIABLE_ARGS -no-color -lock-timeout=300s "$INPUT_WORKSPACE")
41+
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME workspace delete $EARLY_VARIABLE_ARGS -no-color -lock-timeout=300s "$INPUT_WORKSPACE")
4242
fi

image/entrypoints/new-workspace.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ init-backend-default-workspace
1515

1616
set +e
1717
# shellcheck disable=SC2086
18-
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME workspace list $VARIABLE_ARGS -no-color) \
18+
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME workspace list $EARLY_VARIABLE_ARGS -no-color) \
1919
2>"$STEP_TMP_DIR/terraform_workspace_list.stderr" \
2020
>"$STEP_TMP_DIR/terraform_workspace_list.stdout"
2121

@@ -34,13 +34,13 @@ fi
3434
if workspace_exists "$INPUT_WORKSPACE" <"$STEP_TMP_DIR/terraform_workspace_list.stdout"; then
3535
echo "Workspace appears to exist, selecting it"
3636
# shellcheck disable=SC2086
37-
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME workspace select $VARIABLE_ARGS -no-color "$INPUT_WORKSPACE")
37+
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME workspace select $EARLY_VARIABLE_ARGS -no-color "$INPUT_WORKSPACE")
3838
else
3939
echo "Workspace does not appear to exist, attempting to create it"
4040

4141
set +e
4242
# shellcheck disable=SC2086
43-
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME workspace new $VARIABLE_ARGS -no-color -lock-timeout=300s "$INPUT_WORKSPACE") \
43+
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME workspace new $EARLY_VARIABLE_ARGS -no-color -lock-timeout=300s "$INPUT_WORKSPACE") \
4444
2>"$STEP_TMP_DIR/terraform_workspace_new.stderr" \
4545
>"$STEP_TMP_DIR/terraform_workspace_new.stdout"
4646

@@ -56,7 +56,7 @@ else
5656
if grep -Fq "already exists" "$STEP_TMP_DIR/terraform_workspace_new.stderr"; then
5757
echo "Workspace does exist, selecting it"
5858
# shellcheck disable=SC2086
59-
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME workspace select $VARIABLE_ARGS -no-color "$INPUT_WORKSPACE")
59+
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME workspace select $EARLY_VARIABLE_ARGS -no-color "$INPUT_WORKSPACE")
6060
else
6161
cat "$STEP_TMP_DIR/terraform_workspace_new.stderr"
6262
cat "$STEP_TMP_DIR/terraform_workspace_new.stdout"

image/entrypoints/plan.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ if [[ -n "$PLAN_OUT" ]]; then
8484
cp "$PLAN_OUT" "$GITHUB_WORKSPACE/$WORKSPACE_TMP_DIR/plan.tfplan"
8585
set_output plan_path "$WORKSPACE_TMP_DIR/plan.tfplan"
8686

87-
if (cd "$INPUT_PATH" && $TOOL_COMMAND_NAME show -json "$PLAN_OUT") >"$GITHUB_WORKSPACE/$WORKSPACE_TMP_DIR/plan.json" 2>"$STEP_TMP_DIR/terraform_show.stderr"; then
87+
# shellcheck disable=SC2086
88+
if (cd "$INPUT_PATH" && $TOOL_COMMAND_NAME show -json $EARLY_VARIABLE_ARGS "$PLAN_OUT") >"$GITHUB_WORKSPACE/$WORKSPACE_TMP_DIR/plan.json" 2>"$STEP_TMP_DIR/terraform_show.stderr"; then
8889
set_output json_plan_path "$WORKSPACE_TMP_DIR/plan.json"
8990
else
9091
debug_file "$STEP_TMP_DIR/terraform_show.stderr"

image/entrypoints/validate.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ fi
2121

2222
init || true
2323

24-
if ! (cd "$INPUT_PATH" && TF_WORKSPACE="$TF_WORKSPACE" $TOOL_COMMAND_NAME validate -json | convert_validate_report "$INPUT_PATH"); then
24+
# shellcheck disable=SC2086
25+
if ! (cd "$INPUT_PATH" && TF_WORKSPACE="$TF_WORKSPACE" $TOOL_COMMAND_NAME validate -json $EARLY_VARIABLE_ARGS | convert_validate_report "$INPUT_PATH"); then
2526
(cd "$INPUT_PATH" && TF_WORKSPACE="$TF_WORKSPACE" $TOOL_COMMAND_NAME validate)
2627
else
2728
echo -e "\033[1;32mSuccess!\033[0m The configuration is valid"

0 commit comments

Comments
 (0)