Skip to content

Instantly share code, notes, and snippets.

@fquffio
Created March 21, 2024 15:50
Show Gist options
  • Save fquffio/9fa7943aebf3c8eed8ebc13187539304 to your computer and use it in GitHub Desktop.
Save fquffio/9fa7943aebf3c8eed8ebc13187539304 to your computer and use it in GitHub Desktop.
Extemely simple shell script to check if currently installed Terraform providers are to the latest available version. Requires Terraform, JQ and CURL.
#!/bin/sh
set -eo pipefail
if ! command -v terraform &> /dev/null
then
echo 'Terraform is not installed or `terraform` executable could not be found in $PATH.' >&2
exit 127
fi
if ! command -v jq &> /dev/null
then
echo 'JQ is not installed or `jq` executable could not be found in $PATH.' >&2
exit 127
fi
if ! command -v curl &> /dev/null
then
echo 'CURL is not installed or `curl` executable could not be found in $PATH.' >&2
exit 127
fi
CHDIR="${1:-.}"
STATUS=0
for p in $(terraform -chdir="${CHDIR}" version -json | jq '.provider_selections | to_entries[] | "\(.key)@\(.value)"' -cr)
do
PROVIDER="$(cut -d@ -f1 <<< $p)"
REGISTRY="$(cut -d/ -f1 <<< "${PROVIDER}")"
PROVIDER_NAME="$(cut -d/ -f2- <<< "${PROVIDER}")"
CURRENT_VERSION="$(cut -d@ -f2 <<< $p)"
printf "=====> \e[1;34m%s\e[0m: %s (current) -> " "${PROVIDER}" "${CURRENT_VERSION}"
LATEST_VERSION="$(curl -sf "https://${REGISTRY}/v1/providers/${PROVIDER_NAME}" | jq -r '.version')"
if [ "${LATEST_VERSION}" = "${CURRENT_VERSION}" ]
then
printf "\e[32m%s\e[0m\n" "${LATEST_VERSION}"
elif [ "$(cut -d. -f 1 <<< "${LATEST_VERSION}")" = "$(cut -d. -f 1 <<< "${CURRENT_VERSION}")" ]
then
printf "\e[1;33m%s\e[0m\n" "${LATEST_VERSION}"
STATUS=1
else
printf "\e[1;31m%s\e[0m\n" "${LATEST_VERSION}"
STATUS=1
fi
done
exit $STATUS
@fquffio
Copy link
Author

fquffio commented Mar 21, 2024

Example usage:

  • Check versions of Terraform providers for the module located in the current working directory:

    $ terraform-providers-versions-check
  • Check versions of Terraform providers for the module located in ./my-module:

    $ terraform-providers-versions-check ./my-module

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment