Fix rtd merge job to handle new tag uploaded
[releng/global-jjb.git] / shell / pypi-tag-release.sh
1 #!/bin/bash
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 "---> pypi-tag-release.sh"
12
13 # Ensure we fail the job if any steps fail.
14 set -eu -o pipefail
15
16 # Functions.
17
18 set_variables(){
19     echo "INFO: Setting variables"
20     # Verify if using release file or parameters
21     if $USE_RELEASE_FILE; then
22         echo "INFO: Checking number of release yaml files"
23         release_files=$(git diff-tree --no-commit-id -r "$GIT_COMMIT" --name-only -- "releases/" ".releases/")
24         if (( $(echo "$release_files" | wc -w) != 1 )); then
25           echo "ERROR: RELEASE FILES: $release_files"
26           echo "ERROR: Committing multiple release files in the same commit OR rename/amend of existing files is not supported."
27           exit 1
28         else
29           release_file="$release_files"
30           echo "INFO: RELEASE FILE: $release_file"
31         fi
32     else
33         echo "INFO: This job is built with parameters, no release file"
34         release_file="None"
35     fi
36
37     if [[ -z ${DISTRIBUTION_TYPE:-} ]]; then
38         echo "INFO: reading DISTRIBUTION_TYPE from file $release_file"
39         DISTRIBUTION_TYPE="$(niet ".distribution_type" "$release_file")"
40     fi
41     if [[ -z ${VERSION:-} ]]; then
42         echo "INFO: reading VERSION from file $release_file"
43         VERSION="$(niet ".version" "$release_file")"
44     fi
45
46     # Display Release Information
47     printf "\t%-30s\n" RELEASE_ENVIRONMENT_INFO:
48     printf "\t%-30s %s\n" RELEASE_FILE: $release_file
49     printf "\t%-30s %s\n" JENKINS_HOSTNAME: $JENKINS_HOSTNAME
50     printf "\t%-30s %s\n" SILO: $SILO
51     printf "\t%-30s %s\n" PROJECT: $PROJECT
52     printf "\t%-30s %s\n" PROJECT-DASHED: ${PROJECT//\//-}
53     printf "\t%-30s %s\n" DISTRIBUTION_TYPE: $DISTRIBUTION_TYPE
54     printf "\t%-30s %s\n" VERSION: $VERSION
55 }
56
57 # needs to run in the repository root
58 verify_schema(){
59     echo "INFO: Fetching schema"
60     pypi_schema="release-pypi-schema.yaml"
61     wget https://raw.githubusercontent.com/lfit/releng-global-jjb/master/schema/${pypi_schema}
62     echo "INFO: Verifying $release_file against schema $pypi_schema"
63     lftools schema verify "$release_file" "$pypi_schema"
64     echo "INFO: $release_file passed schema verification"
65 }
66
67 verify_version(){
68     # Verify allowed patterns "v#.#.#" or "#.#.#" aka SemVer
69     echo "INFO: Verifying version string $VERSION"
70     allowed_version_regex="^((v?)([0-9]+)\.([0-9]+)\.([0-9]+))$"
71     if [[ $VERSION =~ $allowed_version_regex ]]; then
72         echo "INFO: The version $VERSION is a valid semantic version"
73     else
74         echo "ERROR: The version $VERSION is not a valid semantic version"
75         echo "ERROR: Allowed versions are \"v#.#.#\" or \"#.#.#\" aka SemVer"
76         echo "ERROR: See https://semver.org/ for more details on SemVer"
77         exit 1
78     fi
79 }
80
81 verify_dist(){
82     # Verify all file names in dist folder have the expected version string
83     dir="$WORKSPACE/$TOX_DIR/dist"
84     echo "INFO: Listing files in $dir"
85     ls $dir
86     echo "INFO: Checking files in $dir for $VERSION"
87     if unex_files=$(find $dir | grep -v $VERSION | egrep -v "^$dir$"); then
88         echo "ERROR: found unexpected files: $unex_files"
89         exit 1
90     else
91         echo "INFO: All file names have expected string ${VERSION}"
92     fi
93 }
94
95 # TODO: how to tag Github?
96 tag_gerrit(){
97     echo "INFO: Verifying tag $VERSION in repo"
98     # Import public signing key
99     gpg --import "$SIGNING_PUBKEY"
100     # Fail if tag exists
101     if git tag -v "$VERSION"; then
102         echo "ERROR: Repo already tagged"
103         exit 1
104     else
105         echo "INFO: Repo has not yet been tagged"
106     fi
107     echo "INFO: Tagging repo"
108     git tag -am "${PROJECT//\//-} $VERSION" "$VERSION"
109     echo "INFO: Signing tag"
110     sigul --batch -c "$SIGUL_CONFIG" sign-git-tag "$SIGUL_KEY" "$VERSION" < "$SIGUL_PASSWORD"
111     echo "INFO: Verifying tag"
112     # may fail due to missing public key
113     if ! git tag -v "$VERSION"; then
114         echo "WARN: failed to verify tag, continuing anyhow"
115     fi
116     # The verify job also calls this script
117     if [[ ! $JOB_NAME =~ "merge" ]] ; then
118         echo "INFO: job is not a merge, skipping push"
119     else
120         echo "INFO: configuring Gerrit remote"
121         gerrit_ssh=$(echo "$GERRIT_URL" | awk -F"/" '{print $3}')
122         git remote set-url origin "ssh://$RELEASE_USERNAME@$gerrit_ssh:29418/$PROJECT"
123         git config user.name "$RELEASE_USERNAME"
124         git config user.email "$RELEASE_EMAIL"
125         echo -e "Host $gerrit_ssh\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
126         chmod 600 ~/.ssh/config
127         if $DRY_RUN; then
128             echo "INFO: dry run, skipping push"
129         else
130             echo "INFO: pushing tag"
131             git push origin "$VERSION"
132         fi
133     fi
134 }
135
136 # Main
137 virtualenv -p python3 /tmp/pypi
138 PATH=/tmp/pypi/bin:$PATH
139 pip install lftools jsonschema niet
140 set_variables
141 if [[ $DISTRIBUTION_TYPE != "pypi" ]]; then
142     echo "ERROR: unexpected distribution type $DISTRIBUTION_TYPE"
143     exit 1
144 fi
145 if $USE_RELEASE_FILE; then
146     verify_schema
147 fi
148 verify_version
149 verify_dist
150 tag_gerrit
151 echo "---> pypi-tag-release.sh ends"