X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=blobdiff_plain;f=shell%2Fpacker-build.sh;h=7dd2434b67379aba57288f48615395f719f1927c;hb=bafd57c92d85a6c8b2c3fd3dbe30aa3a09f8f620;hp=4cb3df32a90dc71e130116c9ec8b2d3a9c108829;hpb=048aff89d28864363ba051c7624d280368e362d3;p=releng%2Fglobal-jjb.git diff --git a/shell/packer-build.sh b/shell/packer-build.sh index 4cb3df32..7dd2434b 100644 --- a/shell/packer-build.sh +++ b/shell/packer-build.sh @@ -11,32 +11,57 @@ echo "---> packer-build.sh" # The script builds an image using packer # $CLOUDENV : Provides the cloud credential file. +# $PACKER_BUILDER : Provides the packer cloud type. # $PACKER_PLATFORM : Provides the packer platform. -# $PACKER_TEMPLATE : Provides the packer temnplate. +# $PACKER_TEMPLATE : Provides the packer template. # 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" PACKER_BUILD_LOG="$PACKER_LOGS_DIR/packer-build.log" mkdir -p "$PACKER_LOGS_DIR" export PATH="${WORKSPACE}/bin:$PATH" -export PACKER_BUILDER=${PACKER_BUILDER:-openstack} +template_file="${template_file:-}" cd packer +# Pick the correct format (hcl or json) based on packer version # Prioritize the project's own version of vars if available -platform_file="common-packer/vars/$PACKER_PLATFORM.json" -if [[ -f "vars/$PACKER_PLATFORM.json" ]]; then - platform_file="vars/$PACKER_PLATFORM.json" +if version_ge "$PACKER_VERSION" "1.9.0"; then + platform_file="common-packer/vars/$PACKER_PLATFORM.pkrvars.hcl" + template_file="templates/$PACKER_TEMPLATE.pkr.hcl" + only="${PACKER_BUILDER}.${PACKER_TEMPLATE}" + + if [[ -f "vars/$PACKER_PLATFORM.pkrvars.hcl" ]]; then + platform_file="vars/$PACKER_PLATFORM.pkrvars.hcl" + fi +else + platform_file="common-packer/vars/$PACKER_PLATFORM.json" + template_file="templates/$PACKER_TEMPLATE.json" + only="${PACKER_BUILDER}" + + if [[ -f "vars/$PACKER_PLATFORM.json" ]]; then + platform_file="vars/$PACKER_PLATFORM.json" + fi fi export PACKER_LOG="yes" export PACKER_LOG_PATH="$PACKER_BUILD_LOG" + +# download plugins only for HCL format +if [[ "${template_file#*.}" == "pkr.hcl" ]]; then + echo "packer init ${template_file} ..." + packer.io init "${template_file}" +fi + packer.io validate \ -var-file="$CLOUDENV" \ -var-file="$platform_file" \ - "templates/$PACKER_TEMPLATE.json" + "$template_file" set -x # If this is a Gerrit system, check patch comments for successful verify build. @@ -54,8 +79,9 @@ then elif [[ "${GIT_BASE:-}" =~ https://github.com ]]; then LAST_CHANGE_SHA=$(git log --no-merges -1 --format=%H) API_BASE=$(echo "$GIT_BASE" | sed -E 's#(www.)?github.com#api.github.com/repos#') - STATUS=$(curl "${API_BASE}/statuses/${LAST_CHANGE_SHA}" \ - | jq ".[] | select(.state == \"success\" and .context == \"Packer ${PACKER_PLATFORM}-${PACKER_TEMPLATE} Verify Build\")") + CONTEXT_VALUE="\"Packer ${PACKER_PLATFORM}-${PACKER_TEMPLATE} Verify Build\"" + JQ_QUERY=".[] | select(.state == \"success\" and .context == ${CONTEXT_VALUE})" + STATUS=$(curl "${API_BASE}/statuses/${LAST_CHANGE_SHA}" | jq "${JQ_QUERY}") if [[ -n ${STATUS} ]]; then echo "Build already successful for this patch set. Skipping merge build..." exit @@ -64,9 +90,10 @@ fi set +x packer.io build -color=false \ + -only "$only" \ -var-file="$CLOUDENV" \ -var-file="$platform_file" \ - "templates/$PACKER_TEMPLATE.json" + "$template_file" # Extract image name from log and store value in the downstream job if [[ ${UPDATE_CLOUD_IMAGE} == 'true' ]]; then