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