Skip to content

Instantly share code, notes, and snippets.

@kitos9112
Forked from josh-padnick/get-latest-terraform.sh
Last active March 26, 2024 14:41
Show Gist options
  • Save kitos9112/70faa4b7e53a84f72cf21256f2aa86a8 to your computer and use it in GitHub Desktop.
Save kitos9112/70faa4b7e53a84f72cf21256f2aa86a8 to your computer and use it in GitHub Desktop.
Bash script that automatically updates to latest version of Terraform and TFlint
#!/bin/bash
###########################################################################
# Fetch and install the latest Terraform and tflint version
# curl -Ls https://gist.github.com/kitos9112/70faa4b7e53a84f72cf21256f2aa86a8/raw/get-terraform.sh | bash
##########################################################################
function get_latest_github_release {
curl -s https://api.github.com/repos/$1/$2/releases/latest | grep -oP '"tag_name": "[v]\K(.*)(?=")'
}
function get_latest_github_tag {
curl -s https://api.github.com/repos/$1/$2/tags | grep -m1 -oP '"name": "\K(.*)(?=")'
}
INSTALL_DIR='/usr/local/bin'
REGEX="^PATH=.*\K$INSTALL_DIR(?=.*)"
if [[ $(env | grep -oP $REGEX) != $INSTALL_DIR ]]; then
echo "Looks like your install directory $INSTALL_DIR is not in your PATH. Make sure you export it"; fi
INSTALLED_TERRAFORM_VERSION=$(terraform --version | grep -Poie '\d+.\d+.\d+')
INSTALLED_TFLINT_VERSION=v$(tflint --version | awk '{if (NR == 1 ) print $3}')
LATEST_TERRAFORM_RELEASE=$(get_latest_github_release hashicorp terraform)
LATEST_TFLINT_TAG=$(get_latest_github_tag terraform-linters tflint)
# Download and install Terraform if there is a version mismatch
if [[ ${LATEST_TERRAFORM_RELEASE} != ${INSTALLED_TERRAFORM_VERSION} ]]; then
echo "Installing Terraform ${LATEST_RELEASE}..."
wget https://releases.hashicorp.com/terraform/${LATEST_TERRAFORM_RELEASE}/terraform_${LATEST_TERRAFORM_RELEASE}_linux_amd64.zip
unzip -o terraform_${LATEST_TERRAFORM_RELEASE}_linux_amd64.zip
rm terraform_${LATEST_TERRAFORM_RELEASE}_linux_amd64.zip
sudo install terraform ${INSTALL_DIR}/terraform
rm -rf terraform
else
echo "Nothing done. Latest Terraform already installed."
fi
# Download and install TFlint if there is a version mismatch
if [[ ${LATEST_TFLINT_TAG} != ${INSTALLED_TFLINT_VERSION} ]]; then
echo "Installing Tflint ${LATEST_TFLINT_TAG}..."
wget https://github.com/terraform-linters/tflint/releases/download/${LATEST_TFLINT_TAG}/tflint_linux_amd64.zip
unzip -o tflint_linux_amd64.zip
rm tflint_linux_amd64.zip
sudo install tflint ${INSTALL_DIR}/tflint
rm -rf tflint
else
echo "Nothing done. Latest TFlint already installed."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment