Fix WS CLI Agent calls
[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     # Import public signing key
123     gpg --import "$SIGNING_PUBKEY"
124     if git tag -v "$VERSION"; then
125         echo "---> OK: Repo already tagged $VERSION Continuting to release"
126     else
127         echo "---> INFO: Repo has not yet been tagged $VERSION"
128         git tag -am "${PROJECT//\//-} $VERSION" "$VERSION"
129         sigul --batch -c "$SIGUL_CONFIG" sign-git-tag "$SIGUL_KEY" "$VERSION" < "$SIGUL_PASSWORD"
130         echo "Showing latest signature for $PROJECT:"
131         echo "git tag -v $VERSION"
132         git tag -v "$VERSION"
133
134         ########## Merge Part ##############
135         if [[ "$JOB_NAME" =~ "merge" ]] && [[ "$DRY_RUN" = false ]]; then
136             echo "--> INFO: Running merge"
137             gerrit_ssh=$(echo "$GERRIT_URL" | awk -F"/" '{print $3}')
138             git remote set-url origin ssh://"$RELEASE_USERNAME"@"$gerrit_ssh":29418/"$PROJECT"
139             git config user.name "$RELEASE_USERNAME"
140             git config user.email "$RELEASE_EMAIL"
141             echo -e "Host $gerrit_ssh\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
142             chmod 600 ~/.ssh/config
143             git push origin "$VERSION"
144         fi
145     fi
146 }
147
148 nexus_release(){
149     for staging_url in $(zcat "$PATCH_DIR"/staging-repo.txt.gz | awk -e '{print $2}'); do
150         # extract the domain name from URL
151         NEXUS_URL=$(echo "$staging_url" | sed -e 's|^[^/]*//||' -e 's|/.*$||')
152         echo "---> INFO: NEXUS_URL: $NEXUS_URL"
153         # extract the staging repo from URL
154         STAGING_REPO=${staging_url#*repositories/}
155         echo "Running Nexus Verify"
156         lftools nexus release -v --server https://"$NEXUS_URL" "$STAGING_REPO"
157         echo "Merge will run"
158         echo "lftools nexus release --server https://$NEXUS_URL $STAGING_REPO"
159     done
160
161     #Run the loop twice, to catch errors on either nexus repo
162     if [[ "$JOB_NAME" =~ "merge" ]] && [[ "$DRY_RUN" = false ]]; then
163         for staging_url in $(zcat "$PATCH_DIR"/staging-repo.txt.gz | awk -e '{print $2}'); do
164           NEXUS_URL=$(echo "$staging_url" | sed -e 's|^[^/]*//||' -e 's|/.*$||')
165           STAGING_REPO=${staging_url#*repositories/}
166           echo "Promoting $STAGING_REPO on $NEXUS_URL."
167           lftools nexus release --server https://"$NEXUS_URL" "$STAGING_REPO"
168         done
169     fi
170 }
171
172 container_release_file(){
173     echo "---> Processing container release"
174     local lfn_umbrella
175     lfn_umbrella="$(echo "$GERRIT_HOST" | awk -F"." '{print $2}')"
176
177     for namequoted in $(cat $release_file | yq '.containers[].name'); do
178         versionquoted=$(cat $release_file | yq ".containers[] |select(.name=="$namequoted") |.version")
179
180         #Remove extra yaml quotes
181         name="${namequoted#\"}"
182         name="${name%\"}"
183         version="${versionquoted#\"}"
184         version="${version%\"}"
185
186         echo "$name"
187         echo "$version"
188         echo "---> INFO: Merge will release $name $version as $VERSION"
189         #Pull from public, to see if we have already tagged this.
190         if docker pull "$DOCKER_REGISTRY":10002/"$lfn_umbrella"/"$name":"$VERSION"; then
191             echo "---> OK: $VERSION is already released for image $name, Continuing..."
192         else
193             echo "---> OK: $VERSION not found in releases, release will be prepared. Continuing..."
194             docker pull "$DOCKER_REGISTRY":10001/"$lfn_umbrella"/"$name":"$version"
195             container_image_id="$(docker images | grep $name | grep $version | awk '{print $3}')"
196             echo "---> INFO: Merge will run the following commands:"
197             echo "docker tag $container_image_id $DOCKER_REGISTRY:10002/$lfn_umbrella/$name:$VERSION"
198             echo "docker push $DOCKER_REGISTRY:10002/$lfn_umbrella/$name:$VERSION"
199             if [[ "$JOB_NAME" =~ "merge" ]]; then
200                 docker tag "$container_image_id" "$DOCKER_REGISTRY":10002/"$lfn_umbrella"/"$name":"$VERSION"
201                 docker push "$DOCKER_REGISTRY":10002/"$lfn_umbrella"/"$name":"$VERSION"
202             fi
203             echo "#########################"
204         fi
205     done
206
207     echo "---> INFO: Merge will tag ref: $ref"
208     git checkout "$ref"
209     tag
210 }
211
212 maven_release_file(){
213     echo "---> INFO: wget -P $PATCH_DIR ${LOGS_URL}/staging-repo.txt.gz"
214     wget -P "$PATCH_DIR" "${LOGS_URL}/"staging-repo.txt.gz
215     pushd "$PATCH_DIR"
216         echo "---> INFO: wget ${LOGS_URL}/patches/{${PROJECT//\//-}.bundle,taglist.log.gz}"
217         wget "${LOGS_URL}"/patches/{"${PROJECT//\//-}".bundle,taglist.log.gz}
218         gunzip taglist.log.gz
219         cat "$PATCH_DIR"/taglist.log
220     popd
221     git checkout "$(awk '{print $NF}' "$PATCH_DIR/taglist.log")"
222     git fetch "$PATCH_DIR/${PROJECT//\//-}.bundle"
223     git merge --ff-only FETCH_HEAD
224     nexus_release
225     tag
226 }
227
228 echo "########### Start Script release-job.sh ###################################"
229
230 # Check if this is a container or maven release: release-container-schema.yaml vs release-schema.yaml
231 # Logic to determine what we are releasing.
232 ##########################################
233
234 # Set common environment variables
235 set_variables_common
236
237 if [[ "$DISTRIBUTION_TYPE" == "maven" ]]; then
238     wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/release-schema.yaml
239     RELEASE_SCHEMA="release-schema.yaml"
240     if $USE_RELEASE_FILE ; then
241         verify_schema
242     fi
243     set_variables_maven
244     verify_version
245     maven_release_file
246 elif [[ "$DISTRIBUTION_TYPE" == "container" ]]; then
247     wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/release-container-schema.yaml
248     RELEASE_SCHEMA="release-container-schema.yaml"
249     verify_schema
250     set_variables_container
251     verify_version
252     container_release_file
253 else
254     echo "---> ERROR: distribution_type: $DISTRIBUTION_TYPE not supported"
255     echo "Must be maven or container"
256     exit 1
257 fi
258 ##########################################
259
260 echo "########### End Script release-job.sh ###################################"