diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1d062cb..862f4db 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -58,7 +58,7 @@ jobs: - name: Get Terraform version run: | - TERRAFORM_VERSION=$(jq --raw-output '.terraform_version' cookiecutter.json) + source scripts/get_terraform_versions_dockerized.sh echo "TERRAFORM_VERSION=${TERRAFORM_VERSION}" >> "${GITHUB_ENV}" - name: Setup Terraform diff --git a/Dockerfile b/Dockerfile index a9c6f37..ce4c2f6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,8 @@ FROM golang:1.22.3 -RUN apt-get update && apt-get install --yes jq vim +RUN apt-get update && apt-get install --yes jq # https://pkg.go.dev/github.com/hashicorp/terraform-config-inspect?tab=versions RUN go install github.com/hashicorp/terraform-config-inspect@v0.0.0-20240509232506-4708120f8f30 -CMD external/scripts/run_from_docker_container.sh +CMD external/scripts/get_terraform_versions_json.sh external diff --git a/hooks/pre_prompt.sh b/hooks/pre_prompt.sh index 2ffab0f..17a618e 100755 --- a/hooks/pre_prompt.sh +++ b/hooks/pre_prompt.sh @@ -1,6 +1,4 @@ #!/bin/bash -DOCKER_IMAGE_TAG="tf-config-inspect" - -docker build --tag "${DOCKER_IMAGE_TAG}" "." -docker run --rm --volume ".:/go/external" "${DOCKER_IMAGE_TAG}" +source scripts/get_terraform_versions_dockerized.sh +scripts/edit_cookiecutter_json.sh diff --git a/scripts/edit_cookiecutter_json.sh b/scripts/edit_cookiecutter_json.sh index 24d7e23..ba486f7 100755 --- a/scripts/edit_cookiecutter_json.sh +++ b/scripts/edit_cookiecutter_json.sh @@ -3,10 +3,7 @@ [[ -z "${TERRAFORM_VERSION}" ]] && echo "Error: TERRAFORM_VERSION must be defined" && exit 10 [[ -z "${TERRAFORM_AWS_VERSION}" ]] && echo "Error: TERRAFORM_AWS_VERSION must be defined" && exit 20 -COOKIECUTTER_JSON="$1" -[[ -z "${COOKIECUTTER_JSON}" ]] && echo "Error: COOKIECUTTER_JSON must be passed as first argument" && exit 100 - ex -u NONE \ -c "%s//${TERRAFORM_VERSION}/" \ -c "%s//${TERRAFORM_AWS_VERSION}/" \ - -c "wq" "${COOKIECUTTER_JSON}" + -c "wq" "cookiecutter.json" diff --git a/scripts/get_terraform_versions.sh b/scripts/get_terraform_versions.sh deleted file mode 100755 index 8266ee8..0000000 --- a/scripts/get_terraform_versions.sh +++ /dev/null @@ -1,41 +0,0 @@ -#!/bin/bash - -[[ -z "${TERRAFORM_PROD_DIR}" ]] && echo "Error: TERRAFORM_PROD_DIR must be defined" && exit 10 - -TF_CONFIG_INSPECT_CMD="terraform-config-inspect" - -echo "Chech if '${TF_CONFIG_INSPECT_CMD}' exists..." -command -v "${TF_CONFIG_INSPECT_CMD}" -# echo "'${TF_CONFIG_INSPECT_CMD}' found!" - -TF_CONFIG_INSPECTION=$( - "${TF_CONFIG_INSPECT_CMD}" --json "${TERRAFORM_PROD_DIR}" | \ - jq --compact-output '.' -) - -TERRAFORM_VERSION=$( - echo "${TF_CONFIG_INSPECTION}" | \ - jq --raw-output '.required_core[0] | select(. != null)' -) -TERRAFORM_AWS_VERSION=$( - echo "$TF_CONFIG_INSPECTION" | \ - jq --raw-output '.required_providers.aws.version_constraints[0] | select(. != null)' -) - -TF_DIAGNOSTICS=$( - echo "${TF_CONFIG_INSPECTION}" | \ - jq --compact-output '.diagnostics | select(. != null)' -) -echo "TF_DIAGNOSTICS: ${TF_DIAGNOSTICS}" - -if [[ -z "${TERRAFORM_VERSION}" ]]; then - echo "Diagnostics:" - echo "${TF_DIAGNOSTICS}" | jq '.' - exit 123 -fi - -echo "TERRAFORM_VERSION: ${TERRAFORM_VERSION}" -export TERRAFORM_VERSION - -echo "TERRAFORM_AWS_VERSION: ${TERRAFORM_AWS_VERSION}" -export TERRAFORM_AWS_VERSION diff --git a/scripts/get_terraform_versions_dockerized.sh b/scripts/get_terraform_versions_dockerized.sh new file mode 100755 index 0000000..2bd36e7 --- /dev/null +++ b/scripts/get_terraform_versions_dockerized.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +DOCKER_IMAGE_TAG="tf-config-inspect" + +docker build --tag "${DOCKER_IMAGE_TAG}" "." + +ALL_TERRAFORM_VERSIONS=$( docker run --rm --volume ".:/go/external" "${DOCKER_IMAGE_TAG}" ) + +echo "ALL_TERRAFORM_VERSIONS = ${ALL_TERRAFORM_VERSIONS}" + +export TERRAFORM_VERSION=$( echo "${ALL_TERRAFORM_VERSIONS}" | jq --raw-output '.terraform_version' ) +echo "TERRAFORM_VERSION = ${TERRAFORM_VERSION}" + +export TERRAFORM_AWS_VERSION=$( echo "${ALL_TERRAFORM_VERSIONS}" | jq --raw-output '.terraform_aws_version' ) +echo "TERRAFORM_AWS_VERSION = ${TERRAFORM_AWS_VERSION}" diff --git a/scripts/get_terraform_versions_json.sh b/scripts/get_terraform_versions_json.sh new file mode 100755 index 0000000..d40a88d --- /dev/null +++ b/scripts/get_terraform_versions_json.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +TERRAFORM_PROD_DIR="$1" +[[ -z "${TERRAFORM_PROD_DIR}" ]] && echo '{ "error": "TERRAFORM_PROD_DIR must be passed as first argument" }' && exit 10 + +terraform-config-inspect --json "${TERRAFORM_PROD_DIR}" | \ + jq --monochrome-output --compact-output '{ + "terraform_version": .required_core[0], + "terraform_aws_version": .required_providers.aws.version_constraints[0] + }' diff --git a/scripts/run_from_docker_container.sh b/scripts/run_from_docker_container.sh deleted file mode 100755 index 2e097a2..0000000 --- a/scripts/run_from_docker_container.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash - -TERRAFORM_PROD_DIR=external source external/scripts/get_terraform_versions.sh -external/scripts/edit_cookiecutter_json.sh "external/cookiecutter.json"