Zack Williams | 5aa37e1 | 2019-02-13 13:28:29 -0700 | [diff] [blame^] | 1 | #!/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 | |
| 12 | set -eu -o pipefail |
| 13 | |
| 14 | echo "Using twine version:" |
| 15 | twine --version |
| 16 | |
| 17 | pypi_success=0 |
| 18 | |
| 19 | # environmental vars |
| 20 | WORKSPACE=${WORKSPACE:-.} |
| 21 | PYPI_INDEX=${PYPI_INDEX:-testpypi} |
| 22 | PYPI_MODULE_DIRS=${PYPI_MODULE_DIRS:-.} |
| 23 | |
| 24 | # check that we're on a semver released version |
| 25 | GIT_VERSION=$(git tag -l --points-at HEAD) |
| 26 | |
| 27 | if [[ "$GIT_VERSION" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]] |
| 28 | then |
| 29 | echo "git has a SemVer released version tag: '$GIT_VERSION', publishing to PyPI" |
| 30 | else |
| 31 | echo "No SemVer released version tag found, exiting..." |
| 32 | exit 0 |
| 33 | fi |
| 34 | |
| 35 | # iterate over $PYPI_MODULE_DIRS |
| 36 | # field separator is pipe character |
| 37 | IFS=$'|' |
| 38 | for pymod in $PYPI_MODULE_DIRS |
| 39 | do |
| 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 |
| 59 | done |
| 60 | |
| 61 | exit $pypi_success |