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

Prefer the json output from terraform version #99

Merged
merged 4 commits into from
Sep 28, 2021
Merged
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
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions image/actions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion image/entrypoints/version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ debug
setup
init

(cd "$INPUT_PATH" && terraform version -no-color | convert_version)
(cd "$INPUT_PATH" && terraform version -json | convert_version)
43 changes: 39 additions & 4 deletions image/tools/convert_version.py
Original file line number Diff line number Diff line change
@@ -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]:
Expand Down Expand Up @@ -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)
25 changes: 24 additions & 1 deletion tests/version/test_version.py
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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