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[nexus] jsonschema niet twine yq"
23 set_variables_common(){
24 echo "INFO: Setting all common variables"
25 LOGS_SERVER="${LOGS_SERVER:-None}"
26 if [ "${LOGS_SERVER}" == 'None' ]; then
27 echo "ERROR: log server not found"
30 NEXUS_PATH="${SILO}/${JENKINS_HOSTNAME}/"
31 # Verify if using release file or parameters
32 if $USE_RELEASE_FILE ; then
33 release_files=$(git diff-tree --no-commit-id -r "$GIT_COMMIT" --name-only -- "releases/" ".releases/")
34 if (( $(grep -c . <<<"$release_files") > 1 )); then
35 echo "INFO: RELEASE FILES ARE AS FOLLOWS: $release_files"
36 echo "ERROR: Committing multiple release files in the same commit OR rename/amend of existing files is not supported."
39 release_file="$release_files"
40 echo "INFO: RELEASE FILE: $release_files"
43 echo "INFO: This job is built with parameters, no release file needed. Continuing..."
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 VERSION="${VERSION:-None}"
68 if [[ $VERSION == "None" ]]; then
69 VERSION="$(niet ".version" "$release_file")"
71 LOG_DIR="${LOG_DIR:-None}"
72 if [[ $LOG_DIR == "None" ]]; then
73 LOG_DIR="$(niet ".log_dir" "$release_file")"
75 LOGS_URL="${LOGS_SERVER}/${NEXUS_PATH}${LOG_DIR}"
76 LOGS_URL=${LOGS_URL%/} # strip any trailing '/'
78 # Continuing displaying Release Information (Maven)
79 printf "\t%-30s\n" RELEASE_MAVEN_INFO:
80 printf "\t%-30s %s\n" VERSION: $VERSION
81 printf "\t%-30s %s\n" LOG_DIR: $LOG_DIR
82 printf "\t%-30s %s\n" LOGS_URL: $LOGS_URL
85 set_variables_container(){
86 VERSION="${VERSION:-None}"
87 if [[ $VERSION == "None" ]]; then
88 VERSION="$(niet ".container_release_tag" "$release_file")"
90 if grep -q "container_pull_registry" "$release_file" ; then
91 CONTAINER_PULL_REGISTRY="$(niet ".container_pull_registry" "$release_file")"
93 if grep -q "container_push_registry" "$release_file" ; then
94 CONTAINER_PUSH_REGISTRY="$(niet ".container_push_registry" "$release_file")"
96 # Make sure both pull and push registries are defined
97 if [ -z ${CONTAINER_PULL_REGISTRY+x} ] || [ -z ${CONTAINER_PUSH_REGISTRY+x} ]; then
98 echo "ERROR: CONTAINER_PULL_REGISTRY and CONTAINER_PUSH_REGISTRY need to be defined"
101 ref="$(niet ".ref" "$release_file")"
103 # Continuing displaying Release Information (Container)
104 printf "\t%-30s\n" RELEASE_CONTAINER_INFO:
105 printf "\t%-30s %s\n" CONTAINER_RELEASE_TAG: $VERSION
106 printf "\t%-30s %s\n" CONTAINER_PULL_REGISTRY: $CONTAINER_PULL_REGISTRY
107 printf "\t%-30s %s\n" CONTAINER_PUSH_REGISTRY: $CONTAINER_PUSH_REGISTRY
108 printf "\t%-30s %s\n" GERRIT_REF_TO_TAG: $ref
111 set_variables_pypi(){
112 # use Jenkins parameter if set; else get value from release file
113 echo "INFO: Setting pypi variables"
114 LOG_DIR="${LOG_DIR:-None}"
115 if [[ $LOG_DIR == "None" ]]; then
116 LOG_DIR="$(yq -er .log_dir "$release_file")"
118 LOGS_URL="${LOGS_SERVER}/${NEXUS_PATH}${LOG_DIR}"
119 LOGS_URL=${LOGS_URL%/} # strip any trailing '/'
120 PYPI_PROJECT="${PYPI_PROJECT:-None}"
121 if [[ $PYPI_PROJECT == "None" ]]; then
122 PYPI_PROJECT="$(yq -er .pypi_project "$release_file")"
124 PYTHON_VERSION="${PYTHON_VERSION:-None}"
125 if [[ $PYTHON_VERSION == "None" ]]; then
126 PYTHON_VERSION="$(yq -er .python_version "$release_file")"
128 VERSION="${VERSION:-None}"
129 if [[ $VERSION == "None" ]]; then
130 VERSION="$(yq -er .version "$release_file")"
133 # Continuing displaying Release Information (pypi)
134 printf "\t%-30s\n" RELEASE_PYPI_INFO:
135 printf "\t%-30s %s\n" LOG_DIR: "$LOG_DIR"
136 printf "\t%-30s %s\n" LOGS_URL: "$LOGS_URL"
137 printf "\t%-30s %s\n" PYPI_INDEX: "$PYPI_INDEX" # from job configuration
138 printf "\t%-30s %s\n" PYPI_PROJECT: "$PYPI_PROJECT"
139 printf "\t%-30s %s\n" PYTHON_VERSION: "$PYTHON_VERSION"
140 printf "\t%-30s %s\n" VERSION: "$VERSION"
144 echo "INFO: Verifying $release_file schema."
145 lftools schema verify "$release_file" "$RELEASE_SCHEMA"
149 # Verify allowed patterns "v#.#.#" or "#.#.#" aka SemVer
150 echo "INFO: Verifying version string $VERSION"
151 allowed_version_regex="^((v?)([0-9]+)\.([0-9]+)\.([0-9]+))$"
152 if [[ $VERSION =~ $allowed_version_regex ]]; then
153 echo "INFO: The version $VERSION is a valid semantic version"
155 echo "INFO: The version $VERSION is not a semantic valid version"
156 echo "INFO: Allowed versions are \"v#.#.#\" or \"#.#.#\" aka SemVer"
157 echo "INFO: See https://semver.org/ for more details on SemVer"
162 verify_version_match_release(){
163 wget -P /tmp "${LOGS_URL}/"console.log.gz
164 echo "INFO: Comparing version $VERSION with log snippet from maven-stage:"
165 if zgrep "Successfully uploaded" /tmp/console.log.gz | grep "$VERSION"; then
166 echo "INFO: version $VERSION matches maven-stage artifacts"
168 echo "ERROR: Defined version in release file does not match staging repo artifacts version to be released"
169 echo " Please make sure maven-stage job selected as candidate and release version are correct"
174 # check prerequisites to detect mistakes in the release YAML file
175 verify_pypi_match_release(){
176 wget -q -P /tmp "${LOGS_URL}/"console.log.gz
177 echo "INFO: Searching for strings >$PYPI_PROJECT< and >$VERSION< in job log"
178 # pypi-upload.sh generates success message with file list
179 if zgrep -i "uploaded" /tmp/console.log.gz | grep "$PYPI_PROJECT" | grep "$VERSION" ; then
180 echo "INFO: found expected strings in job log"
182 echo "ERROR: failed to find expected strings in job log"
187 # sigul is only available on Centos
188 # TODO: write tag_github function
190 # Import public signing key
191 gpg --import "$SIGNING_PUBKEY"
192 if git tag -v "$VERSION"; then
193 echo "OK: Repo already tagged $VERSION Continuting to release"
195 echo "INFO: Repo has not yet been tagged $VERSION"
196 git tag -am "${PROJECT//\//-} $VERSION" "$VERSION"
197 sigul --batch -c "$SIGUL_CONFIG" sign-git-tag "$SIGUL_KEY" "$VERSION" < "$SIGUL_PASSWORD"
198 echo "INFO: Showing latest signature for $PROJECT:"
199 echo "INFO: git tag -v $VERSION"
200 git tag -v "$VERSION"
202 ########## Merge Part ##############
203 if [[ "$JOB_NAME" =~ "merge" ]] && [[ "$DRY_RUN" = false ]]; then
204 echo "INFO: Running merge, pushing tag"
205 gerrit_ssh=$(echo "$GERRIT_URL" | awk -F"/" '{print $3}')
206 git remote set-url origin ssh://"$RELEASE_USERNAME"@"$gerrit_ssh":29418/"$PROJECT"
207 git config user.name "$RELEASE_USERNAME"
208 git config user.email "$RELEASE_EMAIL"
209 echo -e "Host $gerrit_ssh\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
210 chmod 600 ~/.ssh/config
211 git push origin "$VERSION"
217 for staging_url in $(zcat "$PATCH_DIR"/staging-repo.txt.gz | awk -e '{print $2}'); do
218 # extract the domain name from URL
219 NEXUS_URL=$(echo "$staging_url" | sed -e 's|^[^/]*//||' -e 's|/.*$||')
220 echo "INFO: NEXUS_URL: $NEXUS_URL"
221 # extract the staging repo from URL
222 STAGING_REPO=${staging_url#*repositories/}
223 echo "INFO: Running Nexus Verify"
224 lftools nexus release -v --server https://"$NEXUS_URL" "$STAGING_REPO"
225 echo "INFO: Merge will run:"
226 echo "lftools nexus release --server https://$NEXUS_URL $STAGING_REPO"
229 #Run the loop twice, to catch errors on either nexus repo
230 if [[ "$JOB_NAME" =~ "merge" ]] && [[ "$DRY_RUN" = false ]]; then
231 for staging_url in $(zcat "$PATCH_DIR"/staging-repo.txt.gz | awk -e '{print $2}'); do
232 NEXUS_URL=$(echo "$staging_url" | sed -e 's|^[^/]*//||' -e 's|/.*$||')
233 STAGING_REPO=${staging_url#*repositories/}
234 echo "INFO: Promoting $STAGING_REPO on $NEXUS_URL."
235 lftools nexus release --server https://"$NEXUS_URL" "$STAGING_REPO"
240 container_release_file(){
241 echo "INFO: Processing container release"
243 lfn_umbrella="$(echo "$GERRIT_HOST" | awk -F"." '{print $2}')"
245 for namequoted in $(cat $release_file | yq '.containers[].name'); do
246 versionquoted=$(cat $release_file | yq ".containers[] |select(.name==$namequoted) |.version")
248 #Remove extra yaml quotes
249 name="${namequoted#\"}"
251 version="${versionquoted#\"}"
252 version="${version%\"}"
256 echo "INFO: Merge will release $name $version as $VERSION"
257 # Attempt to pull from releases registry to see if the image has been released.
258 if docker pull "$CONTAINER_PUSH_REGISTRY"/"$lfn_umbrella"/"$name":"$VERSION"; then
259 echo "OK: $VERSION is already released for image $name, Continuing..."
261 echo "OK: $VERSION not found in releases, release will be prepared. Continuing..."
262 docker pull "$CONTAINER_PULL_REGISTRY"/"$lfn_umbrella"/"$name":"$version"
263 container_image_id="$(docker images | grep $name | grep $version | awk '{print $3}')"
264 echo "INFO: Merge will run the following commands:"
265 echo "docker tag $container_image_id $CONTAINER_PUSH_REGISTRY/$lfn_umbrella/$name:$VERSION"
266 echo "docker push $CONTAINER_PUSH_REGISTRY/$lfn_umbrella/$name:$VERSION"
267 if [[ "$JOB_NAME" =~ "merge" ]]; then
268 docker tag "$container_image_id" "$CONTAINER_PUSH_REGISTRY"/"$lfn_umbrella"/"$name":"$VERSION"
269 docker push "$CONTAINER_PUSH_REGISTRY"/"$lfn_umbrella"/"$name":"$VERSION"
271 echo "#########################"
275 echo "INFO: Merge will tag ref: $ref"
280 maven_release_file(){
281 echo "INFO: wget -P $PATCH_DIR ${LOGS_URL}/staging-repo.txt.gz"
282 wget -P "$PATCH_DIR" "${LOGS_URL}/"staging-repo.txt.gz
284 echo "INFO: wget ${LOGS_URL}/patches/{${PROJECT//\//-}.bundle,taglist.log.gz}"
285 wget "${LOGS_URL}"/patches/{"${PROJECT//\//-}".bundle,taglist.log.gz}
286 gunzip taglist.log.gz
287 cat "$PATCH_DIR"/taglist.log
289 git checkout "$(awk '{print $NF}' "$PATCH_DIR/taglist.log")"
290 git fetch "$PATCH_DIR/${PROJECT//\//-}.bundle"
291 git merge --ff-only FETCH_HEAD
296 # calls pip to download binary and source distributions from the specified index,
297 # which requires a recent-in-2019 version. Uploads the files it received.
301 pip_pfx="pip download -d $tgtdir --no-deps --python-version $PYTHON_VERSION -i $PYPI_INDEX"
302 module="$PYPI_PROJECT==$VERSION"
303 pip_bin="$pip_pfx $module"
304 echo "INFO: downloading binary: $pip_bin"
306 echo "WARN: failed to download binary distribution"
308 pip_src="$pip_pfx --no-binary=:all: $module"
309 echo "INFO: downloading source: $pip_src"
311 echo "WARN: failed to download source distribution"
313 echo "INFO: Checking files in $tgtdir"
314 filecount=$(ls $tgtdir | wc -l)
315 if [[ $filecount = 0 ]] ; then
316 echo "ERROR: no files downloaded"
319 # shellcheck disable=SC2046
320 echo "INFO: downloaded $filecount distributions: " $(ls $tgtdir)
323 if [[ ! "$JOB_NAME" =~ "merge" ]] ; then
324 echo "INFO: not a merge job, not uploading files"
328 cmd="twine upload -r $REPOSITORY $tgtdir/*"
330 echo "INFO: dry-run is set, echoing command only"
333 echo "INFO: uploading $filecount distributions to repo $REPOSITORY"
339 packagecloud_verify(){
340 echo "INFO: ---> Verifying $1 exists in staging..."
341 if [[ $1 == $(curl --netrc-file ~/packagecloud_api --silent \
342 https://packagecloud.io/api/v1/repos/"$2"/search?q="$1" \
343 | yq -r .[].filename) ]]; then
344 echo "INFO: $1 exists in staging!"
345 echo "INFO: Existing package location: https://packagecloud.io$(curl \
346 --netrc-file ~/packagecloud_api --silent \
347 https://packagecloud.io/api/v1/repos/"$2"/search?q="$1" \
348 | yq -r .[].package_html_url)"
350 echo "ERROR: $package_name does not exist in staging"
355 packagecloud_promote(){
356 echo "INFO: ---> Preparing to promote $1..."
357 promote_url="https://packagecloud.io$(curl --netrc-file ~/packagecloud_api \
358 --silent https://packagecloud.io/api/v1/repos/"$2"/search?q="$1" \
359 | yq -r .[].promote_url)"
360 echo "INFO: Promoting $1 from staging to release"
361 curl --netrc-file ~/packagecloud_api -X POST -F \
362 destination=o-ran-sc/release "$promote_url" \
363 | echo "INFO: Promoted package location: \
364 https://packagecloud.io$(yq -r .package_html_url)"
367 ############################## End Function Declarations ################################
369 # Set common environment variables
372 # Determine the type of release:
373 # - container, release-container-schema.yaml
374 # - maven, release-schema.yaml
375 # - pypi, release-pypi-schema.yaml
377 if [[ "$DISTRIBUTION_TYPE" == "maven" ]]; then
378 if $USE_RELEASE_FILE ; then
379 RELEASE_SCHEMA="release-schema.yaml"
380 echo "INFO: Fetching schema $RELEASE_SCHEMA"
381 wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/release-schema.yaml
386 verify_version_match_release
388 elif [[ "$DISTRIBUTION_TYPE" == "container" ]]; then
389 if $USE_RELEASE_FILE ; then
390 RELEASE_SCHEMA="release-container-schema.yaml"
391 echo "INFO: Fetching schema $RELEASE_SCHEMA"
392 wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/${RELEASE_SCHEMA}
395 set_variables_container
397 container_release_file
398 elif [[ "$DISTRIBUTION_TYPE" == "pypi" ]]; then
399 if $USE_RELEASE_FILE ; then
400 RELEASE_SCHEMA="release-pypi-schema.yaml"
401 echo "INFO: Fetching schema $RELEASE_SCHEMA"
402 wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/${RELEASE_SCHEMA}
407 verify_pypi_match_release
409 elif [[ "$DISTRIBUTION_TYPE" == "packagecloud" ]]; then
410 RELEASE_SCHEMA="release-packagecloud-schema.yaml"
411 package_name=$(yq -r '.package_name' $release_file)
412 username_repo="o-ran-sc/staging"
413 echo "INFO: Fetching schema $RELEASE_SCHEMA"
414 wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/${RELEASE_SCHEMA}
416 for name in $(yq -r '.package_name[].name' $release_file); do
418 packagecloud_verify "$package_name" "$username_repo"
419 if [[ "$JOB_NAME" =~ "merge" ]] && ! $DRY_RUN; then
420 packagecloud_promote "$package_name" "$username_repo"
424 echo "ERROR: distribution_type: $DISTRIBUTION_TYPE not supported"
428 echo "---> release-job.sh ends"