Upgrade lftools to v0.26.0
[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
13 set -eu -o pipefail
14
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
25 pip install --user lftools[nexus] jsonschema niet
26
27 echo "########### Start Script release-job.sh ###################################"
28
29 LOGS_SERVER="${LOGS_SERVER:-None}"
30 MAVEN_CENTRAL_URL="${MAVEN_CENTRAL_URL:-None}"
31
32 if [ "${LOGS_SERVER}" == 'None' ]; then
33     echo "FAILED: log server not found"
34     exit 1
35 fi
36
37 NEXUSPROXY="${NEXUSPROXY:-None}"
38 NEXUS_URL="${NEXUSPROXY:-$NEXUS_URL}"
39
40 # Fetch the release-schema.yaml
41 wget -q https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/release-schema.yaml
42
43 release_files=$(git diff-tree --no-commit-id -r "$GERRIT_PATCHSET_REVISION" --name-only -- "releases/")
44 echo "RELEASE FILES ARE AS FOLLOWS: $release_files"
45
46 if (( $(grep -c . <<<"$release_files") > 1 )); then
47   echo "multiple release files in the same commit do not make sense"
48   exit 1
49 else
50   release_file="$release_files"
51   echo "RELEASE FILE IS AS FOLLOWS: $release_file"
52 fi
53
54 echo "--> Verifying $release_file schema."
55 lftools schema verify "$release_file" release-schema.yaml
56
57 VERSION="$(niet ".version" "$release_file")"
58 LOG_DIR="$(niet ".log_dir" "$release_file")"
59 NEXUS_PATH="${SILO}/${JENKINS_HOSTNAME}/"
60 LOGS_URL="${LOGS_SERVER}/${NEXUS_PATH}${LOG_DIR}"
61 PATCH_DIR="$(mktemp -d)"
62
63 LOGS_URL=${LOGS_URL%/}  # strip any trailing '/'
64 echo "wget -P $PATCH_DIR ${LOGS_URL}/staging-repo.txt.gz"
65 wget -P "$PATCH_DIR" "${LOGS_URL}/"staging-repo.txt.gz
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   # extract the staging repo from URL
72   STAGING_REPO=${staging_url#*repositories/}
73   echo "Merge will run"
74   echo "lftools nexus release --server https://$NEXUS_URL $STAGING_REPO"
75   if [[ "$JOB_NAME" =~ "merge" ]]; then
76     echo "Promoting $STAGING_REPO on $NEXUS_URL."
77     lftools nexus release --server https://"$NEXUS_URL" "$STAGING_REPO"
78   fi
79 done
80 }
81
82
83 #INFO
84 echo "INFO:"
85 echo "RELEASE_FILE: $release_file"
86 echo "LOGS_SERVER: $LOGS_SERVER"
87 echo "NEXUS_URL: $NEXUS_URL"
88 echo "NEXUS_PATH: $NEXUS_PATH"
89 echo "NEXUSPROXY: $NEXUSPROXY"
90 echo "JENKINS_HOSTNAME: $JENKINS_HOSTNAME"
91 echo "SILO: $SILO"
92 echo "PROJECT: $PROJECT"
93 echo "PROJECT-DASHED: ${PROJECT//\//-}"
94
95 echo "VERSION: $VERSION"
96 echo "LOG DIR: $LOG_DIR"
97
98 pushd "$PATCH_DIR"
99   echo "wget ${LOGS_URL}/patches/{${PROJECT//\//-}.bundle,taglist.log.gz}"
100   wget "${LOGS_URL}"/patches/{"${PROJECT//\//-}".bundle,taglist.log.gz}
101   gunzip taglist.log.gz
102   cat "$PATCH_DIR"/taglist.log
103 popd
104
105 # Verify allowed versions
106 # Allowed versions are "v#.#.#" or "#.#.#" aka SemVer
107 allowed_version_regex="^((v?)([0-9]+)\.([0-9]+)\.([0-9]+))$"
108 if [[ ! $VERSION =~ $allowed_version_regex ]]; then
109   echo "The version $VERSION is not a semantic valid version"
110   echo "Allowed versions are \"v#.#.#\" or \"#.#.#\" aka SemVer"
111   echo "See https://semver.org/ for more details on SemVer"
112   exit 1
113 fi
114
115 if git tag -v "$VERSION"; then
116   echo "Repo already tagged $VERSION"
117   echo "This job has already run exit 0"
118   exit 0
119 fi
120
121 git checkout "$(awk '{print $NF}' "$PATCH_DIR/taglist.log")"
122 git fetch "$PATCH_DIR/${PROJECT//\//-}.bundle"
123 git merge --ff-only FETCH_HEAD
124 git tag -am "${PROJECT//\//-} $VERSION" "$VERSION"
125 sigul --batch -c "$SIGUL_CONFIG" sign-git-tag "$SIGUL_KEY" "$VERSION" < "$SIGUL_PASSWORD"
126
127 echo "Showing latest signature for $PROJECT:"
128 gpg --import "$SIGNING_PUBKEY"
129 echo "git tag -v $VERSION"
130 git tag -v "$VERSION"
131
132 ########## Merge Part ##############
133 if [[ "$JOB_NAME" =~ "merge" ]]; then
134   echo "Running merge"
135   gerrit_ssh=$(echo "$GERRIT_URL" | awk -F"/" '{print $3}')
136   git remote set-url origin ssh://"$RELEASE_USERNAME"@"$gerrit_ssh":29418/"$PROJECT"
137   git config user.name "$RELEASE_USERNAME"
138   git config user.email "$RELEASE_EMAIL"
139   echo -e "Host $gerrit_ssh\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
140   chmod 600 ~/.ssh/config
141   git push origin "$VERSION"
142
143
144 fi
145
146 # This function: if merge push to nexus. If verify output the proposed push command.
147 nexus_release
148 #
149 #echo "########### End Script release-job.sh ###################################"