Pass the builder type to verify jobs
[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_PLATFORM     :  Provides the packer platform.
15 # $PACKER_TEMPLATE     :  Provides the packer temnplate.
16
17 # Ensure we fail the job if any steps fail.
18 set -eu -o pipefail
19
20 PACKER_LOGS_DIR="$WORKSPACE/archives/packer"
21 PACKER_BUILD_LOG="$PACKER_LOGS_DIR/packer-build.log"
22 mkdir -p "$PACKER_LOGS_DIR"
23 export PATH="${WORKSPACE}/bin:$PATH"
24 export PACKER_BUILDER=${PACKER_BUILDER:-openstack}
25
26 cd packer
27
28 # Prioritize the project's own version of vars if available
29 platform_file="common-packer/vars/$PACKER_PLATFORM.json"
30 if [[ -f "vars/$PACKER_PLATFORM.json" ]]; then
31     platform_file="vars/$PACKER_PLATFORM.json"
32 fi
33
34 export PACKER_LOG="yes"
35 export PACKER_LOG_PATH="$PACKER_BUILD_LOG"
36 packer.io validate \
37     -var-file="$CLOUDENV" \
38     -var-file="$platform_file" \
39     "templates/$PACKER_TEMPLATE.json"
40
41 set -x
42 # If this is a Gerrit system, check patch comments for successful verify build.
43 if [[ -n ${GERRIT_URL:-} ]] && \
44    [[ -n ${GERRIT_CHANGE_NUMBER:-} ]] && \
45    [[ -n ${GERRIT_PATCHSET_NUMBER:-} ]] && \
46    curl -s "${GERRIT_URL}/changes/${GERRIT_CHANGE_NUMBER}/detail" \
47    | tail -n +2 | jq .messages[].message? \
48    | grep "Patch Set ${GERRIT_PATCHSET_NUMBER}:.*Build Successful.*verify-build-${PACKER_PLATFORM}-${PACKER_TEMPLATE}"
49 then
50     echo "Build already successful for this patch set. Skipping merge build..."
51     exit
52 # If this is Github, check the last non-merge commit for a successful Packer
53 # Verify Build status.
54 elif [[ "${GIT_BASE:-}" =~ https://github.com ]]; then
55     LAST_CHANGE_SHA=$(git log --no-merges -1 --format=%H)
56     API_BASE=$(echo "$GIT_BASE" | sed -E 's#(www.)?github.com#api.github.com/repos#')
57     STATUS=$(curl "${API_BASE}/statuses/${LAST_CHANGE_SHA}" \
58         | jq ".[] | select(.state == \"success\" and .context == \"Packer ${PACKER_PLATFORM}-${PACKER_TEMPLATE} Verify Build\")")
59     if [[ -n ${STATUS} ]]; then
60         echo "Build already successful for this patch set. Skipping merge build..."
61         exit
62     fi
63 fi
64 set +x
65
66 packer.io build -color=false \
67     -var-file="$CLOUDENV" \
68     -var-file="$platform_file" \
69     "templates/$PACKER_TEMPLATE.json"
70
71 # Extract image name from log and store value in the downstream job
72 if [[ ${UPDATE_CLOUD_IMAGE} == 'true' ]]; then
73
74     NEW_IMAGE_NAME=$(grep -P '(\s+.*image: )(ZZCI\s+.*\d+-\d+\.\d+)' \
75                           "$PACKER_BUILD_LOG" | awk -F': ' '{print $4}')
76
77     echo NEW_IMAGE_NAME="$NEW_IMAGE_NAME" >> "$WORKSPACE/variables.prop"
78     echo "NEW_IMAGE_NAME: ${NEW_IMAGE_NAME}"
79
80     # Copy variables.prop to variables.jenkins-trigger so that the end of build
81     # trigger can pick up the file as input for triggering downstream jobs.
82     # Dont tigger downstream job when UPDATE_CLOUD_IMAGE is set to 'false'
83     cp "$WORKSPACE/variables.prop" "$WORKSPACE/variables.jenkins-trigger"
84 fi
85
86 # Retrive the list of cloud providers
87 mapfile -t clouds < <(jq -r '.builders[].name' "templates/$PACKER_TEMPLATE.json")
88
89 # Split public/private clouds logs
90 for cloud in "${clouds[@]}"; do
91     grep -e "$cloud" "$PACKER_BUILD_LOG" > "$PACKER_LOGS_DIR/packer-build_$cloud.log" 2>&1
92 done