blob: 3118df715418df403eb773eb3b91b9fe7690dd43 [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
Zack Williams6e070f52019-10-04 11:08:59 -070029# match bare versions or v-prefixed golang style version
30if [[ "$GIT_VERSION" =~ ^v?([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]
Zack Williams5aa37e12019-02-13 13:28:29 -070031then
32 echo "git has a SemVer released version tag: '$GIT_VERSION', publishing to PyPI"
33else
34 echo "No SemVer released version tag found, exiting..."
35 exit 0
36fi
37
Zack Williams96fecf02019-03-27 13:52:01 -070038# Run commands if PYPI_PREP_COMMANDS if not null
39if [[ -n "$PYPI_PREP_COMMANDS" ]]
40then
41 $PYPI_PREP_COMMANDS
42fi
43
Zack Williams5aa37e12019-02-13 13:28:29 -070044# iterate over $PYPI_MODULE_DIRS
45# field separator is pipe character
46IFS=$'|'
47for pymod in $PYPI_MODULE_DIRS
48do
49 pymoddir="$WORKSPACE/$pymod"
50
51 if [ ! -f "$pymoddir/setup.py" ]
52 then
53 echo "Directory with python module not found at '$pymoddir'"
54 pypi_success=1
55 else
56 pushd "$pymoddir"
57
58 echo "Building python module in '$pymoddir'"
59 # Create source distribution
60 python setup.py sdist
61
62 # Upload to PyPI
63 echo "Uploading to PyPI"
64 twine upload -r "$PYPI_INDEX" dist/*
65
66 popd
67 fi
68done
69
70exit $pypi_success