From f908fce693719f2fcb077cccaa3169f66a610100 Mon Sep 17 00:00:00 2001 From: magodo Date: Fri, 22 Apr 2022 02:08:08 +0800 Subject: [PATCH] CI: `validate-example` uses local built provider (#16388) --- GNUmakefile | 2 +- scripts/validate-examples.sh | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/GNUmakefile b/GNUmakefile index 0e2c9e6f392f..d92c31760291 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -129,7 +129,7 @@ teamcity-test: @$(MAKE) -C .teamcity tools @$(MAKE) -C .teamcity test -validate-examples: +validate-examples: build ./scripts/validate-examples.sh pr-check: generate build test lint tflint website-lint diff --git a/scripts/validate-examples.sh b/scripts/validate-examples.sh index 3e86bdf1cc48..00f244467103 100755 --- a/scripts/validate-examples.sh +++ b/scripts/validate-examples.sh @@ -6,13 +6,28 @@ exampleDirs=$(find ./examples -mindepth 2 -maxdepth 2 -type d '!' -exec test -e examplesWithErrors=() hasError=false +# Setup a local Terraform config file for setting up the dev_overrides for this provider. +terraformrc=$(mktemp) +cat << EOF > $terraformrc +provider_installation { + dev_overrides { + "hashicorp/azurerm" = "$(go env GOPATH)/bin" + } + direct {} +} +EOF + # first check each example validates via `terraform validate`.. for d in $exampleDirs; do echo "Validating $d.." exampleHasErrors=false - terraform -chdir=$d init > /dev/null || exampleHasErrors=true + # Though we are using the local built azurerm provider to validate example, + # we still need to call `terraform init` here as examples might contain other providers. + TF_CLI_CONFIG_FILE=$terraformrc terraform -chdir=$d init > /dev/null || exampleHasErrors=true if ! ${exampleHasErrors}; then - terraform -chdir=$d validate > /dev/null || exampleHasErrors=true + # Always use the local built azurerm provider to validate examples, to avoid examples using + # unreleased features leading to error during CI. + TF_CLI_CONFIG_FILE=$terraformrc terraform -chdir=$d validate > /dev/null || exampleHasErrors=true fi if ${exampleHasErrors}; then examplesWithErrors[${#examplesWithErrors[@]}]=$d