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

Improve handling of optional variables #2

Merged
merged 3 commits into from
Jan 13, 2025
Merged
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
10 changes: 10 additions & 0 deletions .github/terraform/main.tf
Original file line number Diff line number Diff line change
@@ -14,6 +14,16 @@ variable "image" {
type = string
}

variable "optional" {
type = string
default = null

validation {
condition = var.optional == null || var.optional == "foo"
error_message = "`optional` must only be `null` or `\"foo\"`."
}
}

resource "docker_image" "web_server" {
name = var.image
keep_locally = false
2 changes: 2 additions & 0 deletions .github/workflows/integration-tests.yaml
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@ jobs:
workspace: ${{ github.event.number }}
variables: |-
image: nginx:latest
optional:
- name: Validate apply
run: |
status="$(curl -fsSL -o /dev/null -w "%{http_code}" http://localhost:8000)"
@@ -44,6 +45,7 @@ jobs:
workspace: ${{ github.event.number }}
variables: |-
image: nginx:latest
optional:
- name: Validate destroy
run: |
rc=0
6 changes: 5 additions & 1 deletion apply/action.yaml
Original file line number Diff line number Diff line change
@@ -48,12 +48,16 @@ runs:
# Execute
set -eo pipefail

# Convert JSON variables into CLI flags. We'll remove entries with `null` values as we cannot
# pass those into Terraform:
# https://github.com/hashicorp/terraform/issues/29078
#
# TODO: Add tests for JSON lists and values with spaces. If you aren't very careful can end up
# with `'-var=foo="a b"'` which looks like a positional argument to terraform.
var_flags=()
while read -r var; do
var_flags+=(-var "${var:?}")
done < <(jq -r 'to_entries[] | "\(.key)=\(.value | tostring)"' <<<"${variables_json}")
done < <(jq -r 'to_entries[] | select(.value != null) | "\(.key)=\(.value | tostring)"' <<<"${variables_json}")

echo "::group::terraform init"
terraform init
6 changes: 5 additions & 1 deletion destroy/action.yaml
Original file line number Diff line number Diff line change
@@ -43,12 +43,16 @@ runs:
# Destroy
set -eo pipefail

# Convert JSON variables into CLI flags. We'll remove entries with `null` values as we cannot
# pass those into Terraform:
# https://github.com/hashicorp/terraform/issues/29078
#
# TODO: Add tests for JSON lists and values with spaces. If you aren't very careful can end up
# with `'-var=foo="a b"'` which looks like a positional argument to terraform.
var_flags=()
while read -r var; do
var_flags+=(-var "${var:?}")
done < <(jq -r 'to_entries[] | "\(.key)=\(.value | tostring)"' <<<"${variables_json}")
done < <(jq -r 'to_entries[] | select(.value != null) | "\(.key)=\(.value | tostring)"' <<<"${variables_json}")

echo "::group::terraform init"
terraform init