Merge changes from topic "shellcheck"
[releng/builder.git] / jjb / integration / multipatch-distribution.sh
1 #!/bin/bash
2
3 set -e
4
5 # create a fresh empty place to build this custom distribution
6 BUILD_DIR=${WORKSPACE}/patch_tester
7 DISTRIBUTION_BRANCH_TO_BUILD=$DISTROBRANCH  #renaming variable for clarity
8 MAVEN_OPTIONS="${MAVEN_PARAMS} \
9     --show-version \
10     --batch-mode \
11     -Djenkins \
12     -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn \
13     -Dmaven.repo.local=/tmp/r \
14     -Dorg.ops4j.pax.url.mvn.localRepository=/tmp/r"
15
16 rm -rf "$BUILD_DIR"
17 mkdir -p "$BUILD_DIR"
18 cd "$BUILD_DIR"
19
20 # Download distribution pom.xml
21 wget "http://git.opendaylight.org/gerrit/gitweb?p=integration/distribution.git;a=blob_plain;f=artifacts/upstream/properties/pom.xml;hb=refs/heads/$DISTROBRANCH" -O "pom.xml"
22 cat pom.xml
23
24 # Set up git committer name and email, needed for commit creation when cherry-picking.
25 export EMAIL="sandbox@jenkins.opendaylight.org"
26 export GIT_COMMITTER_NAME="Multipatch Job"
27
28 # Extract a list of patches per project from an comment trigger. An example is:
29 # multipatch-build:openflowplugin:45/69445/1,genius:46/69446/1,netvirt:47/69447/1
30 if [ -n "$GERRIT_EVENT_COMMENT_TEXT" ]; then
31     if [[ "$GERRIT_EVENT_COMMENT_TEXT" == *fast* ]]; then
32         BUILD_FAST="true"
33         PATCHES_TO_BUILD=$(echo "$GERRIT_EVENT_COMMENT_TEXT" | grep 'multipatch-build-fast:')
34     else
35         BUILD_FAST="false"
36         PATCHES_TO_BUILD=$(echo "$GERRIT_EVENT_COMMENT_TEXT" | grep 'multipatch-build:')
37     fi
38     PATCHES_TO_BUILD=${PATCHES_TO_BUILD#*:}
39 fi
40 # check if topic exists:
41 # if topic=binding-rpc, then checkout first patch in binding-rpc topic (if it exists)
42 # if topic:binding-rpc, then cherry-pick first patch in binding-rpc topic (if it exists)
43 if [[ "${PATCHES_TO_BUILD}" == *"topic"* ]]; then
44     if [[ "${PATCHES_TO_BUILD}" == *"topic="* ]]; then
45         CHERRY_PICK="false"
46         TOPIC="${PATCHES_TO_BUILD#*=}"
47     elif [[ "${PATCHES_TO_BUILD}" == *"topic:"* ]]; then
48         CHERRY_PICK="true"
49         TOPIC="${PATCHES_TO_BUILD#*:}"
50     else
51         echo "ERROR: Topic has wrong format" && exit 1
52     fi
53     echo "Create topic ${TOPIC} patch list"
54     PATCHES_TO_BUILD=""
55     read -ra PROJECT_LIST <<< "${BUILD_ORDER}"
56     echo "List of projects to check patch in topic: ${PROJECT_LIST[*]}"
57     for PROJECT in "${PROJECT_LIST[@]}"; do
58         # get all patches number for a topic for a given project
59         IFS=$'\n' read -rd '' -a GERRIT_PATCH_LIST <<< "$(ssh -p 29418 "jenkins-$SILO@git.opendaylight.org" gerrit query status:open "topic:${TOPIC}" "project:${PROJECT}" 2> /dev/null \
60         | grep 'number:' | awk '{{ print $2 }}')" || true
61         # add project if it is the first with patches or it is not the first
62         if [[ -z "${PATCHES_TO_BUILD}" && -n "${GERRIT_PATCH_LIST[*]}" ]]; then
63             PATCHES_TO_BUILD="${PROJECT}"
64         elif [[ -n "${PATCHES_TO_BUILD}" ]]; then
65             if [[ -n "${GERRIT_PATCH_LIST[*]}" ]]; then
66                 echo "Add ${PROJECT}:${DISTRIBUTION_BRANCH_TO_BUILD}"
67             fi
68             PATCHES_TO_BUILD="${PATCHES_TO_BUILD},${PROJECT}"
69         fi
70         # sort project patches
71         if [[ -n "${GERRIT_PATCH_LIST[*]}" ]]; then
72             echo "Add ${PROJECT}:${GERRIT_PATCH_LIST[*]}"
73             REF_LIST=()
74             # create reference list with patch number-refspec
75             for PATCH in "${GERRIT_PATCH_LIST[@]}"; do
76                 REFSPEC=$(ssh -p 29418 "jenkins-$SILO@git.opendaylight.org" gerrit query "change:${PATCH}" --current-patch-set \
77                 | grep 'ref:' | awk '{{ print $2 }}')
78                 REF_LIST+=("${PATCH}-${REFSPEC/refs\/changes\/}")
79             done
80             # sort reference list by patch number
81             IFS=$'\n' SORT_REF=$(sort <<<"${REF_LIST[*]}") && unset IFS
82             read -rd '' -a SORT_REF_LIST <<< "${SORT_REF[*]}" || true
83             # add refspec to patches to build list
84             COUNT=0
85             for PATCH in "${SORT_REF_LIST[@]}"; do
86                 COUNT=$((COUNT+1))
87                 if [ "${COUNT}" == "1" ] && [ "${CHERRY_PICK}" == "false" ]; then
88                     # checkout patch
89                     PATCHES_TO_BUILD="${PATCHES_TO_BUILD}=${PATCH/*-/}"
90                 else
91                     # cherry-pick is better than checkout patch
92                     PATCHES_TO_BUILD="${PATCHES_TO_BUILD}:${PATCH/*-/}"
93                 fi
94             done
95         fi
96     done
97 fi
98 echo "Patches to build: ${PATCHES_TO_BUILD}"
99 IFS=',' read -ra PATCHES <<< "${PATCHES_TO_BUILD}"
100
101 # First phase: clone the necessary repos and set the patches up
102
103 declare -a PROJECTS
104
105 # For each patch:
106 # 1. Clone the project.
107 # 2. Checkout an specific (typically unmerged) Gerrit patch. If none,
108 # use distribution pom.xml file to figure out right branch or tag to checkout.
109 # In case of Gerrit patch in MRI project, adjust version for the stream.
110 # 3. Optionally, cherry-pick series of patches on top of the checkout.
111 #
112 # Each patch is found in the ${PATCHES_TO_BUILD} variable as a comma separated
113 # list of project[=checkout][:cherry-pick]* values. Examples:
114 #
115 # Checkout a (typically unmerged) Gerrit patch on top of odlparent's git clone:
116 # PATCHES_TO_BUILD='odlparent=45/30045/2'
117 #
118 # Checkout patches for both odlparent and yangtools:
119 # PATCHES_TO_BUILD='odlparent=45/30045/2,yangtools:53/26853/25'
120 #
121 # Checkout a patch for controller, cherry-pick another patch on top of it:
122 # PATCHES_TO_BUILD='controller=61/29761/5:45/29645/6'
123 distribution_status="not_included"
124 for patch in "${PATCHES[@]}"
125 do
126     echo "-- working on ${patch} --"
127     # For patch=controller=61/29761/5:45/29645/6, this gives controller.
128     PROJECT="$(echo "${patch}" | cut -d':' -f 1 | cut -d'=' -f 1)"
129     if [ "${PROJECT}" == "integration/distribution" ]; then
130         distribution_status="included"
131     fi
132     PROJECT_SHORTNAME="${PROJECT##*/}"  # http://stackoverflow.com/a/3162500
133     PROJECTS+=("${PROJECT_SHORTNAME}")
134     echo "1. cloning project ${PROJECT}"
135     git clone "https://git.opendaylight.org/gerrit/${PROJECT}"
136     cd "${PROJECT_SHORTNAME}"
137     # For patch = controller=61/29761/5:45/29645/6, this gives 61/29761/5.
138     CHECKOUT="$(echo "${patch}" | cut -d'=' -s -f 2 | cut -d':' -f 1)"
139     # If there is a base patch for this project, checkout patch, otherwise use
140     # distribution pom.xml file to figure out right branch or tag to checkout.
141     if [ "x${CHECKOUT}" != "x" ]; then
142         echo "2. checking out patch ${CHECKOUT}"
143         # TODO: Make this script accept "29645/6" as a shorthand for "45/29645/6".
144         git fetch "https://git.opendaylight.org/gerrit/${PROJECT}" "refs/changes/$CHECKOUT"
145         git checkout FETCH_HEAD
146         # If the patch is for MRI project, adjust the MRI versions
147         # shellcheck disable=SC2235
148         if [ "${PROJECT}" == "odlparent" ] || [ "${PROJECT}" == "yangtools" ] || ([ "${PROJECT}" == "mdsal" ] && [ "${DISTROSTREAM}" != "fluorine" ]); then
149             ODLPARENT_VERSION="$(xmlstarlet sel -N x=http://maven.apache.org/POM/4.0.0 -t -v //x:odlparent.version ../pom.xml)"
150             echo "change odlparent version to ${ODLPARENT_VERSION}"
151             find . -name "pom.xml" -print0 | xargs -0 xmlstarlet ed --inplace -P -N x=http://maven.apache.org/POM/4.0.0 -u //x:version\[../x:parent/x:groupId=\"org.opendaylight.odlparent\"\] -v "${ODLPARENT_VERSION}"
152             find . -name "pom.xml" -print0 | xargs -0 xmlstarlet ed --inplace -P -N x=http://maven.apache.org/POM/4.0.0 -u //x:version\[../x:groupId=\"org.opendaylight.odlparent\"\] -v "${ODLPARENT_VERSION}"
153         fi
154         # shellcheck disable=SC2235
155         if [ "${PROJECT}" == "yangtools" ] || ([ "${PROJECT}" == "mdsal" ] && [ "${DISTROSTREAM}" != "fluorine" ]); then
156             YANGTOOLS_VERSION="$(xmlstarlet sel -N x=http://maven.apache.org/POM/4.0.0 -t -v //x:yangtools.version ../pom.xml)"
157             echo "change yangtools version to ${YANGTOOLS_VERSION}"
158             find . -name "pom.xml" -print0 | xargs -0 xmlstarlet ed --inplace -P -N x=http://maven.apache.org/POM/4.0.0 -u //x:version\[../x:parent/x:groupId=\"org.opendaylight.yangtools\"\] -v "${YANGTOOLS_VERSION}"
159             find . -name "pom.xml" -print0 | xargs -0 xmlstarlet ed --inplace -P -N x=http://maven.apache.org/POM/4.0.0 -u //x:version\[../x:groupId=\"org.opendaylight.yangtools\"\] -v "${YANGTOOLS_VERSION}"
160         fi
161         if [ "${PROJECT}" == "mdsal" ] && [ "${DISTROSTREAM}" != "fluorine" ]; then
162             MDSAL_VERSION="$(xmlstarlet sel -N x=http://maven.apache.org/POM/4.0.0 -t -v //x:mdsal.version ../pom.xml)"
163             echo "change mdsal version to ${MDSAL_VERSION}"
164             find . -name "pom.xml" -print0 | xargs -0 xmlstarlet ed --inplace -P -N x=http://maven.apache.org/POM/4.0.0 -u //x:version\[../x:parent/x:groupId=\"org.opendaylight.mdsal\"\] -v "${MDSAL_VERSION}"
165             find . -name "pom.xml" -print0 | xargs -0 xmlstarlet ed --inplace -P -N x=http://maven.apache.org/POM/4.0.0 -u //x:version\[../x:groupId=\"org.opendaylight.mdsal\"\] -v "${MDSAL_VERSION}"
166         fi
167     else
168         # If project with no patch is MRI, download release tag:
169         # shellcheck disable=SC2235
170         if [ "${PROJECT}" == "odlparent" ] || [ "${PROJECT}" == "yangtools" ] || ([ "${PROJECT}" == "mdsal" ] && [ "${DISTROSTREAM}" != "fluorine" ]); then
171             # shellcheck disable=SC2086
172             PROJECT_VERSION="$(xmlstarlet sel -N x=http://maven.apache.org/POM/4.0.0 -t -v //x:${PROJECT_SHORTNAME}.version ../pom.xml)"
173             echo "2. checking out tag v${PROJECT_VERSION}"
174             git checkout "tags/v${PROJECT_VERSION}"
175         # Otherwise download distribution branch:
176         else
177             echo "2. checking out branch ${DISTRIBUTION_BRANCH_TO_BUILD}"
178             git checkout "${DISTRIBUTION_BRANCH_TO_BUILD}"
179         fi
180     fi
181     # For patch=controller=61/29761/5:45/29645/6, this gives 45/29645/6
182     PICK_SEGMENT="$(echo "${patch}" | cut -d: -s -f 2-)"
183     IFS=':' read -ra PICKS <<< "${PICK_SEGMENT}"
184     for pick in "${PICKS[@]}"
185     do
186         echo "3. cherry-picking ${pick}"
187         git fetch "https://git.opendaylight.org/gerrit/${PROJECT}" "refs/changes/${pick}"
188         git cherry-pick --ff --keep-redundant-commits FETCH_HEAD
189     done
190     cd "${BUILD_DIR}"
191 done
192
193 # Finally add distribution if there is no int/dist patch
194 if [ "${distribution_status}" == "not_included" ]; then
195     echo "adding integration/distribution"
196     PROJECTS+=(distribution)
197     # clone distribution and add it as a module in root pom
198     git clone "https://git.opendaylight.org/gerrit/integration/distribution"
199     cd distribution
200     git checkout "${DISTRIBUTION_BRANCH_TO_BUILD}"
201     cd "${BUILD_DIR}"
202 fi
203
204 # Second phase: build everything
205
206 for PROJECT_SHORTNAME in "${PROJECTS[@]}"; do
207     # Set Fast build if project is not in BUILD_NORMAL and BUILD_FAST is true
208     if [[ "${BUILD_NORMAL}" != *"${PROJECT_SHORTNAME}"* ]] && ${BUILD_FAST}; then
209         fast_option="-Pq"
210     else
211         fast_option=""
212     fi
213     pushd "${PROJECT_SHORTNAME}"
214         # Build project
215         "$MVN" clean install \
216             -e ${fast_option} \
217             -Dstream="$DISTROSTREAM" \
218             -Dgitid.skip=false \
219             -Dmaven.gitcommitid.skip=false \
220             --global-settings "$GLOBAL_SETTINGS_FILE" \
221             --settings "$SETTINGS_FILE" \
222             "$MAVEN_OPTIONS"
223         # Since we've installed the artifacts, we can clean the build and save
224         # disk space
225         "$MVN" clean \
226             -e ${fast_option} \
227             -Dstream="$DISTROSTREAM" \
228             -Dgitid.skip=false \
229             -Dmaven.gitcommitid.skip=false \
230             --global-settings "$GLOBAL_SETTINGS_FILE" \
231             --settings "$SETTINGS_FILE" \
232             "$MAVEN_OPTIONS"
233     popd
234 done
235