e50ec453c0d791b3401413e8df4313ef900878e1
[releng/builder.git] / jjb / autorelease / include-raw-autorelease-notify-build-failure.sh
1 #!/bin/bash -x
2 # @License EPL-1.0 <http://spdx.org/licenses/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
12 RELEASE_EMAIL="release@lists.opendaylight.org"
13 ARCHIVES_DIR="$JENKINS_HOSTNAME/$JOB_NAME/$BUILD_NUMBER"
14 CONSOLE_LOG="/tmp/autorelease-build.log"
15 STREAM=${JOB_NAME#*-*e-}
16
17 # get console logs
18 wget -O "$CONSOLE_LOG" "${BUILD_URL}consoleText"
19
20 # TODO: This section is still required since some of the projects use
21 # description. Remove this section when the reactor info is more consistant.
22 # extract failing project from reactor information
23 REACTOR_INFO=$(awk '/Reactor Summary:/ { flag=1 }
24           flag {
25              if ( sub(/^\[(INFO)\]/,"") && sub(/FAILURE \[.*/,"") ) {
26                  gsub(/[[:space:]]*::[[:space:]]*/,"::")
27                  gsub(/^[[:space:]]+|[[:space:]]+$|[.]/,"")
28                  print
29              }
30           }
31           /Final Memory:/ { flag=0 }' $CONSOLE_LOG)
32
33 # check for project format
34 if [[ ${REACTOR_INFO} =~ .*::*.*::*. ]]; then
35     # extract project and artifactId from full format
36     ODL=$(echo "${REACTOR_INFO}" | awk -F'::' '{ gsub(/^[ \t]+|[ \t]+$/, "", $1); print $1 }')
37     PROJECT_=$(echo "${REACTOR_INFO}" | awk -F'::' '{ gsub(/^[ \t]+|[ \t]+$/, "", $2); print $2 }')
38     NAME=$(echo "${REACTOR_INFO}" | awk -F'::' '{ gsub(/^[ \t]+|[ \t]+$/, "", $3); print $3 }')
39 else
40     # set ARTIFACTID to partial format
41     ODL=""
42     PROJECT_=""
43     NAME=$(echo "${REACTOR_INFO}" | awk '{ gsub(/^[ \t]+|[ \t]+$/, ""); print }')
44 fi
45
46
47 # determine ARTIFACT_ID for project mailing list
48 ARTIFACT_ID=$(awk -F: '/\[ERROR\].*mvn <goals> -rf :/ { print $2}' $CONSOLE_LOG)
49
50 # determine project mailing list using xpaths
51 # if project.groupId:
52 #     project.groupId is set and is not inherited
53 # else if project.parent.groupId:
54 #     project.groupId is not set but IS inherited from project.parent.groupId
55 # else
56 #     exclude project mailing list
57 if [ ! -z "$ARTIFACT_ID" ]; then
58     grouplist=()
59     while IFS="" read -r p; do
60         GROUP=$(xmlstarlet sel\
61               -N "x=http://maven.apache.org/POM/4.0.0"\
62               -t -m "/x:project[x:artifactId='$ARTIFACT_ID']"\
63               --if "/x:project/x:groupId"\
64               -v "/x:project/x:groupId"\
65               --elif "/x:project/x:parent/x:groupId"\
66               -v "/x:project/x:parent/x:groupId"\
67               --else -o ""\
68               "$p" 2>/dev/null)
69         if [ ! -z "${GROUP}" ]; then
70             grouplist+=($(echo "${GROUP}" | awk -F'.' '{ print $3 }'))
71         fi
72     done < <(find . -name "pom.xml")
73
74     if [ "${#grouplist[@]}" -eq 1 ]; then
75         PROJECT="${grouplist[0]}"
76     elif [ "${#grouplist[@]}" -gt 1 ]; then
77         GROUPLIST="NOTE: The artifactId: $ARTIFACT_ID matches multiple groups: ${grouplist[*]}"
78     else
79         echo "Failed to determine project.groupId using xpaths"
80     fi
81 else
82     echo "Failed to determine ARTIFACT_ID"
83     exit 1
84 fi
85
86 # Construct email subject & body
87 PROJECT_STRING=${PROJECT:+" from $PROJECT"}
88 SUBJECT="[release] Autorelease $STREAM failed to build $ARTIFACT_ID$PROJECT_STRING"
89 BODY="Attention ${PROJECT:-"OpenDaylight"}-devs,
90
91 Autorelease $STREAM failed to build $ARTIFACT_ID$PROJECT_STRING in build
92 $BUILD_NUMBER. Attached is a snippet of the error message related to the
93 failure that we were able to automatically parse as well as console logs.
94 ${PROJECT:+"$GROUPLIST"}
95
96 Console Logs:
97 https://logs.opendaylight.org/$SILO/$ARCHIVES_DIR
98
99 Jenkins Build:
100 $BUILD_URL
101
102 Please review and provide an ETA on when a fix will be available.
103
104 Thanks,
105 ODL releng/autorelease team
106 "
107
108 # check if remote staging is complete successfully
109 BUILD_STATUS=$(awk '/\[INFO\] Remote staging finished/{flag=1;next}/Total time:/{flag=0}flag' $CONSOLE_LOG \
110                    | grep '\] BUILD' | awk '{print $3}')
111
112 if ([ ! -z "${NAME}" ] || [ ! -z "${ARTIFACT_ID}" ]) && [[ "${BUILD_STATUS}" != "SUCCESS" ]]; then
113     # project search pattern should handle both scenarios
114     # 1. Full format:    ODL :: $PROJECT :: $ARTIFACT_ID
115     # 2. Partial format: Building $ARTIFACT_ID
116     sed -e "/\[INFO\] Building \(${NAME} \|${ARTIFACT_ID} \|${ODL} :: ${PROJECT_} :: ${NAME} \)/,/Reactor Summary:/!d;//d" \
117           $CONSOLE_LOG > /tmp/error.txt
118
119     if [ -n "${PROJECT}" ]; then
120         RELEASE_EMAIL="${RELEASE_EMAIL}, ${PROJECT}-dev@lists.opendaylight.org"
121     fi
122
123     # Only send emails in production (releng), not testing (sandbox)
124     if [ "${SILO}" == "releng" ]; then
125         echo "${BODY}" | mail -a /tmp/error.txt \
126             -r "Jenkins <jenkins-dontreply@opendaylight.org>" \
127             -s "${SUBJECT}" "${RELEASE_EMAIL}"
128     elif [ "${SILO}" == "sandbox" ]; then
129         echo "Running in sandbox, not actually sending notification emails"
130         echo "Subject: ${SUBJECT}"
131         echo "Body: ${BODY}"
132     else
133         echo "Not sure how to notify in \"${SILO}\" Jenkins silo"
134     fi
135 fi
136
137 rm $CONSOLE_LOG