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