31721d11ecc253a4df7d133ce1aed491f05d8630
[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 container_release_file(){
86   echo "---> Processing container release"
87   # Container-release code is addressed in a new feature patchset.
88 }
89
90 maven_release_file(){
91   echo "---> INFO: wget -P $PATCH_DIR ${LOGS_URL}/staging-repo.txt.gz"
92   wget -P "$PATCH_DIR" "${LOGS_URL}/"staging-repo.txt.gz
93   pushd "$PATCH_DIR"
94     echo "---> INFO: wget ${LOGS_URL}/patches/{${PROJECT//\//-}.bundle,taglist.log.gz}"
95     wget "${LOGS_URL}"/patches/{"${PROJECT//\//-}".bundle,taglist.log.gz}
96     gunzip taglist.log.gz
97     cat "$PATCH_DIR"/taglist.log
98   popd
99   git checkout "$(awk '{print $NF}' "$PATCH_DIR/taglist.log")"
100   git fetch "$PATCH_DIR/${PROJECT//\//-}.bundle"
101   git merge --ff-only FETCH_HEAD
102   nexus_release
103   tag
104 }
105
106 echo "########### Start Script release-job.sh ###################################"
107 echo "---> INFO: Setting all VARS"
108
109 LOGS_SERVER="${LOGS_SERVER:-None}"
110 MAVEN_CENTRAL_URL="${MAVEN_CENTRAL_URL:-None}"
111 if [ "${LOGS_SERVER}" == 'None' ]; then
112     echo "FAILED: log server not found"
113     exit 1
114 fi
115
116 RELEASE_FILE="${RELEASE_FILE:-True}"
117 if [[ "$RELEASE_FILE" == "True" ]]; then
118
119   release_files=$(git diff-tree --no-commit-id -r "$GERRIT_PATCHSET_REVISION" --name-only -- "releases/" ".releases/")
120   if (( $(grep -c . <<<"$release_files") > 1 )); then
121     echo "---> INFO: RELEASE FILES ARE AS FOLLOWS: $release_files"
122     echo "---> ERROR: Committing multiple release files in the same commit OR rename/amend of existing files is not supported."
123     exit 1
124   else
125     release_file="$release_files"
126     echo "---> INFO: RELEASE FILE: $release_files"
127   fi
128
129 else
130   echo "This is job is built with parameters, no release file needed"
131   release_file="None"
132 fi
133
134
135 NEXUS_PATH="${SILO}/${JENKINS_HOSTNAME}/"
136
137 VERSION="${VERSION:-None}"
138 if [[ $VERSION == "None" ]]; then
139   VERSION="$(niet ".version" "$release_file")"
140 fi
141
142 LOG_DIR="${LOG_DIR:-None}"
143 if [[ $LOG_DIR == "None" ]]; then
144   LOG_DIR="$(niet ".log_dir" "$release_file")"
145 fi
146
147 DISTRIBUTION_TYPE="${DISTRIBUTION_TYPE:-None}"
148 if [[ $DISTRIBUTION_TYPE == "None" ]]; then
149   DISTRIBUTION_TYPE="$(niet ".distribution_type" "$release_file")"
150 fi
151
152 ####
153 LOGS_URL="${LOGS_SERVER}/${NEXUS_PATH}${LOG_DIR}"
154 LOGS_URL=${LOGS_URL%/}  # strip any trailing '/'
155 PATCH_DIR="$(mktemp -d)"
156 #INFO
157 echo "INFO:"
158 echo "RELEASE_FILE: $release_file"
159 echo "LOGS_SERVER: $LOGS_SERVER"
160 echo "NEXUS_PATH: $NEXUS_PATH"
161 echo "JENKINS_HOSTNAME: $JENKINS_HOSTNAME"
162 echo "SILO: $SILO"
163 echo "PROJECT: $PROJECT"
164 echo "PROJECT-DASHED: ${PROJECT//\//-}"
165 echo "VERSION: $VERSION"
166 echo "LOG DIR: $LOG_DIR"
167 echo "LOGS URL: $LOGS_URL"
168 echo "DISTRIBUTION_TYPE: $DISTRIBUTION_TYPE"
169 #Check if this is a container or maven release: release-container-schema.yaml vs release-schema.yaml
170
171 #Logic to determine what we are releasing.
172 ##########################################
173 if [[ "$DISTRIBUTION_TYPE" == "maven" ]]; then
174   wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/release-schema.yaml
175   RELEASE_SCHEMA="release-schema.yaml"
176   if [[ "$RELEASE_FILE" == "True" ]]; then
177     verify_schema
178   fi
179   maven_release_file
180 elif [[ "$DISTRIBUTION_TYPE" == "container" ]]; then
181   # Container-release code is addressed in a new feature patchset.
182   container_release_file
183 else
184   echo "---> ERROR: distribution_type: $DISTRIBUTION_TYPE not supported"
185   echo "Must be maven or container"
186   exit 1
187 fi
188 ##########################################
189
190 echo "########### End Script release-job.sh ###################################"
191