Add step to verify stage repo is closed
[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 # sigul is only available on Centos
96 tag_gerrit(){
97     echo "INFO: Verifying tag $VERSION in repo"
98     # Import public signing key
99     gpg --import "$SIGNING_PUBKEY"
100     if git tag -v "$VERSION"; then
101         echo "INFO: Repo already tagged"
102         return 0
103     fi
104     echo "INFO: Tagging repo"
105     git tag -am "${PROJECT//\//-} $VERSION" "$VERSION"
106     echo "INFO: Signing tag"
107     sigul --batch -c "$SIGUL_CONFIG" sign-git-tag "$SIGUL_KEY" "$VERSION" < "$SIGUL_PASSWORD"
108     echo "INFO: Verifying tag"
109     # may fail due to missing public key
110     if ! git tag -v "$VERSION"; then
111         echo "WARN: failed to verify tag, continuing anyhow"
112     fi
113     # The verify job also calls this script
114     if [[ ! $JOB_NAME =~ "merge" ]] ; then
115         echo "INFO: job is not a merge, skipping push"
116     else
117         echo "INFO: configuring Gerrit remote"
118         gerrit_ssh=$(echo "$GERRIT_URL" | awk -F"/" '{print $3}')
119         git remote set-url origin "ssh://$RELEASE_USERNAME@$gerrit_ssh:29418/$PROJECT"
120         git config user.name "$RELEASE_USERNAME"
121         git config user.email "$RELEASE_EMAIL"
122         echo -e "Host $gerrit_ssh\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
123         chmod 600 ~/.ssh/config
124         if $DRY_RUN; then
125             echo "INFO: dry run, skipping push"
126         else
127             echo "INFO: pushing tag"
128             git push origin "$VERSION"
129         fi
130     fi
131 }
132
133 # Main
134 virtualenv -p python3 /tmp/pypi
135 PATH=/tmp/pypi/bin:$PATH
136 pip install lftools jsonschema niet
137 set_variables
138 if [[ $DISTRIBUTION_TYPE != "pypi" ]]; then
139     echo "ERROR: unexpected distribution type $DISTRIBUTION_TYPE"
140     exit 1
141 fi
142 if $USE_RELEASE_FILE; then
143     verify_schema
144 fi
145 verify_version
146 verify_dist
147 # TODO: write tag_github function
148 tag_gerrit
149 echo "---> pypi-tag-release.sh ends"