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 lftools jsonschema niet twine yq"
23 set_variables_common(){
24 echo "INFO: Setting common variables"
25 if [[ -z ${LOGS_SERVER:-} ]]; then
26 echo "ERROR: LOGS_SERVER not defined"
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."
38 release_file="$release_files"
39 echo "INFO: RELEASE FILE: $release_files"
42 echo "INFO: This job is built with parameters, no release file needed."
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")"
52 PATCH_DIR="$(mktemp -d)"
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
66 set_variables_maven(){
67 echo "INFO: Setting maven variables"
68 if [[ -z ${VERSION:-} ]]; then
69 VERSION="$(niet ".version" "$release_file")"
71 if [[ -z ${GIT_TAG:-} ]]; then
72 if grep -q "git_tag" "$release_file" ; then
73 GIT_TAG="$(niet ".git_tag" "$release_file")"
78 if [[ -z ${LOG_DIR:-} ]]; then
79 LOG_DIR="$(niet ".log_dir" "$release_file")"
81 LOGS_URL="${LOGS_SERVER}/${NEXUS_PATH}${LOG_DIR}"
82 LOGS_URL=${LOGS_URL%/} # strip any trailing '/'
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
92 set_variables_container(){
93 echo "INFO: Setting container variables"
94 if [[ -z ${VERSION:-} ]]; then
95 VERSION="$(niet ".container_release_tag" "$release_file")"
97 if [[ -z ${GIT_TAG:-} ]]; then
98 if grep -q "git_tag" "$release_file" ; then
99 GIT_TAG="$(niet ".git_tag" "$release_file")"
104 if grep -q "container_pull_registry" "$release_file" ; then
105 CONTAINER_PULL_REGISTRY="$(niet ".container_pull_registry" "$release_file")"
107 if grep -q "container_push_registry" "$release_file" ; then
108 CONTAINER_PUSH_REGISTRY="$(niet ".container_push_registry" "$release_file")"
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"
115 ref="$(niet ".ref" "$release_file")"
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
126 set_variables_pypi(){
127 echo "INFO: Setting pypi variables"
128 if [[ -z ${LOG_DIR:-} ]]; then
129 LOG_DIR="$(niet ".log_dir" "$release_file")"
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")"
136 if [[ -z ${PYTHON_VERSION:-} ]]; then
137 PYTHON_VERSION="$(niet ".python_version" "$release_file")"
139 if [[ -z ${VERSION:-} ]]; then
140 VERSION="$(niet ".version" "$release_file")"
142 if [[ -z ${GIT_TAG:-} ]]; then
143 if grep -q "git_tag" "$release_file" ; then
144 GIT_TAG="$(niet ".git_tag" "$release_file")"
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
162 echo "INFO: Verifying $release_file against schema $release_schema"
163 lftools schema verify "$release_file" "$release_schema"
167 # Verify allowed patterns "#.#.#" (SemVer) or "v#.#.#"
168 echo "INFO: Verifying version $VERSION"
169 allowed_version_regex="^((v?)([0-9]+)\.([0-9]+)\.([0-9]+))$"
170 if [[ $VERSION =~ $allowed_version_regex ]]; then
171 echo "INFO: The version $VERSION is valid"
173 echo "ERROR: The version $VERSION is not valid"
174 echo "ERROR: Valid versions are \"#.#.#\" (SemVer) or \"v#.#.#\""
175 echo "ERROR: See https://semver.org/ for more details on SemVer"
180 verify_version_match_release(){
181 echo "INFO: Fetching console log from $LOGS_URL"
182 wget -P /tmp "${LOGS_URL}/"console.log.gz
183 echo "INFO: Searching for uploaded step and version $VERSION in job log"
184 if zgrep "Successfully uploaded" /tmp/console.log.gz | grep "$VERSION"; then
185 echo "INFO: found expected strings in job log"
187 echo "ERROR: Defined version in release file does not match staging repo artifacts version to be released"
188 echo "ERROR: Please make sure maven stage job log dir and release version are both correct"
193 # check prerequisites to detect mistakes in the release YAML file
194 verify_pypi_match_release(){
195 echo "INFO: Fetching console log from $LOGS_URL"
196 wget -q -P /tmp "${LOGS_URL}/"console.log.gz
197 echo "INFO: Searching for uploaded step, project $PYPI_PROJECT and version $VERSION in job log"
198 # pypi-upload.sh generates success message with file list
199 if zgrep -i "uploaded" /tmp/console.log.gz | grep "$PYPI_PROJECT" | grep "$VERSION" ; then
200 echo "INFO: found expected strings in job log"
202 echo "ERROR: failed to find expected strings in job log"
207 # sigul is only available on Centos
208 # TODO: write tag-github-repo function
210 echo "INFO: tag gerrit with $GIT_TAG"
211 # Import public signing key
212 gpg --import "$SIGNING_PUBKEY"
213 if type=$(git cat-file -t "$GIT_TAG"); then
214 if [[ $type == "tag" ]]; then
215 echo "INFO: Repo already has signed tag $GIT_TAG, nothing to do"
217 echo "ERROR: Repo has lightweight tag $GIT_TAG, blocks push of signed tag"
221 echo "INFO: Repo has not yet been tagged $GIT_TAG"
222 git tag -am "${PROJECT//\//-} $GIT_TAG" "$GIT_TAG"
223 sigul --batch -c "$SIGUL_CONFIG" sign-git-tag "$SIGUL_KEY" "$GIT_TAG" < "$SIGUL_PASSWORD"
224 echo "INFO: Showing latest signature for $PROJECT:"
225 echo "INFO: git tag -v $GIT_TAG"
226 git tag -v "$GIT_TAG"
228 ########## Merge Part ##############
229 if [[ "$JOB_NAME" =~ "merge" ]] && [[ "$DRY_RUN" = false ]]; then
230 echo "INFO: Running merge, pushing tag"
231 gerrit_ssh=$(echo "$GERRIT_URL" | awk -F"/" '{print $3}')
232 git remote set-url origin ssh://"$RELEASE_USERNAME"@"$gerrit_ssh":29418/"$PROJECT"
233 git config user.name "$RELEASE_USERNAME"
234 git config user.email "$RELEASE_EMAIL"
235 echo -e "Host $gerrit_ssh\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
236 chmod 600 ~/.ssh/config
237 git push origin "$GIT_TAG"
243 echo "INFO: Processing nexus release"
244 for staging_url in $(zcat "$PATCH_DIR"/staging-repo.txt.gz | awk -e '{print $2}'); do
245 # extract the domain name from URL
246 NEXUS_URL=$(echo "$staging_url" | sed -e 's|^[^/]*//||' -e 's|/.*$||')
247 echo "INFO: NEXUS_URL: $NEXUS_URL"
248 # extract the staging repo from URL
249 STAGING_REPO=${staging_url#*repositories/}
250 echo "INFO: Running Nexus Verify"
251 lftools nexus release -v --server https://"$NEXUS_URL" "$STAGING_REPO"
252 echo "INFO: Merge will run:"
253 echo "lftools nexus release --server https://$NEXUS_URL $STAGING_REPO"
256 #Run the loop twice, to catch errors on either nexus repo
257 if [[ "$JOB_NAME" =~ "merge" ]] && [[ "$DRY_RUN" = false ]]; then
258 for staging_url in $(zcat "$PATCH_DIR"/staging-repo.txt.gz | awk -e '{print $2}'); do
259 NEXUS_URL=$(echo "$staging_url" | sed -e 's|^[^/]*//||' -e 's|/.*$||')
260 STAGING_REPO=${staging_url#*repositories/}
261 echo "INFO: Promoting $STAGING_REPO on $NEXUS_URL."
262 lftools nexus release --server https://"$NEXUS_URL" "$STAGING_REPO"
267 container_release_file(){
268 echo "INFO: Processing container release"
270 lfn_umbrella="$(echo "$GERRIT_URL" | awk -F"." '{print $2}')"
272 for namequoted in $(cat $release_file | yq '.containers[].name'); do
273 versionquoted=$(cat $release_file | yq ".containers[] |select(.name==$namequoted) |.version")
275 #Remove extra yaml quotes
276 name="${namequoted#\"}"
278 version="${versionquoted#\"}"
279 version="${version%\"}"
283 echo "INFO: Merge will release $name $version as $VERSION"
284 # Attempt to pull from releases registry to see if the image has been released.
285 if docker pull "$CONTAINER_PUSH_REGISTRY"/"$lfn_umbrella"/"$name":"$VERSION"; then
286 echo "INFO: $VERSION is already released for image $name, Continuing..."
288 echo "INFO: $VERSION not found in releases, release will be prepared. Continuing..."
289 docker pull "$CONTAINER_PULL_REGISTRY"/"$lfn_umbrella"/"$name":"$version"
290 container_image_id="$(docker images | grep $name | grep $version | awk '{print $3}')"
291 echo "INFO: Merge will run the following commands:"
292 echo "docker tag $container_image_id $CONTAINER_PUSH_REGISTRY/$lfn_umbrella/$name:$VERSION"
293 echo "docker push $CONTAINER_PUSH_REGISTRY/$lfn_umbrella/$name:$VERSION"
294 if [[ "$JOB_NAME" =~ "merge" ]]; then
295 docker tag "$container_image_id" "$CONTAINER_PUSH_REGISTRY"/"$lfn_umbrella"/"$name":"$VERSION"
296 docker push "$CONTAINER_PUSH_REGISTRY"/"$lfn_umbrella"/"$name":"$VERSION"
298 echo "#########################"
302 echo "INFO: Merge will tag ref: $ref"
307 maven_release_file(){
308 echo "INFO: Processing maven release"
309 echo "INFO: wget -P $PATCH_DIR ${LOGS_URL}/staging-repo.txt.gz"
310 wget -P "$PATCH_DIR" "${LOGS_URL}/"staging-repo.txt.gz
312 echo "INFO: wget ${LOGS_URL}/patches/{${PROJECT//\//-}.bundle,taglist.log.gz}"
313 wget "${LOGS_URL}"/patches/{"${PROJECT//\//-}".bundle,taglist.log.gz}
314 gunzip taglist.log.gz
315 cat "$PATCH_DIR"/taglist.log
317 git checkout "$(awk '{print $NF}' "$PATCH_DIR/taglist.log")"
318 git fetch "$PATCH_DIR/${PROJECT//\//-}.bundle"
319 git merge --ff-only FETCH_HEAD
324 # calls pip to download binary and source distributions from the specified index,
325 # which requires a recent-in-2019 version. Uploads the files it received.
327 echo "INFO: Processing pypi release"
330 pip_pfx="pip download -d $tgtdir --no-deps --python-version $PYTHON_VERSION -i $PYPI_INDEX"
331 module="$PYPI_PROJECT==$VERSION"
332 pip_bin="$pip_pfx $module"
333 echo "INFO: downloading binary: $pip_bin"
335 echo "WARN: failed to download binary distribution"
337 pip_src="$pip_pfx --no-binary=:all: $module"
338 echo "INFO: downloading source: $pip_src"
340 echo "WARN: failed to download source distribution"
342 echo "INFO: Checking files in $tgtdir"
343 filecount=$(ls $tgtdir | wc -l)
344 if [[ $filecount = 0 ]] ; then
345 echo "ERROR: no files downloaded"
348 # shellcheck disable=SC2046
349 echo "INFO: downloaded $filecount distributions: " $(ls $tgtdir)
352 if [[ ! "$JOB_NAME" =~ "merge" ]] ; then
353 echo "INFO: not a merge job, not uploading files"
357 cmd="twine upload -r $REPOSITORY $tgtdir/*"
359 echo "INFO: dry-run is set, echoing command only"
362 echo "INFO: uploading $filecount distributions to repo $REPOSITORY"
368 packagecloud_verify(){
369 echo "INFO: Verifying $1 exists in staging..."
370 if [[ $1 == $(curl --netrc-file ~/packagecloud_api --silent \
371 https://packagecloud.io/api/v1/repos/"$2"/staging/search?q="$1" \
372 | yq -r .[].filename) ]]; then
373 echo "INFO: $1 exists in staging!"
374 echo "INFO: Existing package location: https://packagecloud.io$(curl \
375 --netrc-file ~/packagecloud_api --silent \
376 https://packagecloud.io/api/v1/repos/"$2"/staging/search?q="$1" \
377 | yq -r .[].package_html_url)"
379 echo "ERROR: $1 does not exist in staging"
384 packagecloud_promote(){
385 echo "INFO: Preparing to promote $1..."
386 promote_url="https://packagecloud.io$(curl --netrc-file ~/packagecloud_api \
387 --silent https://packagecloud.io/api/v1/repos/"$2"/staging/search?q="$1" \
388 | yq -r .[].promote_url)"
389 echo "INFO: Promoting $1 from staging to release"
390 curl --netrc-file ~/packagecloud_api -X POST -F \
391 destination="$2/release" "$promote_url" \
392 | echo "INFO: Promoted package location: \
393 https://packagecloud.io$(yq -r .package_html_url)"
396 ############################## End Function Declarations ################################
398 # Set common environment variables
401 # Determine the type of release:
402 # - container, release-container-schema.yaml
403 # - maven, release-schema.yaml
404 # - pypi, release-pypi-schema.yaml
406 case $DISTRIBUTION_TYPE in
409 if $USE_RELEASE_FILE ; then
410 release_schema="release-schema.yaml"
411 echo "INFO: Fetching schema $release_schema"
412 wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/$release_schema
417 verify_version_match_release
422 if $USE_RELEASE_FILE ; then
423 release_schema="release-container-schema.yaml"
424 echo "INFO: Fetching schema $release_schema"
425 wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/${release_schema}
428 set_variables_container
430 container_release_file
434 if $USE_RELEASE_FILE ; then
435 release_schema="release-pypi-schema.yaml"
436 echo "INFO: Fetching schema $release_schema"
437 wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/${release_schema}
442 verify_pypi_match_release
447 release_schema="release-packagecloud-schema.yaml"
448 package_name=$(yq -r '.package_name' $release_file)
449 packagecloud_account=$(cat "$ACCOUNT_NAME_FILE")
450 echo "INFO: Fetching schema $release_schema"
451 wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/${release_schema}
453 for name in $(yq -r '.package_name[].name' $release_file); do
455 packagecloud_verify "$package_name" "$packagecloud_account"
456 if [[ "$JOB_NAME" =~ "merge" ]] && ! $DRY_RUN; then
457 packagecloud_promote "$package_name" "$packagecloud_account"
463 echo "ERROR: distribution_type: $DISTRIBUTION_TYPE not supported"
468 echo "---> release-job.sh ends"