Skip to content

Commit

Permalink
feat: add support for planning with opentofu (#37)
Browse files Browse the repository at this point in the history
I was looking for the simplest way to add these without any breaking
change for existing users. I think maybe this is pragmatic enough.

For now, the commands are all drop in - just replace the binary, so the
action just needs a way for that to be passed in.
  • Loading branch information
aclemons authored Jan 17, 2025
1 parent ba947eb commit dce0cbf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
29 changes: 24 additions & 5 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ inputs:
terraform_version:
description: "Terraform version to run the check against. Set to 'system' to use the system terraform."
default: "latest"
terraform_binary:
description: "Terraform binary to use. Defaults to 'terraform'. Can be set to 'tofu' to use opentofu."
default: "terraform"
plan_args:
description: "Optional: Additional arguments to pass to terraform plan. This should be passed as json array."
post_comment:
Expand Down Expand Up @@ -38,6 +41,22 @@ runs:
with:
python-version: "3.12"

- id: set-binary
env:
TERRAFORM_BINARY: ${{ inputs.terraform_binary }}
run: |
# sanitise input
if [[ "$TERRAFORM_BINARY" == "tofu" ]] ; then
printf 'TF=%s\n' "tofu" >> "$GITHUB_ENV"
elif [[ "$TERRAFORM_BINARY" == "terraform" ]] ; then
printf 'TF=%s\n' "terraform" >> "$GITHUB_ENV"
else
1>&2 printf 'Unsupported terraform binary %s\n' "$TERRAFORM_BINARY"
exit 1
fi
shell: bash


- name: Setup tenv
if: ${{ inputs.terraform_version != 'system' }}
run: |
Expand Down Expand Up @@ -101,14 +120,14 @@ runs:
fi
if [ "${versions[$i]}" = "system" ] ; then
echo "Using system provided terraform"
terraform --version
echo "Using system provided $TF"
"$TF" --version
elif [ -z "${versions[$i]}" ] ; then
: # using whatever we setup previously
else
# Change Terraform version
tenv tf install "${versions[$i]}"
tenv tf use "${versions[$i]}"
# Change version
tenv "$TF" install "${versions[$i]}"
tenv "$TF" use "${versions[$i]}"
fi
if [ -z "${plan_args[$i]}" ] ; then
Expand Down
9 changes: 5 additions & 4 deletions tfcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

from jinja2 import Environment, FileSystemLoader

FMT_CMD = "terraform fmt -check=true -write=false -recursive -diff"
INIT_CMD = "terraform init -no-color"
VALIDATE_CMD = "terraform validate -no-color"
PLAN_CMD = "terraform plan -detailed-exitcode -no-color"
TF_CMD = os.environ.get("TF", "terraform")
FMT_CMD = f"{TF_CMD} fmt -check=true -write=false -recursive -diff"
INIT_CMD = f"{TF_CMD} init -no-color"
VALIDATE_CMD = f"{TF_CMD} validate -no-color"
PLAN_CMD = f"{TF_CMD} plan -detailed-exitcode -no-color"

parser = argparse.ArgumentParser()
parser.add_argument("path", help="path to run terraform checks", type=str)
Expand Down

0 comments on commit dce0cbf

Please sign in to comment.