-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupload.sh
117 lines (89 loc) · 3.05 KB
/
upload.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/bin/sh
# Get release tag from environment and checkout branch.
tag=$(basename $GITHUB_REF)
# sometimes $GITHUB_REF is empty
# See: https://github.com/actions/runner/issues/2788
if [ $tag == ""]; then
tag=$TAG_NAME
fi
git checkout tags/$tag
# Upgrade pip.
python -m pip install --quiet --upgrade pip
# Upgrade build.
python -m pip install --quiet --upgrade build
# Upgrade setuptools.
python -m pip install --quiet --upgrade setuptools
# Check if release tag matches the package version.
pip install --quiet "packaging>=17.0"
# Get package version. Must run after installing build.
version=$(python -c 'import setuptools; setuptools.setup()' --version)
match=$(python -c "
from packaging.version import parse
match = parse('$tag') == parse('$version')
print(match)
")
if [ $match != "True" ]; then
echo "Release $tag does not match package $version"
exit 1
fi
# Get action that triggered event.
action=$(python -c "
import json
with open('$GITHUB_EVENT_PATH', 'r') as file:
event = json.load(file)
action = event.get('action')
print(action)
")
# Infer which repository to use.
repository=$(python -c "
from packaging.version import parse, Version
version = parse('$tag')
if isinstance(version, Version):
repository = 'Test PyPI' if version.is_devrelease else 'PyPI'
print(repository)
")
build_package() {
# Remove build artifacts.
rm -rf .eggs/ rm -rf dist/ rm -rf build/
# Create distributions.
python -m build
}
upload_package() {
# Build the package to upload.
build_package
# Create and activate the virtualenv to download twine.
# Done to keep twine separated in a clean upload environment.
python -m venv venv; . venv/bin/activate
# Upgrade pip.
python -m pip install --quiet --upgrade pip
# Install twine which is used to upload the package.
python -m pip --quiet install twine
echo
echo "---------- Uploading to $repository --------------------"
echo
# Upload the package to PyPI or Test PyPI. Overwrite if files already exist.
python -m twine upload dist/* --skip-existing --verbose \
--username $1 --password $2 --repository-url $3
}
release_package() {
# If the inferred repository is PyPI, then release to PyPI.
if [ "$repository" = "PyPI" ]; then
TWINE_REPOSITORY_URL="https://upload.pypi.org/legacy/"
upload_package $PYPI_USERNAME $PYPI_PASSWORD $TWINE_REPOSITORY_URL
# Else if the inferred repository is Test PyPI, then release to Test PyPI.
elif [ "$repository" = "Test PyPI" ]; then
TWINE_REPOSITORY_URL="https://test.pypi.org/legacy/"
upload_package $TEST_PYPI_USERNAME $TEST_PYPI_PASSWORD $TWINE_REPOSITORY_URL
# Else, raise an error and exit.
else
echo "Unable to make inference on release $tag"
exit 1
fi
}
echo
echo "=================================================="
echo "Release $tag was $action on GitHub"
echo "=================================================="
echo
# If release was published on GitHub then release package.
if [ $action = "published" ]; then release_package; fi