-
-
Notifications
You must be signed in to change notification settings - Fork 0
89 lines (83 loc) · 3.07 KB
/
pypiRelease.yml
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
# See https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries
name: Make PyPI Release
on:
workflow_run:
workflows: ["Python Tests"]
types: [completed]
workflow_dispatch:
release:
types: [published, released]
permissions:
contents: read
jobs:
check-version:
if: ${{ github.event.workflow_run.conclusion == 'success' || github.event_name == 'workflow_dispatch' }}
runs-on: ubuntu-latest
outputs:
should_publish: ${{ steps.check.outputs.should_publish }}
version: ${{ steps.version.outputs.version }}
package_name: ${{ steps.check.outputs.package_name }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.x"
- name: Get package version
id: version
run: |
pip install toml
version=$(python -c "import toml; print(toml.load('pyproject.toml')['project']['version'])")
echo "version=$version" >> "$GITHUB_OUTPUT"
- name: Check PyPI version
id: check
run: |
set -e
pip install requests
version="${{ steps.version.outputs.version }}"
package_name=$(python -c "import toml; print(toml.load('pyproject.toml')['project']['name'])")
echo "package_name=$package_name" >> "$GITHUB_OUTPUT"
echo "Checking version $version for package $package_name"
if python -c "import requests, sys; package_name=sys.argv[1]; version=sys.argv[2]; exit(0 if version in requests.get(f'https://pypi.org/pypi/{package_name}/json').json()['releases'] else 1)" "$package_name" "$version"; then
echo "Version already on PyPI"
echo "should_publish=false" >> "$GITHUB_OUTPUT"
else
echo "New version - should publish"
echo "should_publish=true" >> "$GITHUB_OUTPUT"
fi
release-build:
needs: check-version
if: needs.check-version.outputs.should_publish == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.x"
- name: Build release distributions
run: |
python -m pip install build
python -m build
- name: Upload distributions
uses: actions/upload-artifact@v4
with:
name: release-dists
path: dist/
pypi-publish:
needs: [check-version, release-build]
if: needs.check-version.outputs.should_publish == 'true'
runs-on: ubuntu-latest
environment:
name: pypi
url: https://pypi.org/project/${{ needs.check-version.outputs.package_name }}/${{ needs.check-version.outputs.version }}
permissions:
id-token: write
steps:
- name: Retrieve release distributions
uses: actions/download-artifact@v4
with:
name: release-dists
path: dist/
- name: Publish to PyPI
uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc #v1.12.2
with:
packages-dir: dist/