blob: 0f451cc9665c78cb5be65dff007beccaeb19f25f [file] [log] [blame]
Zack Williams5aa37e12019-02-13 13:28:29 -07001#!/usr/bin/env bash
2
3# pypi-publish.sh - Publishes Python modules to PyPI
4#
5# Makes the following assumptions:
6# - PyPI credentials are populated in ~/.pypirc
7# - git repo is tagged with a SEMVER released version. If not, exit.
8# - If required, Environmental variables are set for:
9# PYPI_INDEX - name of PyPI index to use (see contents of ~/.pypirc)
10# PYPI_MODULE_DIRS - pipe-separated list of modules to be uploaded
11
12set -eu -o pipefail
13
14echo "Using twine version:"
15twine --version
16
17pypi_success=0
18
19# environmental vars
20WORKSPACE=${WORKSPACE:-.}
21PYPI_INDEX=${PYPI_INDEX:-testpypi}
22PYPI_MODULE_DIRS=${PYPI_MODULE_DIRS:-.}
23
24# check that we're on a semver released version
25GIT_VERSION=$(git tag -l --points-at HEAD)
26
27if [[ "$GIT_VERSION" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
28then
29 echo "git has a SemVer released version tag: '$GIT_VERSION', publishing to PyPI"
30else
31 echo "No SemVer released version tag found, exiting..."
32 exit 0
33fi
34
35# iterate over $PYPI_MODULE_DIRS
36# field separator is pipe character
37IFS=$'|'
38for pymod in $PYPI_MODULE_DIRS
39do
40 pymoddir="$WORKSPACE/$pymod"
41
42 if [ ! -f "$pymoddir/setup.py" ]
43 then
44 echo "Directory with python module not found at '$pymoddir'"
45 pypi_success=1
46 else
47 pushd "$pymoddir"
48
49 echo "Building python module in '$pymoddir'"
50 # Create source distribution
51 python setup.py sdist
52
53 # Upload to PyPI
54 echo "Uploading to PyPI"
55 twine upload -r "$PYPI_INDEX" dist/*
56
57 popd
58 fi
59done
60
61exit $pypi_success