Chore: Upgrade Jenkins-job-builder to 6.3.0
[releng/global-jjb.git] / shell / packer-build.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2017 The Linux Foundation and others.
5 #
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-build.sh"
12 # The script builds an image using packer
13 # $CLOUDENV            :  Provides the cloud credential file.
14 # $PACKER_BUILDER      :  Provides the packer cloud type.
15 # $PACKER_PLATFORM     :  Provides the packer platform.
16 # $PACKER_TEMPLATE     :  Provides the packer template.
17
18 # Ensure we fail the job if any steps fail.
19 set -eu -o pipefail
20
21 # Functions to compare semantic versions x.y.z
22 version_ge() { test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" == "$1"; }
23
24 PACKER_LOGS_DIR="$WORKSPACE/archives/packer"
25 PACKER_BUILD_LOG="$PACKER_LOGS_DIR/packer-build.log"
26 mkdir -p "$PACKER_LOGS_DIR"
27 export PATH="${WORKSPACE}/bin:$PATH"
28 template_file="${template_file:-}"
29
30 cd packer
31
32 # Pick the correct format (hcl or json) based on packer version
33 # Prioritize the project's own version of vars if available
34 if version_ge "$PACKER_VERSION" "1.9.0"; then
35     platform_file="common-packer/vars/$PACKER_PLATFORM.pkrvars.hcl"
36     template_file="templates/$PACKER_TEMPLATE.pkr.hcl"
37     only="${PACKER_BUILDER}.${PACKER_TEMPLATE}"
38
39     if [[ -f "vars/$PACKER_PLATFORM.pkrvars.hcl" ]]; then
40         platform_file="vars/$PACKER_PLATFORM.pkrvars.hcl"
41     fi
42 else
43     platform_file="common-packer/vars/$PACKER_PLATFORM.json"
44     template_file="templates/$PACKER_TEMPLATE.json"
45     only="${PACKER_BUILDER}"
46
47     if [[ -f "vars/$PACKER_PLATFORM.json" ]]; then
48         platform_file="vars/$PACKER_PLATFORM.json"
49     fi
50 fi
51
52 export PACKER_LOG="yes"
53 export PACKER_LOG_PATH="$PACKER_BUILD_LOG"
54
55 # download plugins only for HCL format
56 if [[ "${template_file#*.}" == "pkr.hcl" ]]; then
57     echo "packer init ${template_file} ..."
58     packer.io init "${template_file}"
59 fi
60
61 packer.io validate \
62     -var-file="$CLOUDENV" \
63     -var-file="$platform_file" \
64     "$template_file"
65
66 set -x
67 # If this is a Gerrit system, check patch comments for successful verify build.
68 if [[ -n ${GERRIT_URL:-} ]] && \
69    [[ -n ${GERRIT_CHANGE_NUMBER:-} ]] && \
70    [[ -n ${GERRIT_PATCHSET_NUMBER:-} ]] && \
71    curl -s "${GERRIT_URL}/changes/${GERRIT_CHANGE_NUMBER}/detail" \
72    | tail -n +2 | jq .messages[].message? \
73    | grep "Patch Set ${GERRIT_PATCHSET_NUMBER}:.*Build Successful.*verify-build-${PACKER_PLATFORM}-${PACKER_TEMPLATE}"
74 then
75     echo "Build already successful for this patch set. Skipping merge build..."
76     exit
77 # If this is Github, check the last non-merge commit for a successful Packer
78 # Verify Build status.
79 elif [[ "${GIT_BASE:-}" =~ https://github.com ]]; then
80     LAST_CHANGE_SHA=$(git log --no-merges -1 --format=%H)
81     API_BASE=$(echo "$GIT_BASE" | sed -E 's#(www.)?github.com#api.github.com/repos#')
82     CONTEXT_VALUE="\"Packer ${PACKER_PLATFORM}-${PACKER_TEMPLATE} Verify Build\""
83     JQ_QUERY=".[] | select(.state == \"success\" and .context == ${CONTEXT_VALUE})"
84     STATUS=$(curl "${API_BASE}/statuses/${LAST_CHANGE_SHA}" | jq "${JQ_QUERY}")
85     if [[ -n ${STATUS} ]]; then
86         echo "Build already successful for this patch set. Skipping merge build..."
87         exit
88     fi
89 fi
90 set +x
91
92 packer.io build -color=false \
93     -only "$only" \
94     -var-file="$CLOUDENV" \
95     -var-file="$platform_file" \
96     "$template_file"
97
98 # Extract image name from log and store value in the downstream job
99 if [[ ${UPDATE_CLOUD_IMAGE} == 'true' ]]; then
100
101     NEW_IMAGE_NAME=$(grep -P '(\s+.*image: )(ZZCI\s+.*\d+-\d+\.\d+)' \
102                           "$PACKER_BUILD_LOG" | awk -F': ' '{print $4}')
103
104     echo NEW_IMAGE_NAME="$NEW_IMAGE_NAME" >> "$WORKSPACE/variables.prop"
105     echo "NEW_IMAGE_NAME: ${NEW_IMAGE_NAME}"
106
107     # Copy variables.prop to variables.jenkins-trigger so that the end of build
108     # trigger can pick up the file as input for triggering downstream jobs.
109     # Dont tigger downstream job when UPDATE_CLOUD_IMAGE is set to 'false'
110     cp "$WORKSPACE/variables.prop" "$WORKSPACE/variables.jenkins-trigger"
111 fi
112
113 # Retrive the list of cloud providers
114 mapfile -t clouds < <(jq -r '.builders[].name' "templates/$PACKER_TEMPLATE.json")
115
116 # Split public/private clouds logs
117 for cloud in "${clouds[@]}"; do
118     grep -e "$cloud" "$PACKER_BUILD_LOG" > "$PACKER_LOGS_DIR/packer-build_$cloud.log" 2>&1
119 done