Skip to content

Instantly share code, notes, and snippets.

@devdrops
Created October 9, 2020 19:11
Show Gist options
  • Save devdrops/5b367b52542787af66e9e48a5c389a53 to your computer and use it in GitHub Desktop.
Save devdrops/5b367b52542787af66e9e48a5c389a53 to your computer and use it in GitHub Desktop.
TF Diff - Checando diferenças nos arquivos terraform

TF Diff - Checando diferenças nos arquivos terraform

Trabalha com Terraform?

Costuma reforçar o uso de terraform fmt nos Pull Requests do seu time?

Cansou de validar se os arquivos do Pull Request seguem corretamenta a sintaxe?

SEUS PROBLEMAS ACABARAM!

Esse script em bash, ao ser executado, valida quais arquivos foram adicionados e/ou modificados, dizendo quais devem ser verificados.

Requisitos

  • Docker
  • Bash
  • Git
#!/bin/bash
echo ""
echo " ======================================================================== "
echo " : ████████ ███████ ██████ ██ ███████ ███████ : "
echo " : ██ ██ ██ ██ ██ ██ ██ : "
echo " : ██ █████ ██ ██ ██ █████ █████ : "
echo " : ██ ██ ██ ██ ██ ██ ██ : "
echo " : ██ ██ ██████ ██ ██ ██ : "
echo " ======================================================================== "
echo ""
echo " Check differences with terraform fmt on all changed .tf files."
echo ""
echo ""
LIST=./list.txt
if [[ -z "${BASE_BRANCH}" ]]; then
export BASE_BRANCH="master"
fi
if [[ -z "${TF_VERSION}" ]]; then
export TF_VERSION="0.11.14"
fi
echo " === Upgrading repository references ====================================>"
echo ""
git fetch --all > /dev/null 2>&1
git diff origin/$BASE_BRANCH...$1 --name-only --diff-filter=AM | grep .tf > "$LIST"
echo " === Target Branch: $1"
echo " === Base Branch: $BASE_BRANCH"
echo " === Terraform Version: $TF_VERSION"
echo " === Files to check:"
cat "$LIST"
echo ""
echo " === Checking files =====================================================>"
while IFS= read -r file; do
docker run --rm -v $(pwd):/code -w /code hashicorp/terraform:${TF_VERSION} fmt -check=true -list=false ./"$file"
if [ $? = 0 ]; then
printf " = OK: $file\n"
else
printf " = FAILED: $file\n"
break
fi
done < "$LIST"
rm "$LIST"
echo ""
echo " === Done! ==============================================================>"
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment