Merge "Add python templates to run tox on merge"
[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 #Python bits. Remove when centos 7.7 builder is avaliable.
15 if [ -d "/opt/pyenv" ]; then
16     echo "---> Setting up pyenv"
17     export PYENV_ROOT="/opt/pyenv"
18     export PATH="$PYENV_ROOT/bin:$PATH"
19 fi
20 PYTHONPATH=$(pwd)
21 export PYTHONPATH
22 pyenv local 3.6.4
23 export PYENV_VERSION="3.6.4"
24 pip install --user lftools[nexus] jsonschema niet yq
25
26 #Functions.
27
28 set_variables_common(){
29     echo "---> INFO: Setting all common variables"
30     LOGS_SERVER="${LOGS_SERVER:-None}"
31     if [ "${LOGS_SERVER}" == 'None' ]; then
32         echo "FAILED: log server not found"
33         exit 1
34     fi
35     NEXUS_PATH="${SILO}/${JENKINS_HOSTNAME}/"
36     # Verify if using release file or parameters
37     if $USE_RELEASE_FILE ; then
38         release_files=$(git diff-tree --no-commit-id -r "$GERRIT_PATCHSET_REVISION" --name-only -- "releases/" ".releases/")
39         if (( $(grep -c . <<<"$release_files") > 1 )); then
40           echo "---> INFO: RELEASE FILES ARE AS FOLLOWS: $release_files"
41           echo "---> ERROR: Committing multiple release files in the same commit OR rename/amend of existing files is not supported."
42           exit 1
43         else
44           release_file="$release_files"
45           echo "---> INFO: RELEASE FILE: $release_files"
46         fi
47     else
48         echo "This job is built with parameters, no release file needed. Continuing..."
49         release_file="None"
50     fi
51
52     DISTRIBUTION_TYPE="${DISTRIBUTION_TYPE:-None}"
53     if [[ $DISTRIBUTION_TYPE == "None" ]]; then
54         DISTRIBUTION_TYPE="$(niet ".distribution_type" "$release_file")"
55     fi
56
57     PATCH_DIR="$(mktemp -d)"
58
59     # Displaying Release Information (Common variables)
60     echo "RELEASE ENVIRONMENT INFO:"
61     echo "RELEASE_FILE: $release_file"
62     echo "LOGS_SERVER: $LOGS_SERVER"
63     echo "NEXUS_PATH: $NEXUS_PATH"
64     echo "JENKINS_HOSTNAME: $JENKINS_HOSTNAME"
65     echo "SILO: $SILO"
66     echo "PROJECT: $PROJECT"
67     echo "PROJECT-DASHED: ${PROJECT//\//-}"
68     echo "DISTRIBUTION_TYPE: $DISTRIBUTION_TYPE"
69 }
70
71 set_variables_maven(){
72     VERSION="${VERSION:-None}"
73     if [[ $VERSION == "None" ]]; then
74         VERSION="$(niet ".version" "$release_file")"
75     fi
76     LOG_DIR="${LOG_DIR:-None}"
77     if [[ $LOG_DIR == "None" ]]; then
78         LOG_DIR="$(niet ".log_dir" "$release_file")"
79     fi
80     LOGS_URL="${LOGS_SERVER}/${NEXUS_PATH}${LOG_DIR}"
81     LOGS_URL=${LOGS_URL%/}  # strip any trailing '/'
82
83     # Continuing displaying Release Information (Maven)
84     echo "RELEASE MAVEN INFO:"
85     echo "VERSION: $VERSION"
86     echo "LOG DIR: $LOG_DIR"
87     echo "LOGS URL: $LOGS_URL"
88 }
89
90 set_variables_container(){
91     VERSION="${VERSION:-None}"
92     if [[ $VERSION == "None" ]]; then
93         VERSION="$(niet ".container_release_tag" "$release_file")"
94     fi
95
96     ref="$(niet ".ref" "$release_file")"
97
98     # Continuing displaying Release Information (Container)
99     echo "RELEASE CONTAINER INFO:"
100     echo "CONTAINER_RELEASE_TAG: $VERSION"
101     echo "GERRIT_REF_TO_TAG: $ref"
102 }
103
104 verify_schema(){
105     echo "---> INFO: Verifying $release_file schema."
106     lftools schema verify "$release_file" "$RELEASE_SCHEMA"
107 }
108
109 verify_version(){
110     # Verify allowed versions
111     # Allowed versions are "v#.#.#" or "#.#.#" aka SemVer
112     allowed_version_regex="^((v?)([0-9]+)\.([0-9]+)\.([0-9]+))$"
113     if [[ ! $VERSION =~ $allowed_version_regex ]]; then
114         echo "The version $VERSION is not a semantic valid version"
115         echo "Allowed versions are \"v#.#.#\" or \"#.#.#\" aka SemVer"
116         echo "See https://semver.org/ for more details on SemVer"
117         exit 1
118     fi
119 }
120
121 tag(){
122     if git tag -v "$VERSION"; then
123         echo "---> OK: Repo already tagged $VERSION Continuting to release"
124     else
125         echo "---> INFO: Repo has not yet been tagged $VERSION"
126         git tag -am "${PROJECT//\//-} $VERSION" "$VERSION"
127         sigul --batch -c "$SIGUL_CONFIG" sign-git-tag "$SIGUL_KEY" "$VERSION" < "$SIGUL_PASSWORD"
128         echo "Showing latest signature for $PROJECT:"
129         gpg --import "$SIGNING_PUBKEY"
130         echo "git tag -v $VERSION"
131         git tag -v "$VERSION"
132
133         ########## Merge Part ##############
134         if [[ "$JOB_NAME" =~ "merge" ]] && [[ "$DRY_RUN" = false ]]; then
135             echo "--> INFO: Running merge"
136             gerrit_ssh=$(echo "$GERRIT_URL" | awk -F"/" '{print $3}')
137             git remote set-url origin ssh://"$RELEASE_USERNAME"@"$gerrit_ssh":29418/"$PROJECT"
138             git config user.name "$RELEASE_USERNAME"
139             git config user.email "$RELEASE_EMAIL"
140             echo -e "Host $gerrit_ssh\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
141             chmod 600 ~/.ssh/config
142             git push origin "$VERSION"
143         fi
144     fi
145 }
146
147 nexus_release(){
148     for staging_url in $(zcat "$PATCH_DIR"/staging-repo.txt.gz | awk -e '{print $2}'); do
149         # extract the domain name from URL
150         NEXUS_URL=$(echo "$staging_url" | sed -e 's|^[^/]*//||' -e 's|/.*$||')
151         echo "---> INFO: NEXUS_URL: $NEXUS_URL"
152         # extract the staging repo from URL
153         STAGING_REPO=${staging_url#*repositories/}
154         echo "Running Nexus Verify"
155         lftools nexus release -v --server https://"$NEXUS_URL" "$STAGING_REPO"
156         echo "Merge will run"
157         echo "lftools nexus release --server https://$NEXUS_URL $STAGING_REPO"
158     done
159
160     #Run the loop twice, to catch errors on either nexus repo
161     if [[ "$JOB_NAME" =~ "merge" ]] && [[ "$DRY_RUN" = false ]]; then
162         for staging_url in $(zcat "$PATCH_DIR"/staging-repo.txt.gz | awk -e '{print $2}'); do
163           NEXUS_URL=$(echo "$staging_url" | sed -e 's|^[^/]*//||' -e 's|/.*$||')
164           STAGING_REPO=${staging_url#*repositories/}
165           echo "Promoting $STAGING_REPO on $NEXUS_URL."
166           lftools nexus release --server https://"$NEXUS_URL" "$STAGING_REPO"
167         done
168     fi
169 }
170
171 container_release_file(){
172     echo "---> Processing container release"
173     local lfn_umbrella
174     lfn_umbrella="$(echo "$GERRIT_HOST" | awk -F"." '{print $2}')"
175
176     for namequoted in $(cat $release_file | yq '.containers[].name'); do
177         versionquoted=$(cat $release_file | yq ".containers[] |select(.name=="$namequoted") |.version")
178
179         #Remove extra yaml quotes
180         name="${namequoted#\"}"
181         name="${name%\"}"
182         version="${versionquoted#\"}"
183         version="${version%\"}"
184
185         echo "$name"
186         echo "$version"
187         echo "---> INFO: Merge will release $name $version as $VERSION"
188         #Pull from public, to see if we have already tagged this.
189         if docker pull "$DOCKER_REGISTRY":10002/"$lfn_umbrella"/"$name":"$VERSION"; then
190             echo "---> OK: $VERSION is already released for image $name, Continuing..."
191         else
192             echo "---> OK: $VERSION not found in releases, release will be prepared. Continuing..."
193             docker pull "$DOCKER_REGISTRY":10001/"$lfn_umbrella"/"$name":"$version"
194             container_image_id="$(docker images | grep $name | grep $version | awk '{print $3}')"
195             echo "---> INFO: Merge will run the following commands:"
196             echo "docker tag $container_image_id $DOCKER_REGISTRY:10002/$lfn_umbrella/$name:$VERSION"
197             echo "docker push $DOCKER_REGISTRY:10002/$lfn_umbrella/$name:$VERSION"
198             if [[ "$JOB_NAME" =~ "merge" ]]; then
199                 docker tag "$container_image_id" "$DOCKER_REGISTRY":10002/"$lfn_umbrella"/"$name":"$VERSION"
200                 docker push "$DOCKER_REGISTRY":10002/"$lfn_umbrella"/"$name":"$VERSION"
201             fi
202             echo "#########################"
203         fi
204     done
205
206     echo "---> INFO: Merge will tag ref: $ref"
207     git checkout "$ref"
208     tag
209 }
210
211 maven_release_file(){
212     echo "---> INFO: wget -P $PATCH_DIR ${LOGS_URL}/staging-repo.txt.gz"
213     wget -P "$PATCH_DIR" "${LOGS_URL}/"staging-repo.txt.gz
214     pushd "$PATCH_DIR"
215         echo "---> INFO: wget ${LOGS_URL}/patches/{${PROJECT//\//-}.bundle,taglist.log.gz}"
216         wget "${LOGS_URL}"/patches/{"${PROJECT//\//-}".bundle,taglist.log.gz}
217         gunzip taglist.log.gz
218         cat "$PATCH_DIR"/taglist.log
219     popd
220     git checkout "$(awk '{print $NF}' "$PATCH_DIR/taglist.log")"
221     git fetch "$PATCH_DIR/${PROJECT//\//-}.bundle"
222     git merge --ff-only FETCH_HEAD
223     nexus_release
224     tag
225 }
226
227 echo "########### Start Script release-job.sh ###################################"
228
229 # Check if this is a container or maven release: release-container-schema.yaml vs release-schema.yaml
230 # Logic to determine what we are releasing.
231 ##########################################
232
233 # Set common environment variables
234 set_variables_common
235
236 if [[ "$DISTRIBUTION_TYPE" == "maven" ]]; then
237     wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/release-schema.yaml
238     RELEASE_SCHEMA="release-schema.yaml"
239     if $USE_RELEASE_FILE ; then
240         verify_schema
241     fi
242     set_variables_maven
243     verify_version
244     maven_release_file
245 elif [[ "$DISTRIBUTION_TYPE" == "container" ]]; then
246     wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/release-container-schema.yaml
247     RELEASE_SCHEMA="release-container-schema.yaml"
248     verify_schema
249     set_variables_container
250     verify_version
251     container_release_file
252 else
253     echo "---> ERROR: distribution_type: $DISTRIBUTION_TYPE not supported"
254     echo "Must be maven or container"
255     exit 1
256 fi
257 ##########################################
258
259 echo "########### End Script release-job.sh ###################################"