fd43d7548fd2b0a45d47d616c3d2dca6040788ce
[releng/global-jjb.git] / shell / release-job.sh
1 #!/bin/bash -l
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2019 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 echo "---> release-job.sh"
12 set -eu -o pipefail
13
14 #Python bits. Remove when centos 7.7 builder is avaliable.
15 if [ -d "/opt/pyenv" ]; then
16     echo "---> Setting up pyenv"
17     export PYENV_ROOT="/opt/pyenv"
18     export PATH="$PYENV_ROOT/bin:$PATH"
19 fi
20 PYTHONPATH=$(pwd)
21 export PYTHONPATH
22 pyenv local 3.6.4
23 export PYENV_VERSION="3.6.4"
24 pip install --user lftools[nexus] jsonschema niet yq
25
26 #Functions.
27 verify_schema(){
28   echo "---> INFO: Verifying $release_file schema."
29   lftools schema verify "$release_file" "$RELEASE_SCHEMA"
30   # Verify allowed versions
31   # Allowed versions are "v#.#.#" or "#.#.#" aka SemVer
32   allowed_version_regex="^((v?)([0-9]+)\.([0-9]+)\.([0-9]+))$"
33   if [[ ! $VERSION =~ $allowed_version_regex ]]; then
34     echo "The version $VERSION is not a semantic valid version"
35     echo "Allowed versions are \"v#.#.#\" or \"#.#.#\" aka SemVer"
36     echo "See https://semver.org/ for more details on SemVer"
37     exit 1
38   fi
39 }
40
41 tag(){
42   if git tag -v "$VERSION"; then
43     echo "---> OK: Repo already tagged $VERSION Continuting to release"
44   else
45
46     echo "---> INFO: Repo has not yet been tagged $VERSION"
47     git tag -am "${PROJECT//\//-} $VERSION" "$VERSION"
48     sigul --batch -c "$SIGUL_CONFIG" sign-git-tag "$SIGUL_KEY" "$VERSION" < "$SIGUL_PASSWORD"
49     echo "Showing latest signature for $PROJECT:"
50     gpg --import "$SIGNING_PUBKEY"
51     echo "git tag -v $VERSION"
52     git tag -v "$VERSION"
53     ########## Merge Part ##############
54     if [[ "$JOB_NAME" =~ "merge" ]] && [[ "$DRY_RUN" = false ]]; then
55       echo "--> INFO: Running merge"
56       gerrit_ssh=$(echo "$GERRIT_URL" | awk -F"/" '{print $3}')
57       git remote set-url origin ssh://"$RELEASE_USERNAME"@"$gerrit_ssh":29418/"$PROJECT"
58       git config user.name "$RELEASE_USERNAME"
59       git config user.email "$RELEASE_EMAIL"
60       echo -e "Host $gerrit_ssh\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
61       chmod 600 ~/.ssh/config
62       git push origin "$VERSION"
63     fi
64   fi
65 }
66
67 nexus_release(){
68   for staging_url in $(zcat "$PATCH_DIR"/staging-repo.txt.gz | awk -e '{print $2}'); do
69     # extract the domain name from URL
70     NEXUS_URL=$(echo "$staging_url" | sed -e 's|^[^/]*//||' -e 's|/.*$||')
71     echo "---> INFO: NEXUS_URL: $NEXUS_URL"
72     # extract the staging repo from URL
73     STAGING_REPO=${staging_url#*repositories/}
74     echo "Running Nexus Verify"
75     lftools nexus release -v --server https://"$NEXUS_URL" "$STAGING_REPO"
76     echo "Merge will run"
77     echo "lftools nexus release --server https://$NEXUS_URL $STAGING_REPO"
78   done
79
80   #Run the loop twice, to catch errors on either nexus repo
81   if [[ "$JOB_NAME" =~ "merge" ]] && [[ "$DRY_RUN" = false ]]; then
82     for staging_url in $(zcat "$PATCH_DIR"/staging-repo.txt.gz | awk -e '{print $2}'); do
83       NEXUS_URL=$(echo "$staging_url" | sed -e 's|^[^/]*//||' -e 's|/.*$||')
84       STAGING_REPO=${staging_url#*repositories/}
85       echo "Promoting $STAGING_REPO on $NEXUS_URL."
86       lftools nexus release --server https://"$NEXUS_URL" "$STAGING_REPO"
87     done
88   fi
89 }
90
91
92 container_release_file(){
93   echo "---> Processing container release"
94   local lfn_umbrella
95   lfn_umbrella="$(echo "$GERRIT_HOST" | awk -F"." '{print $2}')"
96
97
98   for namequoted in $(cat $release_file | yq '.containers[].name'); do
99     versionquoted=$(cat $release_file | yq ".containers[] |select(.name=="$namequoted") |.version")
100
101     #Remove extra yaml quotes
102     name="${namequoted#\"}"
103     name="${name%\"}"
104     version="${versionquoted#\"}"
105     version="${version%\"}"
106
107     echo "$name"
108     echo "$version"
109     echo "---> INFO: Merge will release $name $version as $VERSION"
110     #Pull from public, to see if we have already tagged this.
111     if docker pull "$DOCKER_REGISTRY":10002/"$lfn_umbrella"/"$name":"$VERSION"; then
112       echo "---> OK: $VERSION is already released for image $name, Continuing..."
113     else
114       echo "---> OK: $VERSION not found in releases, release will be prepared. Continuing..."
115       docker pull "$DOCKER_REGISTRY":10001/"$lfn_umbrella"/"$name":"$version"
116       container_image_id="$(docker images | grep $name | grep $version | awk '{print $3}')"
117       echo "---> INFO: Merge will run the following commands:"
118       echo "docker tag $container_image_id $DOCKER_REGISTRY:10002/$lfn_umbrella/$name:$VERSION"
119       echo "docker push $DOCKER_REGISTRY:10002/$lfn_umbrella/$name:$VERSION"
120       if [[ "$JOB_NAME" =~ "merge" ]]; then
121         docker tag "$container_image_id" "$DOCKER_REGISTRY":10002/"$lfn_umbrella"/"$name":"$VERSION"
122         docker push "$DOCKER_REGISTRY":10002/"$lfn_umbrella"/"$name":"$VERSION"
123       fi
124       echo "#########################"
125     fi
126   done
127
128   ref="$(niet ".ref" "$release_file")"
129   echo "---> INFO: Merge will tag ref: $ref"
130   git checkout "$ref"
131   tag
132 }
133
134 maven_release_file(){
135   echo "---> INFO: wget -P $PATCH_DIR ${LOGS_URL}/staging-repo.txt.gz"
136   wget -P "$PATCH_DIR" "${LOGS_URL}/"staging-repo.txt.gz
137   pushd "$PATCH_DIR"
138     echo "---> INFO: wget ${LOGS_URL}/patches/{${PROJECT//\//-}.bundle,taglist.log.gz}"
139     wget "${LOGS_URL}"/patches/{"${PROJECT//\//-}".bundle,taglist.log.gz}
140     gunzip taglist.log.gz
141     cat "$PATCH_DIR"/taglist.log
142   popd
143   git checkout "$(awk '{print $NF}' "$PATCH_DIR/taglist.log")"
144   git fetch "$PATCH_DIR/${PROJECT//\//-}.bundle"
145   git merge --ff-only FETCH_HEAD
146   nexus_release
147   tag
148 }
149
150 echo "########### Start Script release-job.sh ###################################"
151 echo "---> INFO: Setting all VARS"
152
153 LOGS_SERVER="${LOGS_SERVER:-None}"
154 MAVEN_CENTRAL_URL="${MAVEN_CENTRAL_URL:-None}"
155 if [ "${LOGS_SERVER}" == 'None' ]; then
156     echo "FAILED: log server not found"
157     exit 1
158 fi
159
160 if $USE_RELEASE_FILE ; then
161
162   release_files=$(git diff-tree --no-commit-id -r "$GERRIT_PATCHSET_REVISION" --name-only -- "releases/" ".releases/")
163   if (( $(grep -c . <<<"$release_files") > 1 )); then
164     echo "---> INFO: RELEASE FILES ARE AS FOLLOWS: $release_files"
165     echo "---> ERROR: Committing multiple release files in the same commit OR rename/amend of existing files is not supported."
166     exit 1
167   else
168     release_file="$release_files"
169     echo "---> INFO: RELEASE FILE: $release_files"
170   fi
171
172 else
173   echo "This job is built with parameters, no release file needed. Continuing..."
174   release_file="None"
175 fi
176
177
178 NEXUS_PATH="${SILO}/${JENKINS_HOSTNAME}/"
179
180 VERSION="${VERSION:-None}"
181 if [[ $VERSION == "None" ]]; then
182   VERSION="$(niet ".version" "$release_file")"
183 fi
184
185 LOG_DIR="${LOG_DIR:-None}"
186 if [[ $LOG_DIR == "None" ]]; then
187   LOG_DIR="$(niet ".log_dir" "$release_file")"
188 fi
189
190 DISTRIBUTION_TYPE="${DISTRIBUTION_TYPE:-None}"
191 if [[ $DISTRIBUTION_TYPE == "None" ]]; then
192   DISTRIBUTION_TYPE="$(niet ".distribution_type" "$release_file")"
193 fi
194
195 ####
196 LOGS_URL="${LOGS_SERVER}/${NEXUS_PATH}${LOG_DIR}"
197 LOGS_URL=${LOGS_URL%/}  # strip any trailing '/'
198 PATCH_DIR="$(mktemp -d)"
199 #INFO
200 echo "INFO:"
201 echo "RELEASE_FILE: $release_file"
202 echo "LOGS_SERVER: $LOGS_SERVER"
203 echo "NEXUS_PATH: $NEXUS_PATH"
204 echo "JENKINS_HOSTNAME: $JENKINS_HOSTNAME"
205 echo "SILO: $SILO"
206 echo "PROJECT: $PROJECT"
207 echo "PROJECT-DASHED: ${PROJECT//\//-}"
208 echo "VERSION: $VERSION"
209 echo "LOG DIR: $LOG_DIR"
210 echo "LOGS URL: $LOGS_URL"
211 echo "DISTRIBUTION_TYPE: $DISTRIBUTION_TYPE"
212 #Check if this is a container or maven release: release-container-schema.yaml vs release-schema.yaml
213
214 #Logic to determine what we are releasing.
215 ##########################################
216 if [[ "$DISTRIBUTION_TYPE" == "maven" ]]; then
217   wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/release-schema.yaml
218   RELEASE_SCHEMA="release-schema.yaml"
219   if $USE_RELEASE_FILE ; then
220     verify_schema
221   fi
222   maven_release_file
223 elif [[ "$DISTRIBUTION_TYPE" == "container" ]]; then
224   wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/release-container-schema.yaml
225   RELEASE_SCHEMA="release-container-schema.yaml"
226   verify_schema
227   container_release_file
228 else
229   echo "---> ERROR: distribution_type: $DISTRIBUTION_TYPE not supported"
230   echo "Must be maven or container"
231   exit 1
232 fi
233 ##########################################
234
235 echo "########### End Script release-job.sh ###################################"