Skip to content

Commit 390bc71

Browse files
committed
Add support for early evalution in OpenTofu
Pass variables to the init and workspace select commands
1 parent 1b64573 commit 390bc71

File tree

5 files changed

+92
-3
lines changed

5 files changed

+92
-3
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Test OpenTofu early eval
2+
3+
on:
4+
- pull_request
5+
6+
permissions:
7+
contents: read
8+
9+
jobs:
10+
plan:
11+
runs-on: ubuntu-24.04
12+
name: Plan with early eval
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v4
16+
with:
17+
persist-credentials: false
18+
19+
- name: terraform plan
20+
uses: ./tofu-plan
21+
env:
22+
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
23+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
24+
with:
25+
path: tests/workflows/test-plan/early-eval/tofu
26+
add_github_comment: false
27+
variables: |
28+
passphrase = "tofuqwertyuiopasdfgh"

image/actions.sh

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,14 @@ function set-init-args() {
218218
done
219219
fi
220220

221+
if [[ -v OPENTOFU && $TERRAFORM_VER_MINOR -ge 8 ]]; then
222+
debug_log "Preparing variables for early evaluation"
223+
set-variable-args
224+
INIT_ARGS="$INIT_ARGS $VARIABLE_ARGS"
225+
else
226+
VARIABLE_ARGS=""
227+
fi
228+
221229
export INIT_ARGS
222230
}
223231

@@ -302,9 +310,12 @@ function init-backend-default-workspace() {
302310
function select-workspace() {
303311
local WORKSPACE_EXIT
304312

305-
debug_log "$TOOL_COMMAND_NAME" workspace select "$INPUT_WORKSPACE"
313+
# shellcheck disable=SC2086
314+
debug_log "$TOOL_COMMAND_NAME" workspace select $VARIABLE_ARGS "$INPUT_WORKSPACE"
315+
306316
set +e
307-
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME workspace select "$INPUT_WORKSPACE") >"$STEP_TMP_DIR/workspace_select" 2>&1
317+
# shellcheck disable=SC2086
318+
(cd "$INPUT_PATH" && "$TOOL_COMMAND_NAME" workspace select $VARIABLE_ARGS "$INPUT_WORKSPACE") >"$STEP_TMP_DIR/workspace_select" 2>&1
308319
WORKSPACE_EXIT=$?
309320
set -e
310321

image/entrypoints/test.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ function set-test-args() {
3434

3535
function test() {
3636

37-
debug_log $TOOL_COMMAND_NAME test -no-color "$TEST_ARGS" "$VARIABLE_ARGS"
37+
# shellcheck disable=SC2086
38+
debug_log $TOOL_COMMAND_NAME test -no-color $TEST_ARGS $VARIABLE_ARGS
3839

3940
set +e
4041
# shellcheck disable=SC2086
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
terraform {
2+
backend "s3" {
3+
bucket = var.state_bucket
4+
key = "test-plan-early-eval"
5+
region = "eu-west-2"
6+
}
7+
}
8+
9+
provider "aws" {
10+
region = "eu-west-2"
11+
}
12+
13+
variable "state_bucket" {
14+
type = string
15+
}
16+
17+
variable "acm_certificate_version" {
18+
type = string
19+
default = "4.3.0"
20+
}
21+
22+
variable "passphrase" {
23+
type = string
24+
sensitive = true
25+
}
26+
27+
module "s3-bucket" {
28+
source = "terraform-aws-modules/s3-bucket/aws"
29+
version = var.acm_certificate_version
30+
}
31+
32+
terraform {
33+
encryption {
34+
key_provider "pbkdf2" "my_passphrase" {
35+
passphrase = var.passphrase
36+
}
37+
38+
method "aes_gcm" "my_method" {
39+
keys = key_provider.pbkdf2.my_passphrase
40+
}
41+
42+
state {
43+
method = method.aes_gcm.my_method
44+
}
45+
}
46+
47+
required_version = "1.8.8"
48+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
state_bucket = "terraform-github-actions"

0 commit comments

Comments
 (0)