2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2017 The Linux Foundation and others.
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11 echo "---> packer-validate.sh"
12 # The script validates an packers files.
14 # $CLOUDENV : Provides the cloud credential file.
16 # Ensure we fail the job if any steps fail.
19 # Functions to compare semantic versions x.y.z
20 version_ge() { test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" == "$1"; }
22 PACKER_LOGS_DIR="$WORKSPACE/archives/packer"
23 mkdir -p "$PACKER_LOGS_DIR"
24 export PATH="${WORKSPACE}/bin:$PATH"
28 if version_ge "$PACKER_VERSION" "1.9.0"; then
29 varfiles=(vars/*.pkrvars.hcl common-packer/vars/*.pkrvars.hcl)
30 templates=(templates/*.pkr.hcl)
32 varfiles=(vars/*.json common-packer/vars/*.json)
33 templates=(templates/*.json)
36 for varfile in "${varfiles[@]}"; do
37 # cloud-env.{json,pkrvars.hcl} is a file containing credentials which is
38 # pulled in via CLOUDENV variable so skip it here. Also handle case
39 # where a project does not vars/*.{json,pkrvars.hcl} file.
40 if [[ "$varfile" == *"cloud-env.json"* ]] || \
41 [[ "$varfile" == "vars/*.json" ]] || \
42 [[ "$varfile" == *"cloud-env.pkrvars.hcl"* ]] || \
43 [[ "$varfile" == *"cloud-env-aws.pkrvars.hcl"* ]] || \
44 [[ "$varfile" == "vars/*.pkrvars.hcl" ]]; then
48 echo "-----> Testing varfile: $varfile"
49 for template in "${templates[@]}"; do
50 if [[ "$template" == *"variables.pkr.hcl"* ]] || \
51 [[ "$template" == *"variables.auto.pkr.hcl"* ]]; then
55 if [[ "${template#*.}" == "pkr.hcl" ]]; then
56 echo "packer init $template ..."
57 packer.io init "$template"
60 export PACKER_LOG="yes"
61 export PACKER_LOG_PATH="$PACKER_LOGS_DIR/packer-validate-${varfile##*/}-${template##*/}.log"
62 if output=$(packer.io validate -var-file="$CLOUDENV" -var-file="$varfile" "$template"); then
63 echo "$template: $output"
65 echo "$template: $output"