Skip to content

Commit

Permalink
get_terraform_versions_dockerized.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
horothesun committed May 10, 2024
1 parent f7c1c21 commit a85694e
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
6 changes: 2 additions & 4 deletions hooks/pre_prompt.sh
Original file line number Diff line number Diff line change
@@ -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
5 changes: 1 addition & 4 deletions scripts/edit_cookiecutter_json.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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>/${TERRAFORM_VERSION}/" \
-c "%s/<TERRAFORM_AWS_VERSION>/${TERRAFORM_AWS_VERSION}/" \
-c "wq" "${COOKIECUTTER_JSON}"
-c "wq" "cookiecutter.json"
41 changes: 0 additions & 41 deletions scripts/get_terraform_versions.sh

This file was deleted.

15 changes: 15 additions & 0 deletions scripts/get_terraform_versions_dockerized.sh
Original file line number Diff line number Diff line change
@@ -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}"
10 changes: 10 additions & 0 deletions scripts/get_terraform_versions_json.sh
Original file line number Diff line number Diff line change
@@ -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]
}'
4 changes: 0 additions & 4 deletions scripts/run_from_docker_container.sh

This file was deleted.

0 comments on commit a85694e

Please sign in to comment.