Skip to content

Commit

Permalink
Improve handling of optional variables (#2)
Browse files Browse the repository at this point in the history
Co-authored-by: vosscodes <[email protected]>
  • Loading branch information
omus and vosscodes authored Jan 13, 2025
1 parent f738556 commit 336cb67
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
10 changes: 10 additions & 0 deletions .github/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/integration-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand All @@ -44,6 +45,7 @@ jobs:
workspace: ${{ github.event.number }}
variables: |-
image: nginx:latest
optional:
- name: Validate destroy
run: |
rc=0
Expand Down
6 changes: 5 additions & 1 deletion apply/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion destroy/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 336cb67

Please sign in to comment.