diff --git a/CHANGELOG.md b/CHANGELOG.md index 87637556..aa91b895 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,8 +26,7 @@ When using an action you can specify the version as: - [dflook/terraform-destroy-workspace](https://github.com/dflook/terraform-github-actions/tree/master/terraform-destroy-workspace#outputs): destroy-failed ### Fixed -- [dflook/terraform-validate](https://github.com/dflook/terraform-github-actions/tree/master/terraform-validate) was - sometimes unable to create detailed check failures. +- [dflook/terraform-validate](https://github.com/dflook/terraform-github-actions/tree/master/terraform-validate) was sometimes unable to create detailed check failures. ## [1.14.0] - 2021-09-15 diff --git a/image/actions.sh b/image/actions.sh index e07d3153..1c35f4f1 100644 --- a/image/actions.sh +++ b/image/actions.sh @@ -28,6 +28,15 @@ function detect-terraform-version() { fi debug_cmd ls -la "$(which terraform)" + + local TF_VERSION + TF_VERSION=$(terraform version -json | jq -r '.terraform_version' 2>/dev/null || terraform version | grep 'Terraform v' | sed 's/Terraform v//') + + TERRAFORM_VER_MAJOR=`echo $TF_VERSION | cut -d. -f1` + TERRAFORM_VER_MINOR=`echo $TF_VERSION | cut -d. -f2` + TERRAFORM_VER_PATCH=`echo $TF_VERSION | cut -d. -f3` + + debug_log "Terraform version major $TERRAFORM_VER_MAJOR minor $TERRAFORM_VER_MINOR patch $TERRAFORM_VER_PATCH" } function job_markdown_ref() { diff --git a/image/entrypoints/version.sh b/image/entrypoints/version.sh index 49795586..af7efbe7 100755 --- a/image/entrypoints/version.sh +++ b/image/entrypoints/version.sh @@ -6,4 +6,4 @@ debug setup init -(cd "$INPUT_PATH" && terraform version -no-color | convert_version) +(cd "$INPUT_PATH" && terraform version -json | convert_version) diff --git a/image/tools/convert_version.py b/image/tools/convert_version.py index 763175e0..f59d29e8 100755 --- a/image/tools/convert_version.py +++ b/image/tools/convert_version.py @@ -1,8 +1,9 @@ #!/usr/bin/python3 +import json import re import sys -from typing import Iterable +from typing import Iterable, Dict def convert_version(tf_output: str) -> Iterable[str]: @@ -37,10 +38,44 @@ def convert_version(tf_output: str) -> Iterable[str]: yield f'::set-output name={provider_name.strip()}::{provider_version.strip()}' +def convert_version_from_json(tf_output: Dict) -> Iterable[str]: + """ + Convert terraform version JSON output human readable output and GitHub actions output commands + + >>> tf_output = { + "terraform_version": "0.13.7", + "terraform_revision": "", + "provider_selections": { + "registry.terraform.io/hashicorp/random": "2.2.0" + }, + "terraform_outdated": true + } + >>> list(convert_version(tf_output)) + ['Terraform v0.13.7', + '::set-output name=terraform::0.13.7', + '+ provider registry.terraform.io/hashicorp/random v2.2.0', + '::set-output name=random::2.2.0'] + """ + + yield f'Terraform v{tf_output["terraform_version"]}' + yield f'::set-output name=terraform::{tf_output["terraform_version"]}' + + for path, version in tf_output['provider_selections'].items(): + name_match = re.match(r'(.*?)/(.*?)/(.*)', path) + name = name_match.group(3) if name_match else path + + yield f'+ provider {path} v{version}' + yield f'::set-output name={name}::{version}' + + if __name__ == '__main__': tf_output = sys.stdin.read() - print(tf_output) + try: + for line in convert_version_from_json(json.loads(tf_output)): + print(line) + except: + print(tf_output) - for line in convert_version(tf_output): - print(line) + for line in convert_version(tf_output): + print(line) diff --git a/tests/version/test_version.py b/tests/version/test_version.py index e41a7e54..4d657571 100644 --- a/tests/version/test_version.py +++ b/tests/version/test_version.py @@ -1,4 +1,5 @@ -from convert_version import convert_version +from convert_version import convert_version, convert_version_from_json + def test_convert_version(): tf_version_output = 'Terraform v0.12.28' @@ -54,3 +55,25 @@ def test_convert_0_13_providers(): ] assert list(convert_version(tf_version_output)) == expected + +def test_convert_0_13_json_providers(): + tf_version_output = { + "terraform_version": "0.13.0", + "terraform_revision": "", + "provider_selections": { + "registry.terraform.io/hashicorp/random": "2.2.0", + "registry.terraform.io/terraform-providers/acme": "2.5.3" + }, + "terraform_outdated": True + } + + expected = [ + 'Terraform v0.13.0', + '::set-output name=terraform::0.13.0', + '+ provider registry.terraform.io/hashicorp/random v2.2.0', + '::set-output name=random::2.2.0', + '+ provider registry.terraform.io/terraform-providers/acme v2.5.3', + '::set-output name=acme::2.5.3' + ] + + assert list(convert_version_from_json(tf_version_output)) == expected