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 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 idna==2.8 lftools jsonschema niet twine yq"
20 # show installed versions
21 python -m pip --version
26 set_variables_common(){
27 echo "INFO: Setting common variables"
28 if [[ -z ${LOGS_SERVER:-} ]]; then
29 echo "ERROR: LOGS_SERVER 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 # Jenkins parameter drop-down defaults DISTRIBUTION_TYPE to None
52 # in the contain/maven release job; get value from release yaml.
53 # Packagecloud and PyPI jobs set the appropriate value.
54 DISTRIBUTION_TYPE="${DISTRIBUTION_TYPE:-None}"
55 if [[ $DISTRIBUTION_TYPE == "None" ]]; then
56 if ! DISTRIBUTION_TYPE=$(niet ".distribution_type" "$release_file"); then
57 echo "ERROR: Failed to get distribution_type from $release_file"
62 PATCH_DIR=$(mktemp -d)
64 # Displaying Release Information (Common variables)
65 printf "\t%-30s\n" RELEASE_ENVIRONMENT_INFO:
66 printf "\t%-30s %s\n" RELEASE_FILE: "$release_file"
67 printf "\t%-30s %s\n" LOGS_SERVER: "$LOGS_SERVER"
68 printf "\t%-30s %s\n" NEXUS_PATH: "$NEXUS_PATH"
69 printf "\t%-30s %s\n" JENKINS_HOSTNAME: "$JENKINS_HOSTNAME"
70 printf "\t%-30s %s\n" SILO: "$SILO"
71 printf "\t%-30s %s\n" PROJECT: "$PROJECT"
72 printf "\t%-30s %s\n" PROJECT-DASHED: "${PROJECT//\//-}"
73 printf "\t%-30s %s\n" DISTRIBUTION_TYPE: "$DISTRIBUTION_TYPE"
76 set_variables_maven(){
77 echo "INFO: Setting maven variables"
78 if [[ -z ${VERSION:-} ]]; then
79 VERSION=$(niet ".version" "$release_file")
81 if [[ -z ${GIT_TAG:-} ]]; then
82 if grep -q "git_tag" "$release_file" ; then
83 GIT_TAG=$(niet ".git_tag" "$release_file")
88 if [[ -z ${LOG_DIR:-} ]]; then
89 LOG_DIR=$(niet ".log_dir" "$release_file")
91 LOGS_URL="${LOGS_SERVER}/${NEXUS_PATH}${LOG_DIR}"
92 LOGS_URL=${LOGS_URL%/} # strip any trailing '/'
94 # Continuing displaying Release Information (Maven)
95 printf "\t%-30s\n" RELEASE_MAVEN_INFO:
96 printf "\t%-30s %s\n" VERSION: "$VERSION"
97 printf "\t%-30s %s\n" GIT_TAG: "$GIT_TAG"
98 printf "\t%-30s %s\n" LOG_DIR: "$LOG_DIR"
99 printf "\t%-30s %s\n" LOGS_URL: "$LOGS_URL"
102 set_variables_container(){
103 echo "INFO: Setting container variables"
104 if [[ -z ${VERSION:-} ]]; then
105 VERSION=$(niet ".container_release_tag" "$release_file")
107 if [[ -z ${GIT_TAG:-} ]]; then
108 if grep -q "git_tag" "$release_file" ; then
109 GIT_TAG=$(niet ".git_tag" "$release_file")
114 if grep -q "container_pull_registry" "$release_file" ; then
115 CONTAINER_PULL_REGISTRY=$(niet ".container_pull_registry" "$release_file")
117 if grep -q "container_push_registry" "$release_file" ; then
118 CONTAINER_PUSH_REGISTRY=$(niet ".container_push_registry" "$release_file")
120 # Make sure both pull and push registries are defined
121 if [ -z ${CONTAINER_PULL_REGISTRY+x} ] || [ -z ${CONTAINER_PUSH_REGISTRY+x} ]; then
122 echo "ERROR: CONTAINER_PULL_REGISTRY and CONTAINER_PUSH_REGISTRY need to be defined"
125 ref=$(niet ".ref" "$release_file")
127 # Continuing displaying Release Information (Container)
128 printf "\t%-30s\n" RELEASE_CONTAINER_INFO:
129 printf "\t%-30s %s\n" CONTAINER_RELEASE_TAG: "$VERSION"
130 printf "\t%-30s %s\n" CONTAINER_PULL_REGISTRY: "$CONTAINER_PULL_REGISTRY"
131 printf "\t%-30s %s\n" CONTAINER_PUSH_REGISTRY: "$CONTAINER_PUSH_REGISTRY"
132 printf "\t%-30s %s\n" GERRIT_REF_TO_TAG: "$ref"
133 printf "\t%-30s %s\n" GIT_TAG: "$GIT_TAG"
136 set_variables_pypi(){
137 echo "INFO: Setting pypi variables"
138 if [[ -z ${LOG_DIR:-} ]]; then
139 LOG_DIR=$(niet ".log_dir" "$release_file")
141 LOGS_URL="${LOGS_SERVER}/${NEXUS_PATH}${LOG_DIR}"
142 LOGS_URL=${LOGS_URL%/} # strip any trailing '/'
143 if [[ -z ${PYPI_PROJECT:-} ]]; then
144 PYPI_PROJECT=$(niet ".pypi_project" "$release_file")
146 if [[ -z ${PYTHON_VERSION:-} ]]; then
147 PYTHON_VERSION=$(niet ".python_version" "$release_file")
149 if [[ -z ${VERSION:-} ]]; then
150 VERSION=$(niet ".version" "$release_file")
152 if [[ -z ${GIT_TAG:-} ]]; then
153 if grep -q "git_tag" "$release_file" ; then
154 GIT_TAG=$(niet ".git_tag" "$release_file")
160 # Continuing displaying Release Information (pypi)
161 printf "\t%-30s\n" RELEASE_PYPI_INFO:
162 printf "\t%-30s %s\n" LOG_DIR: "$LOG_DIR"
163 printf "\t%-30s %s\n" LOGS_URL: "$LOGS_URL"
164 printf "\t%-30s %s\n" PYPI_INDEX: "$PYPI_INDEX" # from job configuration
165 printf "\t%-30s %s\n" PYPI_PROJECT: "$PYPI_PROJECT"
166 printf "\t%-30s %s\n" PYTHON_VERSION: "$PYTHON_VERSION"
167 printf "\t%-30s %s\n" VERSION: "$VERSION"
168 printf "\t%-30s %s\n" GIT_TAG: "$GIT_TAG"
171 set_variables_packagecloud(){
172 echo "INFO: Setting packagecloud variables"
173 if [[ -z ${VERSION:-} ]]; then
174 VERSION=$(niet ".version" "$release_file")
176 if [[ -z ${GIT_TAG:-} ]]; then
177 if grep -q "git_tag" $release_file ; then
178 GIT_TAG=$(niet ".git_tag" "$release_file")
183 if [[ -z ${LOG_DIR:-} ]]; then
184 LOG_DIR=$(niet ".log_dir" "$release_file")
186 if [[ -z ${REF:-} ]]; then
187 REF=$(niet ".ref" "$release_file")
189 if [[ -z ${PACKAGE_NAME:-} ]]; then
190 PACKAGE_NAME=$(niet ".package_name" "$release_file")
192 logs_url="${LOGS_SERVER}/${NEXUS_PATH}${LOG_DIR}"
193 logs_url=${logs_url%/} # strip any trailing '/'
195 printf "\t%-30s %s\n" PACKAGE_NAME: "$PACKAGE_NAME"
196 printf "\t%-30s %s\n" LOG_DIR: "$LOG_DIR"
197 printf "\t%-30s %s\n" LOGS_URL: "$logs_url"
198 printf "\t%-30s %s\n" GERRIT_REF_TO_TAG: "$REF"
199 printf "\t%-30s %s\n" VERSION: "$VERSION"
200 printf "\t%-30s %s\n" GIT_TAG: "$GIT_TAG"
204 echo "INFO: Verifying $release_file against schema $release_schema"
205 lftools schema verify "$release_file" "$release_schema"
209 # Verify allowed patterns "#.#.#" (SemVer) or "v#.#.#"
210 echo "INFO: Verifying version $VERSION"
211 allowed_version_regex="^((v?)([0-9]+)\.([0-9]+)\.([0-9]+))$"
212 if [[ $VERSION =~ $allowed_version_regex ]]; then
213 echo "INFO: The version $VERSION is valid"
215 echo "ERROR: The version $VERSION is not valid"
216 echo "ERROR: Valid versions are \"#.#.#\" (SemVer) or \"v#.#.#\""
217 echo "ERROR: See https://semver.org/ for more details on SemVer"
222 verify_version_match_release(){
223 echo "INFO: Fetching console log from $LOGS_URL"
224 wget -P /tmp "${LOGS_URL}/"console.log.gz
225 echo "INFO: Searching for uploaded step and version $VERSION in job log"
226 if zgrep "Successfully uploaded" /tmp/console.log.gz | grep "$VERSION"; then
227 echo "INFO: found expected strings in job log"
229 echo "ERROR: Defined version in release file does not match staging repo artifacts version to be released"
230 echo "ERROR: Please make sure maven stage job log dir and release version are both correct"
235 # check prerequisites to detect mistakes in the release YAML file
236 verify_pypi_match_release(){
237 echo "INFO: Fetching console log from $LOGS_URL"
238 wget -q -P /tmp "${LOGS_URL}/"console.log.gz
239 echo "INFO: Searching for uploaded step, project $PYPI_PROJECT and version $VERSION in job log"
240 # pypi-upload.sh generates success message with file list
241 if zgrep -i "uploaded" /tmp/console.log.gz | grep "$PYPI_PROJECT" | grep "$VERSION" ; then
242 echo "INFO: found expected strings in job log"
244 echo "ERROR: failed to find expected strings in job log"
249 verify_packagecloud_match_release(){
250 echo "INFO: Fetching console log from $logs_url"
251 wget -q -P /tmp "${logs_url}/"console.log.gz
252 echo "INFO: Searching for uploaded step, package name $PACKAGE_NAME and version $VERSION in job log"
253 if zgrep -E "Pushing.*$PACKAGE_NAME.*$VERSION.*success\!" /tmp/console.log.gz; then
254 echo "INFO: found expected strings in job log"
256 echo "ERROR: failed to find expected strings in job log"
261 # sigul is only available on Centos
262 # TODO: write tag-github-repo function
264 echo "INFO: tag gerrit with $GIT_TAG"
265 # Import public signing key
266 gpg --import "$SIGNING_PUBKEY"
267 if type=$(git cat-file -t "$GIT_TAG"); then
268 if [[ $type == "tag" ]]; then
269 echo "INFO: Repo already has signed tag $GIT_TAG, nothing to do"
271 echo "ERROR: Repo has lightweight tag $GIT_TAG, blocks push of signed tag"
275 echo "INFO: Repo has not yet been tagged $GIT_TAG"
276 git tag -am "${PROJECT//\//-} $GIT_TAG" "$GIT_TAG"
277 sigul --batch -c "$SIGUL_CONFIG" sign-git-tag "$SIGUL_KEY" "$GIT_TAG" < "$SIGUL_PASSWORD"
278 echo "INFO: Showing latest signature for $PROJECT:"
279 echo "INFO: git tag -v $GIT_TAG"
280 git tag -v "$GIT_TAG"
282 ########## Merge Part ##############
283 if [[ "$JOB_NAME" =~ "merge" ]] && [[ "$DRY_RUN" = false ]]; then
284 echo "INFO: Running merge, pushing tag"
285 gerrit_ssh=$(echo "$GERRIT_URL" | awk -F"/" '{print $3}')
286 git remote set-url origin ssh://"$RELEASE_USERNAME"@"$gerrit_ssh":29418/"$PROJECT"
287 git config user.name "$RELEASE_USERNAME"
288 git config user.email "$RELEASE_EMAIL"
289 echo -e "Host $gerrit_ssh\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
290 chmod 600 ~/.ssh/config
291 git push origin "$GIT_TAG"
297 echo "INFO: Processing nexus release"
298 for staging_url in $(zcat "$PATCH_DIR"/staging-repo.txt.gz | awk -e '{print $2}'); do
299 # extract the domain name from URL
300 NEXUS_URL=$(echo "$staging_url" | sed -e 's|^[^/]*//||' -e 's|/.*$||')
301 echo "INFO: NEXUS_URL: $NEXUS_URL"
302 # extract the staging repo from URL
303 STAGING_REPO=${staging_url#*repositories/}
304 echo "INFO: Running Nexus Verify"
305 lftools nexus release -v --server https://"$NEXUS_URL" "$STAGING_REPO"
306 echo "INFO: Merge will run:"
307 echo "lftools nexus release --server https://$NEXUS_URL $STAGING_REPO"
310 #Run the loop twice, to catch errors on either nexus repo
311 if [[ "$JOB_NAME" =~ "merge" ]] && [[ "$DRY_RUN" = false ]]; then
312 for staging_url in $(zcat "$PATCH_DIR"/staging-repo.txt.gz | awk -e '{print $2}'); do
313 NEXUS_URL=$(echo "$staging_url" | sed -e 's|^[^/]*//||' -e 's|/.*$||')
314 STAGING_REPO=${staging_url#*repositories/}
315 echo "INFO: Promoting $STAGING_REPO on $NEXUS_URL."
316 lftools nexus release --server https://"$NEXUS_URL" "$STAGING_REPO"
321 container_release_file(){
322 echo "INFO: Processing container release"
324 lfn_umbrella="$(echo "$GERRIT_URL" | awk -F"." '{print $2}')"
326 for namequoted in $(yq '.containers[].name' $release_file); do
327 versionquoted=$(yq ".containers[] |select(.name==$namequoted) |.version" $release_file)
329 #Remove extra yaml quotes
330 name="${namequoted#\"}"
332 version="${versionquoted#\"}"
333 version="${version%\"}"
337 echo "INFO: Merge will release $name $version as $VERSION"
338 # Attempt to pull from releases registry to see if the image has been released.
339 if docker pull "$CONTAINER_PUSH_REGISTRY"/"$lfn_umbrella"/"$name":"$VERSION"; then
340 echo "INFO: $VERSION is already released for image $name, Continuing..."
342 echo "INFO: $VERSION not found in releases, release will be prepared. Continuing..."
343 docker pull "$CONTAINER_PULL_REGISTRY"/"$lfn_umbrella"/"$name":"$version"
344 container_image_id=$(docker images | grep "$name" | grep "$version" | awk '{print $3}')
345 echo "INFO: Merge will run the following commands:"
346 echo "docker tag $container_image_id $CONTAINER_PUSH_REGISTRY/$lfn_umbrella/$name:$VERSION"
347 echo "docker push $CONTAINER_PUSH_REGISTRY/$lfn_umbrella/$name:$VERSION"
348 if [[ "$JOB_NAME" =~ "merge" ]]; then
349 docker tag "$container_image_id" "$CONTAINER_PUSH_REGISTRY"/"$lfn_umbrella"/"$name":"$VERSION"
350 docker push "$CONTAINER_PUSH_REGISTRY"/"$lfn_umbrella"/"$name":"$VERSION"
352 echo "#########################"
356 echo "INFO: Merge will tag ref: $ref"
361 maven_release_file(){
362 echo "INFO: Processing maven release"
363 echo "INFO: wget -P $PATCH_DIR ${LOGS_URL}/staging-repo.txt.gz"
364 wget -P "$PATCH_DIR" "${LOGS_URL}/"staging-repo.txt.gz
366 echo "INFO: wget ${LOGS_URL}/patches/{${PROJECT//\//-}.bundle,taglist.log.gz}"
367 wget "${LOGS_URL}"/patches/{"${PROJECT//\//-}".bundle,taglist.log.gz}
368 gunzip taglist.log.gz
369 cat "$PATCH_DIR"/taglist.log
371 git checkout "$(awk '{print $NF}' "$PATCH_DIR/taglist.log")"
372 git fetch "$PATCH_DIR/${PROJECT//\//-}.bundle"
373 git merge --ff-only FETCH_HEAD
378 # calls pip to download binary and source distributions from the specified index,
379 # which requires a recent-in-2019 version. Uploads the files it received.
381 echo "INFO: Processing pypi release"
384 pip_pfx="pip download -d $tgtdir --no-deps --python-version $PYTHON_VERSION -i $PYPI_INDEX"
385 module="$PYPI_PROJECT==$VERSION"
386 pip_bin="$pip_pfx $module"
387 echo "INFO: downloading binary: $pip_bin"
389 echo "WARN: failed to download binary distribution"
391 pip_src="$pip_pfx --no-binary=:all: $module"
392 echo "INFO: downloading source: $pip_src"
394 echo "WARN: failed to download source distribution"
396 echo "INFO: Checking files in $tgtdir"
397 # shellcheck disable=SC2012
398 filecount=$(ls $tgtdir | wc -l)
399 if [[ $filecount = 0 ]] ; then
400 echo "ERROR: no files downloaded"
403 # shellcheck disable=SC2046
404 echo "INFO: downloaded $filecount distributions: " $(ls $tgtdir)
407 if [[ ! "$JOB_NAME" =~ "merge" ]] ; then
408 echo "INFO: not a merge job, not uploading files"
412 cmd="twine upload -r $REPOSITORY $tgtdir/*"
414 echo "INFO: dry-run is set, echoing command only"
417 echo "INFO: uploading $filecount distributions to repo $REPOSITORY"
423 packagecloud_verify(){
424 echo "INFO: Verifying $1 exists in staging..."
425 if [[ $1 == $(curl --netrc-file ~/packagecloud_api --silent \
426 https://packagecloud.io/api/v1/repos/"$2"/staging/search?q="$1" \
427 | yq -r .[].filename) ]]; then
428 echo "INFO: $1 exists in staging!"
429 echo "INFO: Existing package location: https://packagecloud.io$(curl \
430 --netrc-file ~/packagecloud_api --silent \
431 https://packagecloud.io/api/v1/repos/"$2"/staging/search?q="$1" \
432 | yq -r .[].package_html_url)"
434 echo "ERROR: $1 does not exist in staging"
439 packagecloud_promote(){
440 echo "INFO: Preparing to promote $1..."
441 promote_url="https://packagecloud.io$(curl --netrc-file ~/packagecloud_api \
442 --silent https://packagecloud.io/api/v1/repos/"$2"/staging/search?q="$1" \
443 | yq -r .[].promote_url)"
444 echo "INFO: Promoting $1 from staging to release"
445 curl --netrc-file ~/packagecloud_api -X POST -F \
446 destination="$2/release" "$promote_url" \
447 | echo "INFO: Promoted package location: \
448 https://packagecloud.io$(yq -r .package_html_url)"
453 ############################## End Function Declarations ################################
455 # Set common environment variables
458 # Determine the type of release:
459 # - container, release-container-schema.yaml
460 # - maven, release-schema.yaml
461 # - pypi, release-pypi-schema.yaml
463 case $DISTRIBUTION_TYPE in
466 if $USE_RELEASE_FILE ; then
467 release_schema="release-schema.yaml"
468 echo "INFO: Fetching schema $release_schema"
469 wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/$release_schema
474 verify_version_match_release
479 if $USE_RELEASE_FILE ; then
480 release_schema="release-container-schema.yaml"
481 echo "INFO: Fetching schema $release_schema"
482 wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/${release_schema}
485 set_variables_container
487 container_release_file
491 if $USE_RELEASE_FILE ; then
492 release_schema="release-pypi-schema.yaml"
493 echo "INFO: Fetching schema $release_schema"
494 wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/${release_schema}
499 verify_pypi_match_release
504 if $USE_RELEASE_FILE ; then
505 release_schema="release-packagecloud-schema.yaml"
506 packagecloud_account=$(cat "$ACCOUNT_NAME_FILE")
507 echo "INFO: Fetching schema $release_schema"
508 wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/${release_schema}
511 set_variables_packagecloud
512 verify_packagecloud_match_release
513 for name in $(yq -r '.packages[].name' $release_file); do
515 packagecloud_verify "$package" "$packagecloud_account"
516 if [[ "$JOB_NAME" =~ "merge" ]] && ! $DRY_RUN; then
517 packagecloud_promote "$package" "$packagecloud_account"
523 echo "ERROR: distribution_type: $DISTRIBUTION_TYPE not supported"
528 echo "---> release-job.sh ends"