blob: 8b3f486a4021ad74d8240820e86106119c1e4550 [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'"
Eric Ball2ab0bb72026-02-05 18:50:16 -080059 # Activate venv (created by Makefile) and get latest packages
60 if [[ -f .venv/bin/activate ]]; then
61 source .venv/bin/activate
62 fi
63 pip install --upgrade pip
64 pip install --upgrade setuptools pkginfo wheel twine
65 python3 setup.py sdist
Zack Williams5aa37e12019-02-13 13:28:29 -070066 # Create source distribution
67 python setup.py sdist
68
69 # Upload to PyPI
70 echo "Uploading to PyPI"
71 twine upload -r "$PYPI_INDEX" dist/*
72
73 popd
74 fi
75done
76
77exit $pypi_success