blob: b7f3526bc2fd708527486835e9da568ccfd0b08d [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.
Zack Williams96fecf02019-03-27 13:52:01 -07008# - If required, Environmental variables can be set for:
9# PYPI_INDEX - name of PyPI index to use (see contents of ~/.pypirc for reference)
Zack Williams5aa37e12019-02-13 13:28:29 -070010# PYPI_MODULE_DIRS - pipe-separated list of modules to be uploaded
Zack Williams96fecf02019-03-27 13:52:01 -070011# PYPI_PREP_COMMANDS - commands to run (in root directory) to prepare for sdist
Zack Williams5aa37e12019-02-13 13:28:29 -070012
13set -eu -o pipefail
14
15echo "Using twine version:"
16twine --version
17
18pypi_success=0
19
20# environmental vars
21WORKSPACE=${WORKSPACE:-.}
Zack Williams96fecf02019-03-27 13:52:01 -070022PYPI_PREP_COMMANDS=${PYPI_PREP_COMMANDS:-}
Zack Williams5aa37e12019-02-13 13:28:29 -070023PYPI_INDEX=${PYPI_INDEX:-testpypi}
24PYPI_MODULE_DIRS=${PYPI_MODULE_DIRS:-.}
25
26# check that we're on a semver released version
27GIT_VERSION=$(git tag -l --points-at HEAD)
28
29if [[ "$GIT_VERSION" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
30then
31 echo "git has a SemVer released version tag: '$GIT_VERSION', publishing to PyPI"
32else
33 echo "No SemVer released version tag found, exiting..."
34 exit 0
35fi
36
Zack Williams96fecf02019-03-27 13:52:01 -070037# Run commands if PYPI_PREP_COMMANDS if not null
38if [[ -n "$PYPI_PREP_COMMANDS" ]]
39then
40 $PYPI_PREP_COMMANDS
41fi
42
Zack Williams5aa37e12019-02-13 13:28:29 -070043# iterate over $PYPI_MODULE_DIRS
44# field separator is pipe character
45IFS=$'|'
46for pymod in $PYPI_MODULE_DIRS
47do
48 pymoddir="$WORKSPACE/$pymod"
49
50 if [ ! -f "$pymoddir/setup.py" ]
51 then
52 echo "Directory with python module not found at '$pymoddir'"
53 pypi_success=1
54 else
55 pushd "$pymoddir"
56
57 echo "Building python module in '$pymoddir'"
58 # Create source distribution
59 python setup.py sdist
60
61 # Upload to PyPI
62 echo "Uploading to PyPI"
63 twine upload -r "$PYPI_INDEX" dist/*
64
65 popd
66 fi
67done
68
69exit $pypi_success