Feat: Rewrite packer jobs to work with hcl2 format
[releng/global-jjb.git] / shell / packer-validate.sh
index 61e1431..86cf8de 100644 (file)
@@ -16,24 +16,47 @@ echo "---> packer-validate.sh"
 # Ensure we fail the job if any steps fail.
 set -eu -o pipefail
 
+# Functions to compare semantic versions x.y.z
+version_ge() { test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" == "$1"; }
+
 PACKER_LOGS_DIR="$WORKSPACE/archives/packer"
 mkdir -p "$PACKER_LOGS_DIR"
 export PATH="${WORKSPACE}/bin:$PATH"
-
+set -x
 cd packer
-varfiles=(vars/*.json common-packer/vars/*.json)
-templates=(templates/*.json)
+
+if version_ge "$PACKER_VERSION" "1.9.0"; then
+    varfiles=(vars/*.pkrvars.hcl common-packer/vars/*.pkrvars.hcl)
+    templates=(templates/*.pkr.hcl)
+else
+    varfiles=(vars/*.json common-packer/vars/*.json)
+    templates=(templates/*.json)
+fi
 
 for varfile in "${varfiles[@]}"; do
-    # cloud-env.json is a file containing credentials which is pulled in via
-    # CLOUDENV variable so skip it here. Also handle the case where a project
-    # has not vars/*.json file.
-    if [[ "$varfile" == *"cloud-env.json"* ]] || [[ "$varfile" == 'vars/*.json' ]]; then
+    # cloud-env.{json,pkrvars.hcl} is a file containing credentials which is
+    # pulled in via CLOUDENV variable so skip it here. Also handle case
+    # where a project does not vars/*.{json,pkrvars.hcl} file.
+    if [[ "$varfile" == *"cloud-env.json"* ]] || \
+       [[ "$varfile" == "vars/*.json" ]] || \
+       [[ "$varfile" == *"cloud-env.pkrvars.hcl"* ]] || \
+       [[ "$varfile" == *"cloud-env-aws.pkrvars.hcl"* ]] || \
+       [[ "$varfile" == "vars/*.pkrvars.hcl" ]]; then
         continue
     fi
 
     echo "-----> Testing varfile: $varfile"
     for template in "${templates[@]}"; do
+        if [[ "$template" == *"variables.pkr.hcl"* ]] || \
+           [[ "$template" == *"variables.auto.pkr.hcl"* ]]; then
+            continue
+        fi
+
+        if [[ "${template#*.}" == "pkr.hcl" ]]; then
+            echo "packer init $template ..."
+            packer.io init "$template"
+        fi
+
         export PACKER_LOG="yes"
         export PACKER_LOG_PATH="$PACKER_LOGS_DIR/packer-validate-${varfile##*/}-${template##*/}.log"
         if output=$(packer.io validate -var-file="$CLOUDENV" -var-file="$varfile" "$template"); then