Fix release file detection
[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 echo "INFO: creating virtual environment"
15 virtualenv -p python3 /tmp/venv
16 PATH=/tmp/venv/bin:$PATH
17 pipup="python -m pip install -q --upgrade pip lftools jsonschema niet twine yq"
18 echo "INFO: $pipup"
19 $pipup
20
21 #Functions.
22
23 set_variables_common(){
24     echo "INFO: Setting common variables"
25     if [[ -z ${LOGS_SERVER:-} ]]; then
26         echo "ERROR: LOGS_SERVER not defined"
27         exit 1
28     fi
29     NEXUS_PATH="${SILO}/${JENKINS_HOSTNAME}/"
30     # Verify if using release file or parameters
31     if $USE_RELEASE_FILE ; then
32         release_files=$(git diff-tree -m --no-commit-id -r "$GIT_COMMIT" "$GIT_COMMIT^1" \
33             --name-only -- "releases/" ".releases/")
34         if (( $(grep -c . <<<"$release_files") > 1 )); then
35           echo "INFO: RELEASE FILES ARE AS FOLLOWS: $release_files"
36           echo "ERROR: Adding multiple release files in the same commit"
37           echo "ERROR: OR rename/amend/delete of existing files is not supported."
38           exit 1
39         else
40           release_file="$release_files"
41           echo "INFO: RELEASE FILE: $release_files"
42         fi
43     else
44         echo "INFO: This job is built with parameters, no release file needed."
45         release_file="None"
46     fi
47
48     # Jenkins parameter drop-down defaults DISTRIBUTION_TYPE to None
49     DISTRIBUTION_TYPE="${DISTRIBUTION_TYPE:-None}"
50     if [[ $DISTRIBUTION_TYPE == "None" ]]; then
51         DISTRIBUTION_TYPE=$(niet ".distribution_type" "$release_file")
52     fi
53
54     PATCH_DIR=$(mktemp -d)
55
56     # Displaying Release Information (Common variables)
57     printf "\t%-30s\n" RELEASE_ENVIRONMENT_INFO:
58     printf "\t%-30s %s\n" RELEASE_FILE: "$release_file"
59     printf "\t%-30s %s\n" LOGS_SERVER: "$LOGS_SERVER"
60     printf "\t%-30s %s\n" NEXUS_PATH: "$NEXUS_PATH"
61     printf "\t%-30s %s\n" JENKINS_HOSTNAME: "$JENKINS_HOSTNAME"
62     printf "\t%-30s %s\n" SILO: "$SILO"
63     printf "\t%-30s %s\n" PROJECT: "$PROJECT"
64     printf "\t%-30s %s\n" PROJECT-DASHED: "${PROJECT//\//-}"
65     printf "\t%-30s %s\n" DISTRIBUTION_TYPE: "$DISTRIBUTION_TYPE"
66 }
67
68 set_variables_maven(){
69     echo "INFO: Setting maven variables"
70     if [[ -z ${VERSION:-} ]]; then
71         VERSION=$(niet ".version" "$release_file")
72     fi
73     if [[ -z ${GIT_TAG:-} ]]; then
74         if grep -q "git_tag" "$release_file" ; then
75             GIT_TAG=$(niet ".git_tag" "$release_file")
76         else
77             GIT_TAG="$VERSION"
78         fi
79     fi
80     if [[ -z ${LOG_DIR:-} ]]; then
81         LOG_DIR=$(niet ".log_dir" "$release_file")
82     fi
83     LOGS_URL="${LOGS_SERVER}/${NEXUS_PATH}${LOG_DIR}"
84     LOGS_URL=${LOGS_URL%/}  # strip any trailing '/'
85
86     # Continuing displaying Release Information (Maven)
87     printf "\t%-30s\n" RELEASE_MAVEN_INFO:
88     printf "\t%-30s %s\n" VERSION: "$VERSION"
89     printf "\t%-30s %s\n" GIT_TAG: "$GIT_TAG"
90     printf "\t%-30s %s\n" LOG_DIR: "$LOG_DIR"
91     printf "\t%-30s %s\n" LOGS_URL: "$LOGS_URL"
92 }
93
94 set_variables_container(){
95     echo "INFO: Setting container variables"
96     if [[ -z ${VERSION:-} ]]; then
97         VERSION=$(niet ".container_release_tag" "$release_file")
98     fi
99     if [[ -z ${GIT_TAG:-} ]]; then
100         if grep -q "git_tag" "$release_file" ; then
101             GIT_TAG=$(niet ".git_tag" "$release_file")
102         else
103             GIT_TAG="$VERSION"
104         fi
105    fi
106     if grep -q "container_pull_registry" "$release_file" ; then
107         CONTAINER_PULL_REGISTRY=$(niet ".container_pull_registry" "$release_file")
108     fi
109     if grep -q "container_push_registry" "$release_file" ; then
110         CONTAINER_PUSH_REGISTRY=$(niet ".container_push_registry" "$release_file")
111     fi
112     # Make sure both pull and push registries are defined
113     if [ -z ${CONTAINER_PULL_REGISTRY+x} ] || [ -z ${CONTAINER_PUSH_REGISTRY+x} ]; then
114         echo "ERROR: CONTAINER_PULL_REGISTRY and CONTAINER_PUSH_REGISTRY need to be defined"
115         exit 1
116     fi
117     ref=$(niet ".ref" "$release_file")
118
119     # Continuing displaying Release Information (Container)
120     printf "\t%-30s\n" RELEASE_CONTAINER_INFO:
121     printf "\t%-30s %s\n" CONTAINER_RELEASE_TAG: "$VERSION"
122     printf "\t%-30s %s\n" CONTAINER_PULL_REGISTRY: "$CONTAINER_PULL_REGISTRY"
123     printf "\t%-30s %s\n" CONTAINER_PUSH_REGISTRY: "$CONTAINER_PUSH_REGISTRY"
124     printf "\t%-30s %s\n" GERRIT_REF_TO_TAG: "$ref"
125     printf "\t%-30s %s\n" GIT_TAG: "$GIT_TAG"
126 }
127
128 set_variables_pypi(){
129     echo "INFO: Setting pypi variables"
130     if [[ -z ${LOG_DIR:-} ]]; then
131         LOG_DIR=$(niet ".log_dir" "$release_file")
132     fi
133     LOGS_URL="${LOGS_SERVER}/${NEXUS_PATH}${LOG_DIR}"
134     LOGS_URL=${LOGS_URL%/}  # strip any trailing '/'
135     if [[ -z ${PYPI_PROJECT:-} ]]; then
136         PYPI_PROJECT=$(niet ".pypi_project" "$release_file")
137     fi
138     if [[ -z ${PYTHON_VERSION:-} ]]; then
139         PYTHON_VERSION=$(niet ".python_version" "$release_file")
140     fi
141     if [[ -z ${VERSION:-} ]]; then
142         VERSION=$(niet ".version" "$release_file")
143     fi
144     if [[ -z ${GIT_TAG:-} ]]; then
145         if grep -q "git_tag" "$release_file" ; then
146             GIT_TAG=$(niet ".git_tag" "$release_file")
147         else
148             GIT_TAG="$VERSION"
149         fi
150    fi
151
152     # Continuing displaying Release Information (pypi)
153     printf "\t%-30s\n" RELEASE_PYPI_INFO:
154     printf "\t%-30s %s\n" LOG_DIR: "$LOG_DIR"
155     printf "\t%-30s %s\n" LOGS_URL: "$LOGS_URL"
156     printf "\t%-30s %s\n" PYPI_INDEX: "$PYPI_INDEX" # from job configuration
157     printf "\t%-30s %s\n" PYPI_PROJECT: "$PYPI_PROJECT"
158     printf "\t%-30s %s\n" PYTHON_VERSION: "$PYTHON_VERSION"
159     printf "\t%-30s %s\n" VERSION: "$VERSION"
160     printf "\t%-30s %s\n" GIT_TAG: "$GIT_TAG"
161 }
162
163 set_variables_packagecloud(){
164      echo "INFO: Setting packagecloud variables"
165      if [[ -z ${VERSION:-} ]]; then
166          VERSION=$(niet ".version" "$release_file")
167      fi
168      if [[ -z ${GIT_TAG:-} ]]; then
169          if grep -q "git_tag" $release_file ; then
170              GIT_TAG=$(niet ".git_tag" "$release_file")
171          else
172              GIT_TAG="$VERSION"
173          fi
174      fi
175      if [[ -z ${LOG_DIR:-} ]]; then
176          LOG_DIR=$(niet ".log_dir" "$release_file")
177      fi
178      if [[ -z ${REF:-} ]]; then
179          REF=$(niet ".ref" "$release_file")
180      fi
181      if [[ -z ${PACKAGE_NAME:-} ]]; then
182          PACKAGE_NAME=$(niet ".package_name" "$release_file")
183      fi
184      logs_url="${LOGS_SERVER}/${NEXUS_PATH}${LOG_DIR}"
185      logs_url=${logs_url%/}  # strip any trailing '/'
186
187      printf "\t%-30s %s\n" PACKAGE_NAME: "$PACKAGE_NAME"
188      printf "\t%-30s %s\n" LOG_DIR: "$LOG_DIR"
189      printf "\t%-30s %s\n" LOGS_URL: "$logs_url"
190      printf "\t%-30s %s\n" GERRIT_REF_TO_TAG: "$REF"
191      printf "\t%-30s %s\n" VERSION: "$VERSION"
192      printf "\t%-30s %s\n" GIT_TAG: "$GIT_TAG"
193 }
194
195 verify_schema(){
196     echo "INFO: Verifying $release_file against schema $release_schema"
197     lftools schema verify "$release_file" "$release_schema"
198 }
199
200 verify_version(){
201     # Verify allowed patterns "#.#.#" (SemVer) or "v#.#.#"
202     echo "INFO: Verifying version $VERSION"
203     allowed_version_regex="^((v?)([0-9]+)\.([0-9]+)\.([0-9]+))$"
204     if [[ $VERSION =~ $allowed_version_regex ]]; then
205         echo "INFO: The version $VERSION is valid"
206     else
207         echo "ERROR: The version $VERSION is not valid"
208         echo "ERROR: Valid versions are \"#.#.#\" (SemVer) or \"v#.#.#\""
209         echo "ERROR: See https://semver.org/ for more details on SemVer"
210         exit 1
211     fi
212 }
213
214 verify_version_match_release(){
215     echo "INFO: Fetching console log from $LOGS_URL"
216     wget -P /tmp "${LOGS_URL}/"console.log.gz
217     echo "INFO: Searching for uploaded step and version $VERSION in job log"
218     if zgrep "Successfully uploaded" /tmp/console.log.gz | grep "$VERSION"; then
219         echo "INFO: found expected strings in job log"
220     else
221         echo "ERROR: Defined version in release file does not match staging repo artifacts version to be released"
222         echo "ERROR: Please make sure maven stage job log dir and release version are both correct"
223         exit 1
224     fi
225 }
226
227 # check prerequisites to detect mistakes in the release YAML file
228 verify_pypi_match_release(){
229     echo "INFO: Fetching console log from $LOGS_URL"
230     wget -q -P /tmp "${LOGS_URL}/"console.log.gz
231     echo "INFO: Searching for uploaded step, project $PYPI_PROJECT and version $VERSION in job log"
232     # pypi-upload.sh generates success message with file list
233     if zgrep -i "uploaded" /tmp/console.log.gz | grep "$PYPI_PROJECT" | grep "$VERSION" ; then
234         echo "INFO: found expected strings in job log"
235     else
236         echo "ERROR: failed to find expected strings in job log"
237         exit 1
238     fi
239 }
240
241 verify_packagecloud_match_release(){
242     echo "INFO: Fetching console log from $logs_url"
243     wget -q -P /tmp "${logs_url}/"console.log.gz
244     echo "INFO: Searching for uploaded step, package name $PACKAGE_NAME and version $VERSION in job log"
245     if  zgrep -E "Pushing.*$PACKAGE_NAME.*$VERSION.*success\!" /tmp/console.log.gz; then
246         echo "INFO: found expected strings in job log"
247     else
248         echo "ERROR: failed to find expected strings in job log"
249         exit 1
250     fi
251 }
252
253 # sigul is only available on Centos
254 # TODO: write tag-github-repo function
255 tag-gerrit-repo(){
256     echo "INFO: tag gerrit with $GIT_TAG"
257     # Import public signing key
258     gpg --import "$SIGNING_PUBKEY"
259     if type=$(git cat-file -t "$GIT_TAG"); then
260         if [[ $type == "tag" ]]; then
261             echo "INFO: Repo already has signed tag $GIT_TAG, nothing to do"
262         else
263             echo "ERROR: Repo has lightweight tag $GIT_TAG, blocks push of signed tag"
264             exit 1
265         fi
266     else
267         echo "INFO: Repo has not yet been tagged $GIT_TAG"
268         git tag -am "${PROJECT//\//-} $GIT_TAG" "$GIT_TAG"
269         sigul --batch -c "$SIGUL_CONFIG" sign-git-tag "$SIGUL_KEY" "$GIT_TAG" < "$SIGUL_PASSWORD"
270         echo "INFO: Showing latest signature for $PROJECT:"
271         echo "INFO: git tag -v $GIT_TAG"
272         git tag -v "$GIT_TAG"
273
274         ########## Merge Part ##############
275         if [[ "$JOB_NAME" =~ "merge" ]] && [[ "$DRY_RUN" = false ]]; then
276             echo "INFO: Running merge, pushing tag"
277             gerrit_ssh=$(echo "$GERRIT_URL" | awk -F"/" '{print $3}')
278             git remote set-url origin ssh://"$RELEASE_USERNAME"@"$gerrit_ssh":29418/"$PROJECT"
279             git config user.name "$RELEASE_USERNAME"
280             git config user.email "$RELEASE_EMAIL"
281             echo -e "Host $gerrit_ssh\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
282             chmod 600 ~/.ssh/config
283             git push origin "$GIT_TAG"
284         fi
285     fi
286 }
287
288 nexus_release(){
289     echo "INFO: Processing nexus release"
290     for staging_url in $(zcat "$PATCH_DIR"/staging-repo.txt.gz | awk -e '{print $2}'); do
291         # extract the domain name from URL
292         NEXUS_URL=$(echo "$staging_url" | sed -e 's|^[^/]*//||' -e 's|/.*$||')
293         echo "INFO: NEXUS_URL: $NEXUS_URL"
294         # extract the staging repo from URL
295         STAGING_REPO=${staging_url#*repositories/}
296         echo "INFO: Running Nexus Verify"
297         lftools nexus release -v --server https://"$NEXUS_URL" "$STAGING_REPO"
298         echo "INFO: Merge will run:"
299         echo "lftools nexus release --server https://$NEXUS_URL $STAGING_REPO"
300     done
301
302     #Run the loop twice, to catch errors on either nexus repo
303     if [[ "$JOB_NAME" =~ "merge" ]] && [[ "$DRY_RUN" = false ]]; then
304         for staging_url in $(zcat "$PATCH_DIR"/staging-repo.txt.gz | awk -e '{print $2}'); do
305           NEXUS_URL=$(echo "$staging_url" | sed -e 's|^[^/]*//||' -e 's|/.*$||')
306           STAGING_REPO=${staging_url#*repositories/}
307           echo "INFO: Promoting $STAGING_REPO on $NEXUS_URL."
308           lftools nexus release --server https://"$NEXUS_URL" "$STAGING_REPO"
309         done
310     fi
311 }
312
313 container_release_file(){
314     echo "INFO: Processing container release"
315     local lfn_umbrella
316     lfn_umbrella="$(echo "$GERRIT_URL" | awk -F"." '{print $2}')"
317
318     for namequoted in $(yq '.containers[].name' $release_file); do
319         versionquoted=$(yq ".containers[] |select(.name==$namequoted) |.version" $release_file)
320
321         #Remove extra yaml quotes
322         name="${namequoted#\"}"
323         name="${name%\"}"
324         version="${versionquoted#\"}"
325         version="${version%\"}"
326
327         echo "$name"
328         echo "$version"
329         echo "INFO: Merge will release $name $version as $VERSION"
330         # Attempt to pull from releases registry to see if the image has been released.
331         if docker pull "$CONTAINER_PUSH_REGISTRY"/"$lfn_umbrella"/"$name":"$VERSION"; then
332             echo "INFO: $VERSION is already released for image $name, Continuing..."
333         else
334             echo "INFO: $VERSION not found in releases, release will be prepared. Continuing..."
335             docker pull "$CONTAINER_PULL_REGISTRY"/"$lfn_umbrella"/"$name":"$version"
336             container_image_id=$(docker images | grep "$name" | grep "$version" | awk '{print $3}')
337             echo "INFO: Merge will run the following commands:"
338             echo "docker tag $container_image_id $CONTAINER_PUSH_REGISTRY/$lfn_umbrella/$name:$VERSION"
339             echo "docker push $CONTAINER_PUSH_REGISTRY/$lfn_umbrella/$name:$VERSION"
340             if [[ "$JOB_NAME" =~ "merge" ]]; then
341                 docker tag "$container_image_id" "$CONTAINER_PUSH_REGISTRY"/"$lfn_umbrella"/"$name":"$VERSION"
342                 docker push "$CONTAINER_PUSH_REGISTRY"/"$lfn_umbrella"/"$name":"$VERSION"
343             fi
344             echo "#########################"
345         fi
346     done
347
348     echo "INFO: Merge will tag ref: $ref"
349     git checkout "$ref"
350     tag-gerrit-repo
351 }
352
353 maven_release_file(){
354     echo "INFO: Processing maven release"
355     echo "INFO: wget -P $PATCH_DIR ${LOGS_URL}/staging-repo.txt.gz"
356     wget -P "$PATCH_DIR" "${LOGS_URL}/"staging-repo.txt.gz
357     pushd "$PATCH_DIR"
358         echo "INFO: wget ${LOGS_URL}/patches/{${PROJECT//\//-}.bundle,taglist.log.gz}"
359         wget "${LOGS_URL}"/patches/{"${PROJECT//\//-}".bundle,taglist.log.gz}
360         gunzip taglist.log.gz
361         cat "$PATCH_DIR"/taglist.log
362     popd
363     git checkout "$(awk '{print $NF}' "$PATCH_DIR/taglist.log")"
364     git fetch "$PATCH_DIR/${PROJECT//\//-}.bundle"
365     git merge --ff-only FETCH_HEAD
366     nexus_release
367     tag-gerrit-repo
368 }
369
370 # calls pip to download binary and source distributions from the specified index,
371 # which requires a recent-in-2019 version.  Uploads the files it received.
372 pypi_release_file(){
373     echo "INFO: Processing pypi release"
374     tgtdir=dist
375     mkdir $tgtdir
376     pip_pfx="pip download -d $tgtdir --no-deps --python-version $PYTHON_VERSION -i $PYPI_INDEX"
377     module="$PYPI_PROJECT==$VERSION"
378     pip_bin="$pip_pfx $module"
379     echo "INFO: downloading binary: $pip_bin"
380     if ! $pip_bin ; then
381         echo "WARN: failed to download binary distribution"
382     fi
383     pip_src="$pip_pfx --no-binary=:all: $module"
384     echo "INFO: downloading source: $pip_src"
385     if ! $pip_src ; then
386         echo "WARN: failed to download source distribution"
387     fi
388     echo "INFO: Checking files in $tgtdir"
389     # shellcheck disable=SC2012
390     filecount=$(ls $tgtdir | wc -l)
391     if [[ $filecount = 0 ]] ; then
392         echo "ERROR: no files downloaded"
393         exit 1
394     else
395         # shellcheck disable=SC2046
396         echo "INFO: downloaded $filecount distributions: " $(ls $tgtdir)
397     fi
398
399     if [[ ! "$JOB_NAME" =~ "merge" ]] ; then
400         echo "INFO: not a merge job, not uploading files"
401         return
402     fi
403
404     cmd="twine upload -r $REPOSITORY $tgtdir/*"
405     if $DRY_RUN; then
406         echo "INFO: dry-run is set, echoing command only"
407         echo "$cmd"
408     else
409         echo "INFO: uploading $filecount distributions to repo $REPOSITORY"
410         $cmd
411     fi
412     tag-gerrit-repo
413 }
414
415 packagecloud_verify(){
416     echo "INFO: Verifying $1 exists in staging..."
417     if [[ $1 == $(curl --netrc-file ~/packagecloud_api --silent \
418         https://packagecloud.io/api/v1/repos/"$2"/staging/search?q="$1" \
419         | yq -r .[].filename) ]]; then
420         echo "INFO: $1 exists in staging!"
421         echo "INFO: Existing package location: https://packagecloud.io$(curl \
422             --netrc-file ~/packagecloud_api --silent \
423             https://packagecloud.io/api/v1/repos/"$2"/staging/search?q="$1" \
424             | yq -r .[].package_html_url)"
425     else
426         echo "ERROR: $1 does not exist in staging"
427         exit 1
428     fi
429 }
430
431 packagecloud_promote(){
432     echo "INFO: Preparing to promote $1..."
433     promote_url="https://packagecloud.io$(curl --netrc-file ~/packagecloud_api \
434         --silent https://packagecloud.io/api/v1/repos/"$2"/staging/search?q="$1" \
435         | yq -r .[].promote_url)"
436     echo "INFO: Promoting $1 from staging to release"
437     curl --netrc-file ~/packagecloud_api -X POST -F \
438         destination="$2/release" "$promote_url" \
439         | echo "INFO: Promoted package location: \
440         https://packagecloud.io$(yq -r .package_html_url)"
441     git checkout "$REF"
442     tag-gerrit-repo
443 }
444
445 ##############################  End Function Declarations  ################################
446
447 # Set common environment variables
448 set_variables_common
449
450 # Determine the type of release:
451 #   - container, release-container-schema.yaml
452 #   - maven, release-schema.yaml
453 #   - pypi,  release-pypi-schema.yaml
454
455 case $DISTRIBUTION_TYPE in
456
457     maven)
458         if $USE_RELEASE_FILE ; then
459             release_schema="release-schema.yaml"
460             echo "INFO: Fetching schema $release_schema"
461             wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/$release_schema
462             verify_schema
463         fi
464         set_variables_maven
465         verify_version
466         verify_version_match_release
467         maven_release_file
468         ;;
469
470     container)
471         if $USE_RELEASE_FILE ; then
472             release_schema="release-container-schema.yaml"
473             echo "INFO: Fetching schema $release_schema"
474             wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/${release_schema}
475             verify_schema
476         fi
477         set_variables_container
478         verify_version
479         container_release_file
480         ;;
481
482     pypi)
483         if $USE_RELEASE_FILE ; then
484             release_schema="release-pypi-schema.yaml"
485             echo "INFO: Fetching schema $release_schema"
486             wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/${release_schema}
487             verify_schema
488         fi
489         set_variables_pypi
490         verify_version
491         verify_pypi_match_release
492         pypi_release_file
493         ;;
494
495     packagecloud)
496         if $USE_RELEASE_FILE ; then
497             release_schema="release-packagecloud-schema.yaml"
498             packagecloud_account=$(cat "$ACCOUNT_NAME_FILE")
499             echo "INFO: Fetching schema $release_schema"
500             wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/${release_schema}
501             verify_schema
502         fi
503         set_variables_packagecloud
504         verify_packagecloud_match_release
505         for name in $(yq -r '.packages[].name' $release_file); do
506             package=$name
507             packagecloud_verify "$package" "$packagecloud_account"
508             if [[ "$JOB_NAME" =~ "merge" ]] && ! $DRY_RUN; then
509                 packagecloud_promote "$package" "$packagecloud_account"
510             fi
511         done
512         ;;
513
514     *)
515         echo "ERROR: distribution_type: $DISTRIBUTION_TYPE not supported"
516         exit 1
517         ;;
518 esac
519
520 echo "---> release-job.sh ends"