|
1 | 1 | #!/bin/sh |
2 | 2 |
|
3 | | -# Get tag from environment |
| 3 | +# Get release tag from environment and checkout branch. |
4 | 4 | tag=$(basename $GITHUB_REF) |
| 5 | +git checkout tags/$tag |
5 | 6 |
|
6 | | -# Get action that triggered release event |
| 7 | +# Get package version. |
| 8 | +version=$(python setup.py --version) |
| 9 | + |
| 10 | +# Check if release tag matches the package version. |
| 11 | +pip install --quiet "packaging>=17.0" |
| 12 | + |
| 13 | +match=$(python -c " |
| 14 | +from packaging.version import parse |
| 15 | +
|
| 16 | +match = parse('$tag') == parse('$version') |
| 17 | +print(match) |
| 18 | +") |
| 19 | + |
| 20 | +if [ $match != "True" ]; then |
| 21 | + echo "Release $tag does not match package $version" |
| 22 | + exit 1 |
| 23 | +fi |
| 24 | + |
| 25 | +# Get action that triggered event. |
7 | 26 | action=$(python -c " |
8 | 27 | import json |
| 28 | +
|
9 | 29 | with open('$GITHUB_EVENT_PATH', 'r') as file: |
10 | 30 | event = json.load(file) |
11 | 31 | action = event.get('action') |
| 32 | +
|
12 | 33 | print(action) |
13 | 34 | ") |
14 | 35 |
|
15 | | -echo "Release $tag was $action on GitHub ..." |
| 36 | +# Infer which repository to use. |
| 37 | +repository=$(python -c " |
| 38 | +from packaging.version import parse, Version |
16 | 39 |
|
17 | | -upload_to_pypi () { |
18 | | - # Checkout specified tag |
19 | | - git checkout tags/$tag |
| 40 | +version = parse('$tag') |
| 41 | +if isinstance(version, Version): |
| 42 | + repository = 'Test PyPI' if version.is_devrelease else 'PyPI' |
| 43 | + print(repository) |
| 44 | +") |
20 | 45 |
|
21 | | - # Remove build artifacts |
| 46 | +build_package() { |
| 47 | + # Remove build artifacts. |
22 | 48 | rm -rf .eggs/ rm -rf dist/ rm -rf build/ |
23 | 49 |
|
24 | | - # Create distributions |
25 | | - python setup.py -q sdist bdist_wheel |
| 50 | + # Create distributions. |
| 51 | + python setup.py --quiet sdist bdist_wheel |
| 52 | +} |
| 53 | + |
| 54 | +upload_package() { |
| 55 | + # Build the package to upload. |
| 56 | + build_package |
| 57 | + |
| 58 | + # Create and activate the virtualenv to download twine. |
| 59 | + python -m venv venv; . venv/bin/activate |
26 | 60 |
|
27 | | - # Create virtualenv to download twine |
28 | | - python -m venv venv |
29 | | - . venv/bin/activate |
30 | | - |
31 | | - # Upgrade pip |
32 | | - python -m pip install --upgrade pip -q |
| 61 | + # Upgrade pip. |
| 62 | + python -m pip install --quiet --upgrade pip |
33 | 63 |
|
34 | | - # Install twine, module used to upload to pypi |
35 | | - python -m pip install twine -q |
| 64 | + # Install twine which is used to upload the package. |
| 65 | + python -m pip --quiet install twine |
36 | 66 |
|
37 | | - # Upload to pypi or testpypi, overwrite if files already exist. |
| 67 | + echo |
| 68 | + echo "---------- Uploading to $repository --------------------" |
| 69 | + echo |
| 70 | + |
| 71 | + # Upload the package to PyPI or Test PyPI. Overwrite if files already exist. |
38 | 72 | python -m twine upload dist/* --skip-existing --verbose \ |
39 | | - --username $PYPI_USERNAME --password $PYPI_PASSWORD \ |
40 | | - --repository-url $TWINE_REPOSITORY_URL |
| 73 | + --username $1 --password $2 --repository-url $3 |
41 | 74 | } |
42 | 75 |
|
43 | | -# If release was published on GitHub then upload to PyPI |
44 | | -if [ $action = "published" ]; then upload_to_pypi; fi |
| 76 | +release_package() { |
| 77 | + # If the inferred repository is PyPI, then release to PyPI. |
| 78 | + if [ "$repository" = "PyPI" ]; then |
| 79 | + TWINE_REPOSITORY_URL="https://upload.pypi.org/legacy/" |
| 80 | + upload_package $PYPI_USERNAME $PYPI_PASSWORD $TWINE_REPOSITORY_URL |
| 81 | + |
| 82 | + # Else if the inferred repository is Test PyPI, then release to Test PyPI. |
| 83 | + elif [ "$repository" = "Test PyPI" ]; then |
| 84 | + TWINE_REPOSITORY_URL="https://test.pypi.org/legacy/" |
| 85 | + upload_package $TEST_PYPI_USERNAME $TEST_PYPI_PASSWORD $TWINE_REPOSITORY_URL |
| 86 | + |
| 87 | + # Else, raise an error and exit. |
| 88 | + else |
| 89 | + echo "Unable to make inference on release $tag" |
| 90 | + exit 1 |
| 91 | + fi |
| 92 | +} |
| 93 | + |
| 94 | +echo |
| 95 | +echo "==================================================" |
| 96 | +echo "Release $tag was $action on GitHub" |
| 97 | +echo "==================================================" |
| 98 | +echo |
| 99 | + |
| 100 | +# If release was published on GitHub then release package. |
| 101 | +if [ $action = "published" ]; then release_package; fi |
0 commit comments