Remove the -only CLI option for packer build
[releng/global-jjb.git] / shell / packer-build.sh
index b015271..1a44e4f 100644 (file)
@@ -36,13 +36,39 @@ packer.io validate \
     -var-file="$CLOUDENV" \
     -var-file="$platform_file" \
     "templates/$PACKER_TEMPLATE.json"
+
+set -x
+# If this is a Gerrit system, check patch comments for successful verify build.
+if [[ -n ${GERRIT_URL:-} ]] && \
+   [[ -n ${GERRIT_CHANGE_NUMBER:-} ]] && \
+   [[ -n ${GERRIT_PATCHSET_NUMBER:-} ]] && \
+   curl -s "${GERRIT_URL}/changes/${GERRIT_CHANGE_NUMBER}/detail" \
+   | tail -n +2 | jq .messages[].message? \
+   | grep "Patch Set ${GERRIT_PATCHSET_NUMBER}:.*Build Successful.*verify-build-${PACKER_PLATFORM}-${PACKER_TEMPLATE}"
+then
+    echo "Build already successful for this patch set. Skipping merge build..."
+    exit
+# If this is Github, check the last non-merge commit for a successful Packer
+# Verify Build status.
+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\")")
+    if [[ -n ${STATUS} ]]; then
+        echo "Build already successful for this patch set. Skipping merge build..."
+        exit
+    fi
+fi
+set +x
+
 packer.io build -color=false \
     -var-file="$CLOUDENV" \
     -var-file="$platform_file" \
     "templates/$PACKER_TEMPLATE.json"
 
 # Extract image name from log and store value in the downstream job
-if [[ ${UPDATE_CLOUD_IMAGE} ]]; then
+if [[ ${UPDATE_CLOUD_IMAGE} == 'true' ]]; then
 
     NEW_IMAGE_NAME=$(grep -P '(\s+.*image: )(ZZCI\s+.*\d+-\d+\.\d+)' \
                           "$PACKER_BUILD_LOG" | awk -F': ' '{print $4}')
@@ -53,7 +79,7 @@ if [[ ${UPDATE_CLOUD_IMAGE} ]]; then
     # Copy variables.prop to variables.jenkins-trigger so that the end of build
     # trigger can pick up the file as input for triggering downstream jobs.
     # Dont tigger downstream job when UPDATE_CLOUD_IMAGE is set to 'false'
-    cp $WORKSPACE/variables.prop $WORKSPACE/variables.jenkins-trigger
+    cp "$WORKSPACE/variables.prop" "$WORKSPACE/variables.jenkins-trigger"
 fi
 
 # Retrive the list of cloud providers