@@ -30,6 +30,46 @@ copy_terraform_to_mirror() {
3030 cp " ${PWD} /terraform/bin/${TARGET_OS_ARCH} /terraform" " ${PWD} /pkg/terraform/providers/mirror/terraform/"
3131}
3232
33+ # Check if a provider has changed based on the version stored in the binary
34+ check_module_changes () {
35+ binpath=" $1 "
36+ srcpath=" $2 "
37+ build_hash=" $( go version -m " ${binpath} " | grep ' vcs.revision' | cut -f2 -d' =' ) "
38+ # If it's a locally developed provider
39+ if test -n " ${build_hash} " ; then
40+ # Check if a provider has changed based on git changes since the
41+ # revision the provider was last built
42+ git diff --name-only --exit-code " ${build_hash} .." -- " ${srcpath} "
43+ # If it's an imported module
44+ else
45+ # Check if a provider has changed based on its go.mod git hash
46+ version_info=" $( go version -m " ${binpath} " | grep -Eo ' main.builtGoModHash=[a-fA-F0-9]+' | cut -f2 -d' =' ) "
47+ test " ${version_info} " == " $( git hash-object " ${srcpath} /go.mod" ) "
48+ fi
49+ }
50+
51+ # Build terraform and providers only if needed
52+ build_terraform_and_providers () {
53+ TARGET_OS_ARCH=$( go env GOOS) _$( go env GOARCH)
54+ bindir=" ${PWD} /terraform/bin/${TARGET_OS_ARCH} "
55+ find " ${PWD} /terraform/providers/" -maxdepth 1 -mindepth 1 -type d | while read -r dir; do
56+ provider=" $( basename " ${dir} " ) "
57+ binpath=" ${bindir} /terraform-provider-${provider} "
58+ if test -s " ${binpath} " && test -s " ${binpath} .zip" && check_module_changes " ${binpath} " " terraform/providers/${provider} " ; then
59+ echo " ${provider} is up-to-date"
60+ else
61+ echo " Rebuilding ${provider} "
62+ make -C terraform " go-build.${provider} "
63+ fi
64+ done
65+ if test -s " ${bindir} /terraform" && check_module_changes " ${bindir} /terraform" " terraform/terraform" ; then
66+ echo " terraform is up-to-date"
67+ else
68+ echo " Rebuilding terraform"
69+ make -C terraform go-build-terraform
70+ fi
71+ }
72+
3373minimum_go_version=1.20
3474current_go_version=$( go version | cut -d " " -f 3)
3575
@@ -44,8 +84,8 @@ MODE="${MODE:-release}"
4484# Build terraform binaries before setting environment variables since it messes up make
4585if test " ${SKIP_TERRAFORM} " ! = y && ! (echo " ${TAGS} " | grep -q -e ' aro' -e ' altinfra' )
4686then
47- make -C terraform all
48- copy_terraform_to_mirror # Copy terraform parts to embedded mirror.
87+ build_terraform_and_providers
88+ copy_terraform_to_mirror # Copy terraform parts to embedded mirror.
4989fi
5090
5191# build cluster-api binaries
0 commit comments