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