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