Merge "Add 4 node COE CSIT job"
[releng/builder.git] / jjb / integration / multipatch-distribution.sh
1 #!/bin/bash
2
3 set -e
4
5 # TODO: 1) clean up inline todo's below :)
6 # TODO: 2) Use just a topic branch to create a distribution.  see this email:
7 #          https://lists.opendaylight.org/pipermail/discuss/2015-December/006040.html
8 # TODO: 3) Bubble up CSIT jobs that are triggered by the multipatch job to a jenkins
9 #          parameter.  the default can be distribution-test which calls all CSIT jobs
10 #          but being able to easily override it to a smaller subset (or none) will be
11 #          helpful
12
13 # create a fresh empty place to build this custom distribution
14 BUILD_DIR=${WORKSPACE}/patch_tester
15 DISTRIBUTION_BRANCH_TO_BUILD=$DISTROBRANCH  #renaming variable for clarity
16 MAVEN_OPTIONS="$(echo --show-version \
17     --batch-mode \
18     -Djenkins \
19     -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn \
20     -Dmaven.repo.local=/tmp/r \
21     -Dorg.ops4j.pax.url.mvn.localRepository=/tmp/r)"
22
23 rm -rf $BUILD_DIR
24 mkdir -p $BUILD_DIR
25 cd $BUILD_DIR
26
27 # Set up git committer name and email, needed for commit creation when cherry-picking.
28 export EMAIL="sandbox@jenkins.opendaylight.org"
29 export GIT_COMMITTER_NAME="Multipatch Job"
30
31 # Extract a list of patches per project from an comment trigger. An example is:
32 # Patch Set 1:
33 #
34 # multipatch-build:openflowplugin:45/69445/1,genius:46/69446/1,netvirt:47/69447/1
35 if [ -n "$GERRIT_EVENT_COMMENT_TEXT" ]; then
36     if [[ "$GERRIT_EVENT_COMMENT_TEXT" == *fast* ]]; then
37         BUILD_FAST="true"
38         PATCHES_TO_BUILD=$(echo "$GERRIT_EVENT_COMMENT_TEXT" | grep 'multipatch-build-fast:')
39     else
40         BUILD_FAST="false"
41         PATCHES_TO_BUILD=$(echo "$GERRIT_EVENT_COMMENT_TEXT" | grep 'multipatch-build:')
42     fi
43     PATCHES_TO_BUILD=${PATCHES_TO_BUILD#*:}
44 fi
45 if ${BUILD_FAST}; then
46     fast_option="-Pq"
47 else
48     fast_option=""
49 fi
50 # check if topic exists, e.g. topic=binding-tlc-rpc
51 if [[ "${PATCHES_TO_BUILD}" == *topic* ]]; then
52     TOPIC="${PATCHES_TO_BUILD#*=}"
53     echo "Create topic ${TOPIC} patch list"
54     PATCHES_TO_BUILD=""
55     read -ra PROJECT_LIST <<< "${BUILD_ORDER}"
56     for PROJECT in "${PROJECT_LIST[@]}"; do
57         # get all patches number for a topic for a given project
58         IFS=$'\n' read -rd '' -a GERRIT_PATCH_LIST <<< "$(ssh -p 29418 jenkins-$SILO@git.opendaylight.org gerrit query status:open topic:${TOPIC} project:${PROJECT} \
59         | grep 'number:' | awk '{{ print $2 }}')" || true
60         echo "Add ${PROJECT}:${GERRIT_PATCH_LIST[*]}"
61         # add project if it is the first with patches or it is not the first
62         if [[ -z "${PATCHES_TO_BUILD}" && ! -z "${GERRIT_PATCH_LIST[*]}" ]]; then
63             PATCHES_TO_BUILD="${PROJECT}"
64         elif [[ ! -z "${PATCHES_TO_BUILD}" ]]; then
65             PATCHES_TO_BUILD="${PATCHES_TO_BUILD},${PROJECT}"
66         fi
67         # sort project patches
68         if [[ ! -z "${GERRIT_PATCH_LIST[*]}" ]]; then
69             REF_LIST=()
70             # create reference list with patch number-refspec
71             for PATCH in "${GERRIT_PATCH_LIST[@]}"; do
72                 REFSPEC=$(ssh -p 29418 jenkins-$SILO@git.opendaylight.org gerrit query change:${PATCH} --current-patch-set \
73                 | grep 'ref:' | awk '{{ print $2 }}')
74                 REF_LIST+=("${PATCH}-${REFSPEC/refs\/changes\/}")
75             done
76             # sort reference list by patch number
77             IFS=$'\n' SORT_REF=$(sort <<<"${REF_LIST[*]}") && unset IFS
78             read -rd '' -a SORT_REF_LIST <<< "${SORT_REF[*]}" || true
79             # add refspec to patches to build list
80             for PATCH in "${SORT_REF_LIST[@]}"; do
81                 PATCHES_TO_BUILD="${PATCHES_TO_BUILD}:${PATCH/*-/}"
82             done
83         fi
84     done
85 fi
86
87 echo "Patches to build: ${PATCHES_TO_BUILD}"
88 IFS=',' read -ra PATCHES <<< "${PATCHES_TO_BUILD}"
89
90 # First phase: clone the necessary repos and set the patches up
91
92 declare -a PROJECTS
93
94 # For each patch:
95 # * Clone the project.
96 # * Optionally, checkout a specific (typically unmerged) Gerrit patch. If none,
97 #   default to Integration/Distribution branch via {branch} JJB param.
98 # * Also optionally, cherry-pick series of patches on top of the checkout.
99 # * Final option: perform a 'release' by removing "-SNAPSHOT" everywhere within the project.
100 #
101 # Each patch is found in the ${PATCHES_TO_BUILD} variable as a comma separated
102 # list of project[=checkout][:cherry-pick]* values.
103 #
104 # Checkout a (typically unmerged) Gerrit patch on top of odlparent's git clone:
105 #
106 # PATCHES_TO_BUILD='odlparent=45/30045/2'
107 #
108 # Checkout patches for both odlparent and yangtools:
109 #
110 # PATCHES_TO_BUILD='odlparent=45/30045/2,yangtools:53/26853/25'
111 #
112 # Checkout a patch for controller, cherry-pick another patch on top of it:
113 #
114 # PATCHES_TO_BUILD='controller=61/29761/5:45/29645/6'
115 distribution_status="not_included"
116 for proto_patch in "${PATCHES[@]}"
117 do
118     echo "working on ${proto_patch}"
119     # For patch=controller=61/29761/5:45/29645/6, this gives controller
120     PROJECT="$(echo ${proto_patch} | cut -d\: -f 1 | cut -d\= -f 1)"
121     if [ "${PROJECT}" == "integration/distribution" ]; then
122         distribution_status="included"
123     fi
124     PROJECT_SHORTNAME="${PROJECT##*/}"  # http://stackoverflow.com/a/3162500
125     PROJECTS+=("${PROJECT_SHORTNAME}")
126     echo "cloning project ${PROJECT}"
127     git clone "https://git.opendaylight.org/gerrit/p/${PROJECT}"
128     cd ${PROJECT_SHORTNAME}
129     if [ "$(echo -n ${proto_patch} | tail -c 1)" == 'r' ]; then
130         pure_patch="$(echo -n $proto_patch | head -c -1)"
131     else
132         pure_patch="$proto_patch"
133     fi
134     # For patch = controller=61/29761/5:45/29645/6, this gives 61/29761/5
135     CHECKOUT="$(echo ${pure_patch} | cut -d\= -s -f 2 | cut -d\: -f 1)"
136     if [ "x${CHECKOUT}" != "x" ]; then
137         echo "checking out ${CHECKOUT}"
138         # TODO: Make this script accept "29645/6" as a shorthand for "45/29645/6".
139         git fetch "https://git.opendaylight.org/gerrit/${PROJECT}" "refs/changes/$CHECKOUT"
140         git checkout FETCH_HEAD
141     else
142         echo "checking out ${DISTRIBUTION_BRANCH_TO_BUILD}"
143         git checkout "${DISTRIBUTION_BRANCH_TO_BUILD}"
144     fi
145     # For patch=controller=61/29761/5:45/29645/6, this gives 45/29645/6
146     PICK_SEGMENT="$(echo "${pure_patch}" | cut -d\: -s -f 2-)"
147     IFS=':' read -ra PICKS <<< "${PICK_SEGMENT}"
148     for pick in "${PICKS[@]}"
149     do
150         echo "cherry-picking ${pick}"
151         git fetch "https://git.opendaylight.org/gerrit/${PROJECT}" "refs/changes/${pick}"
152         git cherry-pick --ff --keep-redundant-commits FETCH_HEAD
153     done
154     if [ "$(echo -n ${proto_patch} | tail -c 1)" == 'r' ]; then
155         # Here 'r' means release. Useful for testing Nitrogen Odlparent changes.
156         find . -name "*.xml" -print0 | xargs -0 sed -i 's/-SNAPSHOT//g'
157     fi
158     cd "${BUILD_DIR}"
159 done
160
161 if [ "${distribution_status}" == "not_included" ]; then
162     echo "adding integration/distribution"
163     PROJECTS+=(distribution)
164     # clone distribution and add it as a module in root pom
165     git clone "https://git.opendaylight.org/gerrit/p/integration/distribution"
166     cd distribution
167     git checkout "${DISTRIBUTION_BRANCH_TO_BUILD}"
168     cd "${BUILD_DIR}"
169 fi
170
171 # Second phase: build everything
172
173 for PROJECT_SHORTNAME in "${PROJECTS[@]}"; do
174     pushd "${PROJECT_SHORTNAME}"
175     # Build project
176     "$MVN" clean install \
177     -e ${fast_option} \
178     -Dstream="$DISTROSTREAM" \
179     -Dgitid.skip=false \
180     -Dmaven.gitcommitid.skip=false \
181     --global-settings "$GLOBAL_SETTINGS_FILE" \
182     --settings "$SETTINGS_FILE" \
183     $MAVEN_OPTIONS
184     # Since we've installed the artifacts, we can clean the build and save
185     # disk space
186     "$MVN" clean \
187     -e ${fast_option} \
188     -Dstream="$DISTROSTREAM" \
189     -Dgitid.skip=false \
190     -Dmaven.gitcommitid.skip=false \
191     --global-settings "$GLOBAL_SETTINGS_FILE" \
192     --settings "$SETTINGS_FILE" \
193     $MAVEN_OPTIONS
194     popd
195 done
196