Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Early eval #361

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/test-early-eval.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Test OpenTofu early eval

on:
- pull_request

permissions:
contents: read

jobs:
plan:
runs-on: ubuntu-24.04
name: Plan with early eval
steps:
- name: Checkout
uses: actions/checkout@v4
with:
persist-credentials: false

- name: terraform plan
uses: ./tofu-plan
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
with:
path: tests/workflows/test-plan/early-eval/tofu
variables: |
passphrase = "tofuqwertyuiopasdfgh"
19 changes: 15 additions & 4 deletions image/actions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,14 @@ function set-init-args() {
done
fi

if [[ -v OPENTOFU && $TERRAFORM_VER_MINOR -ge 8 ]]; then
debug "Preparing variables for early evaluation"
set-variable-args
INIT_ARGS="$INIT_ARGS $VARIABLE_ARGS"
else
VARIABLE_ARGS=""
fi

export INIT_ARGS
}

Expand Down Expand Up @@ -299,9 +307,9 @@ function init-backend-default-workspace() {
function select-workspace() {
local WORKSPACE_EXIT

debug_log $TOOL_COMMAND_NAME workspace select "$INPUT_WORKSPACE"
debug_log $TOOL_COMMAND_NAME workspace select "$VARIABLE_ARGS" "$INPUT_WORKSPACE" # don't expand VARIABLE_ARGS
set +e
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME workspace select "$INPUT_WORKSPACE") >"$STEP_TMP_DIR/workspace_select" 2>&1
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME workspace select "$VARIABLE_ARGS" "$INPUT_WORKSPACE") >"$STEP_TMP_DIR/workspace_select" 2>&1
WORKSPACE_EXIT=$?
set -e

Expand Down Expand Up @@ -360,6 +368,8 @@ function set-common-plan-args() {
}

function set-variable-args() {
VARIABLE_ARGS=""

if [[ -n "$INPUT_VAR_FILE" ]]; then
for file in $(echo "$INPUT_VAR_FILE" | tr ',' '\n'); do

Expand All @@ -368,13 +378,13 @@ function set-variable-args() {
exit 1
fi

PLAN_ARGS="$PLAN_ARGS -var-file=$(relative_to "$INPUT_PATH" "$file")"
VARIABLE_ARGS="$VARIABLE_ARGS -var-file=$(relative_to "$INPUT_PATH" "$file")"
done
fi

if [[ -n "$INPUT_VARIABLES" ]]; then
echo "$INPUT_VARIABLES" >"$STEP_TMP_DIR/variables.tfvars"
PLAN_ARGS="$PLAN_ARGS -var-file=$STEP_TMP_DIR/variables.tfvars"
VARIABLE_ARGS="$VARIABLE_ARGS -var-file=$STEP_TMP_DIR/variables.tfvars"
fi
}

Expand All @@ -388,6 +398,7 @@ function set-plan-args() {
fi

set-variable-args
PLAN_ARGS="$PLAN_ARGS $VARIABLE_ARGS"

export PLAN_ARGS
}
Expand Down
5 changes: 2 additions & 3 deletions image/entrypoints/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ function set-test-args() {

function test() {

debug_log $TOOL_COMMAND_NAME test -no-color $TEST_ARGS '$PLAN_ARGS' # don't expand PLAN_ARGS
debug_log $TOOL_COMMAND_NAME test -no-color $TEST_ARGS '$VARIABLE_ARGS' # don't expand VARIABLE_ARGS

set +e
# shellcheck disable=SC2086
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME test -no-color $TEST_ARGS $PLAN_ARGS) \
(cd "$INPUT_PATH" && $TOOL_COMMAND_NAME test -no-color $TEST_ARGS $VARIABLE_ARGS) \
2>"$STEP_TMP_DIR/terraform_test.stderr" \
| tee /dev/fd/3 \
>"$STEP_TMP_DIR/terraform_test.stdout"
Expand All @@ -59,7 +59,6 @@ function test() {
}

set-test-args
PLAN_ARGS=""
set-variable-args

test
42 changes: 42 additions & 0 deletions tests/workflows/test-plan/early-eval/tofu/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
terraform {
backend "s3" {
bucket = var.state_bucket
key = "test-plan-early-eval"
region = "eu-west-2"
}
}

variable "state_bucket" {
type = string
}

variable "acm_certificate_version" {
type = string
default = "4.3.0"
}

variable "passphrase" {
type = string
sensitive = true
}

module "s3-bucket" {
source = "terraform-aws-modules/s3-bucket/aws"
version = var.acm_certificate_version
}

terraform {
encryption {
key_provider "pbkdf2" "my_passphrase" {
passphrase = var.passphrase
}

method "aes_gcm" "my_method" {
keys = key_provider.pbkdf2.my_passphrase
}

state {
method = method.aes_gcm.my_method
}
}
}
1 change: 1 addition & 0 deletions tests/workflows/test-plan/early-eval/tofu/terraform.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
state_bucket = "terraform-github-actions"
Loading