2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2019 The Linux Foundation and others.
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"
14 # shellcheck disable=SC1090
17 # Version controlled by JJB_VERSION
18 lf-activate-venv lftools pip idna==2.9 lftools jsonschema twine yq readline
20 # show installed versions
21 python -m pip --version
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"
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."
43 release_file="$release_files"
44 echo "INFO: RELEASE FILE: $release_files"
47 echo "INFO: This job is built with parameters, no release file needed."
51 if [[ -z ${LOG_DIR:-} ]]; then
52 LOG_DIR=$(yq -r ".log_dir" "$release_file")
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}"
59 logs_url=${logs_url%/} # strip any trailing '/'
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"
72 PATCH_DIR=$(mktemp -d)
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")
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"
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}"
98 set_variables_maven(){
99 echo "INFO: Setting maven variables"
100 if [[ -z ${VERSION:-} ]]; then
101 VERSION=$(yq -r ".version" "$release_file")
103 if [[ -z ${GIT_TAG:-} ]]; then
104 if grep -q "git_tag" "$release_file" ; then
105 GIT_TAG=$(yq -r ".git_tag" "$release_file")
110 if [[ -z ${LOG_DIR:-} ]]; then
111 LOG_DIR=$(yq -r ".log_dir" "$release_file")
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"
122 set_variables_container(){
123 echo "INFO: Setting container variables"
124 if [[ -z ${VERSION:-} ]]; then
125 VERSION=$(yq -r ".container_release_tag" "$release_file")
127 if [[ -z ${GIT_TAG:-} ]]; then
128 if grep -q "git_tag" "$release_file" ; then
129 GIT_TAG=$(yq -r ".git_tag" "$release_file")
134 if grep -q "container_pull_registry" "$release_file" ; then
135 CONTAINER_PULL_REGISTRY=$(yq -r ".container_pull_registry" "$release_file")
137 if grep -q "container_push_registry" "$release_file" ; then
138 CONTAINER_PUSH_REGISTRY=$(yq -r ".container_push_registry" "$release_file")
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"
145 ref=$(yq -r ".ref" "$release_file")
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"
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")
162 if [[ -z ${PYPI_PROJECT:-} ]]; then
163 PYPI_PROJECT=$(yq -r ".pypi_project" "$release_file")
165 if [[ -z ${PYTHON_VERSION:-} ]]; then
166 PYTHON_VERSION=$(yq -r ".python_version" "$release_file")
168 if [[ -z ${VERSION:-} ]]; then
169 VERSION=$(yq -r ".version" "$release_file")
171 if [[ -z ${GIT_TAG:-} ]]; then
172 if grep -q "git_tag" "$release_file" ; then
173 GIT_TAG=$(yq -r ".git_tag" "$release_file")
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"
190 set_variables_packagecloud(){
191 echo "INFO: Setting packagecloud variables"
192 if [[ -z ${VERSION:-} ]]; then
193 VERSION=$(yq -r ".version" "$release_file")
195 if [[ -z ${GIT_TAG:-} ]]; then
196 if grep -q "git_tag" $release_file ; then
197 GIT_TAG=$(yq -r ".git_tag" "$release_file")
202 if [[ -z ${LOG_DIR:-} ]]; then
203 LOG_DIR=$(yq -r ".log_dir" "$release_file")
205 if [[ -z ${REF:-} ]]; then
206 REF=$(yq -r ".ref" "$release_file")
208 if [[ -z ${PACKAGE_NAME:-} ]]; then
209 PACKAGE_NAME=$(yq -r ".package_name" "$release_file")
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"
221 echo "INFO: Verifying $release_file against schema $release_schema"
222 lftools schema verify "$release_file" "$release_schema"
227 # Override the regex for projects that do not follow https://semver.org
228 OVERRIDE_SEMVER_REGEX="${OVERRIDE_SEMVER_REGEX:-None}"
229 if [[ $OVERRIDE_SEMVER_REGEX == "None" ]]; then
230 # Use the semver regex taken from https://github.com/fsaintjacques/semver-tool
231 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-]+)*)?$"
233 semver_regex="${OVERRIDE_SEMVER_REGEX}"
236 # Verify SemVer "#.#.#" (SemVer) or "v#.#.#"
237 echo "INFO: Verifying version $VERSION"
238 if [[ $VERSION =~ $semver_regex ]]; then
239 echo "INFO: The version $VERSION is valid"
241 echo "ERROR: The version $VERSION is not valid"
242 echo "ERROR: Valid versions are \"#.#.#\" (SemVer) or \"v#.#.#\""
243 echo "ERROR: Valid version will be matched against \"${semver_regex}\""
244 echo "ERROR: Refer to https://semver.org/ for more details on SemVer"
245 echo "ERROR: Refer SemVer examples from https://github.com/fsaintjacques/semver-tool/#examples"
250 verify_version_match_release(){
251 echo "INFO: Fetching console log from $logs_url"
252 wget -P /tmp "${logs_url}/"console.log.gz
253 echo "INFO: Searching for uploaded step and version $VERSION in job log"
254 if zgrep "Successfully uploaded" /tmp/console.log.gz | grep "$VERSION"; then
255 echo "INFO: found expected strings in job log"
257 echo "ERROR: Defined version in release file does not match staging repo artifacts version to be released"
258 echo "ERROR: Please make sure maven stage job log dir and release version are both correct"
263 # check prerequisites to detect mistakes in the release YAML file
264 verify_pypi_match_release(){
265 echo "INFO: Fetching console log from $logs_url"
266 wget -q -P /tmp "${logs_url}/"console.log.gz
267 echo "INFO: Searching for uploaded step, project $PYPI_PROJECT and version $VERSION in job log"
268 # pypi-upload.sh generates success message with file list
269 if zgrep -i "uploaded" /tmp/console.log.gz | grep "$PYPI_PROJECT" | grep "$VERSION" ; then
270 echo "INFO: found expected strings in job log"
272 echo "ERROR: failed to find expected strings in job log"
277 verify_packagecloud_match_release(){
278 echo "INFO: Fetching console log from $logs_url"
279 wget -q -P /tmp "${logs_url}/"console.log.gz
280 echo "INFO: Searching for uploaded step, package name $PACKAGE_NAME and version $VERSION in job log"
281 if zgrep -E "Pushing.*$PACKAGE_NAME.*$VERSION.*success\!" /tmp/console.log.gz; then
282 echo "INFO: found expected strings in job log"
284 echo "ERROR: failed to find expected strings in job log"
289 # sigul is only available on Centos
290 # TODO: write tag-github-repo function
292 if [[ $TAG_RELEASE == false ]]; then
293 echo "INFO: Skipping code repo tag"
297 if [[ -z ${GERRIT_URL:-} ]]; then
303 echo "INFO: tag repo with $GIT_TAG"
304 # Import public signing key
305 gpg --import "$SIGNING_PUBKEY"
306 if type=$(git cat-file -t "$GIT_TAG"); then
307 if [[ $type == "tag" ]]; then
308 echo "INFO: Repo already has signed tag $GIT_TAG, nothing to do"
310 echo "ERROR: Repo has lightweight tag $GIT_TAG, blocks push of signed tag"
314 echo "INFO: Repo has not yet been tagged $GIT_TAG"
315 git tag -am "${PROJECT//\//-} $GIT_TAG" "$GIT_TAG"
316 sigul --batch -c "$SIGUL_CONFIG" sign-git-tag "$SIGUL_KEY" "$GIT_TAG" < "$SIGUL_PASSWORD"
317 echo "INFO: Showing latest signature for $PROJECT:"
318 echo "INFO: git tag -v $GIT_TAG"
319 git tag -v "$GIT_TAG"
321 ########## Merge Part ##############
322 if [[ "$JOB_NAME" =~ "merge" ]] && [[ "$DRY_RUN" = false ]]; then
323 echo "INFO: Running merge, pushing tag"
324 if [[ $GIT_REPO_BASE == "gerrit" ]]; then
325 gerrit_ssh=$(echo "$GERRIT_URL" | awk -F"/" '{print $3}')
326 git remote set-url origin ssh://"$RELEASE_USERNAME"@"$gerrit_ssh":29418/"$PROJECT"
327 echo -e "Host $gerrit_ssh\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
328 chmod 600 ~/.ssh/config
330 git remote set-url origin "$GIT_CLONE_URL""$RELEASE_USERNAME"/"$PROJECT".git
332 git config user.name "$RELEASE_USERNAME"
333 git config user.email "$RELEASE_EMAIL"
334 git push origin "$GIT_TAG"
340 echo "INFO: Processing nexus release"
341 for staging_url in $(zcat "$PATCH_DIR"/staging-repo.txt.gz | awk -e '{print $2}'); do
342 # extract the domain name from URL
343 NEXUS_URL=$(echo "$staging_url" | sed -e 's|^[^/]*//||' -e 's|/.*$||')
344 echo "INFO: NEXUS_URL: $NEXUS_URL"
345 # extract the staging repo from URL
346 STAGING_REPO=${staging_url#*repositories/}
347 echo "INFO: Running Nexus Verify"
348 lftools nexus release -v --server https://"$NEXUS_URL" "$STAGING_REPO"
349 echo "INFO: Merge will run:"
350 echo "lftools nexus release --server https://$NEXUS_URL $STAGING_REPO"
353 #Run the loop twice, to catch errors on either nexus repo
354 if [[ "$JOB_NAME" =~ "merge" ]] && [[ "$DRY_RUN" = false ]]; then
355 for staging_url in $(zcat "$PATCH_DIR"/staging-repo.txt.gz | awk -e '{print $2}'); do
356 NEXUS_URL=$(echo "$staging_url" | sed -e 's|^[^/]*//||' -e 's|/.*$||')
357 STAGING_REPO=${staging_url#*repositories/}
358 echo "INFO: Promoting $STAGING_REPO on $NEXUS_URL."
359 lftools nexus release --server https://"$NEXUS_URL" "$STAGING_REPO"
364 container_release_file(){
365 echo "INFO: Processing container release"
368 lfn_umbrella="$(echo "$GERRIT_URL" | awk -F"." '{print $2}')"
370 for namequoted in $(yq '.containers[].name' $release_file); do
371 versionquoted=$(yq ".containers[] |select(.name==$namequoted) |.version" $release_file)
373 #Remove extra yaml quotes
374 name="${namequoted#\"}"
376 version="${versionquoted#\"}"
377 version="${version%\"}"
381 echo "INFO: Merge will release $name $version as $VERSION"
382 # Attempt to pull from releases registry to see if the image has been released.
383 if docker pull "$CONTAINER_PUSH_REGISTRY"/"$lfn_umbrella"/"$name":"$VERSION"; then
384 echo "INFO: $VERSION is already released for image $name, Continuing..."
386 echo "INFO: $VERSION not found in releases, release will be prepared. Continuing..."
387 docker pull "$CONTAINER_PULL_REGISTRY"/"$lfn_umbrella"/"$name":"$version"
388 container_image_id=$(docker images | grep "$name" | grep "$version" | awk '{print $3}')
389 echo "INFO: Merge will run the following commands:"
390 echo "docker tag $container_image_id $CONTAINER_PUSH_REGISTRY/$lfn_umbrella/$name:$VERSION"
391 echo "docker push $CONTAINER_PUSH_REGISTRY/$lfn_umbrella/$name:$VERSION"
392 if [[ "$JOB_NAME" =~ "merge" ]]; then
393 docker tag "$container_image_id" "$CONTAINER_PUSH_REGISTRY"/"$lfn_umbrella"/"$name":"$VERSION"
394 docker push "$CONTAINER_PUSH_REGISTRY"/"$lfn_umbrella"/"$name":"$VERSION"
396 echo "#########################"
400 echo "INFO: Merge will tag ref: $ref"
405 maven_release_file(){
406 echo "INFO: Processing maven release"
407 echo "INFO: wget -P $PATCH_DIR ${logs_url}/staging-repo.txt.gz"
408 wget -P "$PATCH_DIR" "${logs_url}/"staging-repo.txt.gz
410 echo "INFO: wget ${logs_url}/patches/{${PROJECT//\//-}.bundle,taglist.log.gz}"
411 wget "${logs_url}"/patches/{"${PROJECT//\//-}".bundle,taglist.log.gz}
412 gunzip taglist.log.gz
413 cat "$PATCH_DIR"/taglist.log
415 git checkout "$(awk '{print $NF}' "$PATCH_DIR/taglist.log")"
416 git fetch "$PATCH_DIR/${PROJECT//\//-}.bundle"
417 git merge --ff-only FETCH_HEAD
422 # calls pip to download binary and source distributions from the specified index,
423 # which requires a recent-in-2019 version. Uploads the files it received.
425 echo "INFO: Processing pypi release"
428 pip_pfx="pip download -d $tgtdir --no-deps --python-version $PYTHON_VERSION -i $PYPI_INDEX"
429 module="$PYPI_PROJECT==$VERSION"
430 pip_bin="$pip_pfx $module"
431 echo "INFO: downloading binary: $pip_bin"
433 echo "WARN: failed to download binary distribution"
435 pip_src="$pip_pfx --no-binary=:all: $module"
436 echo "INFO: downloading source: $pip_src"
438 echo "WARN: failed to download source distribution"
440 echo "INFO: Checking files in $tgtdir"
441 # shellcheck disable=SC2012
442 filecount=$(ls $tgtdir | wc -l)
443 if [[ $filecount = 0 ]] ; then
444 echo "ERROR: no files downloaded"
447 # shellcheck disable=SC2046
448 echo "INFO: downloaded $filecount distributions: " $(ls $tgtdir)
451 if [[ ! "$JOB_NAME" =~ "merge" ]] ; then
452 echo "INFO: not a merge job, not uploading files"
456 cmd="twine upload -r $REPOSITORY $tgtdir/*"
458 echo "INFO: dry-run is set, echoing command only"
461 echo "INFO: uploading $filecount distributions to repo $REPOSITORY"
467 packagecloud_verify(){
468 echo "INFO: Verifying $1 exists in staging..."
469 if [[ $1 == $(curl --netrc-file ~/packagecloud_api --silent \
470 https://packagecloud.io/api/v1/repos/"$2"/staging/search?q="$1" \
471 | yq -r .[].filename) ]]; then
472 echo "INFO: $1 exists in staging!"
473 echo "INFO: Existing package location: https://packagecloud.io$(curl \
474 --netrc-file ~/packagecloud_api --silent \
475 https://packagecloud.io/api/v1/repos/"$2"/staging/search?q="$1" \
476 | yq -r .[].package_html_url)"
478 echo "ERROR: $1 does not exist in staging"
483 packagecloud_promote(){
484 echo "INFO: Preparing to promote $1..."
485 promote_url="https://packagecloud.io$(curl --netrc-file ~/packagecloud_api \
486 --silent https://packagecloud.io/api/v1/repos/"$2"/staging/search?q="$1" \
487 | yq -r .[].promote_url)"
488 echo "INFO: Promoting $1 from staging to release"
489 curl --netrc-file ~/packagecloud_api -X POST -F \
490 destination="$2/release" "$promote_url" \
491 | echo "INFO: Promoted package location: \
492 https://packagecloud.io$(yq -r .package_html_url)"
497 ############################## End Function Declarations ################################
499 # Set common environment variables
502 # Determine the type of release:
503 # - container, release-container-schema.yaml
504 # - maven, release-schema.yaml
505 # - pypi, release-pypi-schema.yaml
507 case $DISTRIBUTION_TYPE in
510 if $USE_RELEASE_FILE ; then
511 release_schema="release-schema.yaml"
512 echo "INFO: Fetching schema $release_schema"
513 wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/$release_schema
518 verify_version_match_release
523 if $USE_RELEASE_FILE ; then
524 release_schema="release-container-schema.yaml"
525 echo "INFO: Fetching schema $release_schema"
526 wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/${release_schema}
529 set_variables_container
531 container_release_file
535 if $USE_RELEASE_FILE ; then
536 release_schema="release-pypi-schema.yaml"
537 echo "INFO: Fetching schema $release_schema"
538 wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/${release_schema}
543 verify_pypi_match_release
548 if $USE_RELEASE_FILE ; then
549 release_schema="release-packagecloud-schema.yaml"
550 packagecloud_account=$(cat "$ACCOUNT_NAME_FILE")
551 echo "INFO: Fetching schema $release_schema"
552 wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/${release_schema}
555 set_variables_packagecloud
556 verify_packagecloud_match_release
557 for name in $(yq -r '.packages[].name' $release_file); do
559 packagecloud_verify "$package" "$packagecloud_account"
560 if [[ "$JOB_NAME" =~ "merge" ]] && ! $DRY_RUN; then
561 packagecloud_promote "$package" "$packagecloud_account"
567 echo "ERROR: distribution_type: $DISTRIBUTION_TYPE not supported"
572 echo "---> release-job.sh ends"