-
Notifications
You must be signed in to change notification settings - Fork 2
174 lines (150 loc) · 5.31 KB
/
publish-pypi.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#schema: https://github.com/softprops/github-actions-schemas/blob/master/workflow.json
name: "Publish to PyPI"
on:
release:
types:
- published
workflow_dispatch:
inputs:
tag_name:
description: 'Release tag'
required: true
repository:
description: "The PyPI repository to upload to."
required: true
options:
- pypi
- testpypi
skip_version_check:
description: "Skip check that verifies the pixelator version matches the release tag"
default: false
# Needed for PyPI OIDC permissions
permissions:
id-token: write
actions: write
contents: read
jobs:
pre_job:
name: Collect metadata
runs-on: ubuntu-latest
outputs:
tag: ${{ steps.get_version_tag.outputs.tag }}
pypi_repo_url: ${{ steps.get_version_tag.outputs.pypi_repo_url }}
steps:
- name: Get version tag & repo url
id: get_version_tag
uses: actions/github-script@v6
with:
script: |
const inputs = ${{ toJSON(inputs) }}
const gh_event = ${{ toJSON(github.event) }}
let tag = null
let pypi_repo_url = null
if (inputs.repository == "testpypi") {
pypi_repo_url = "https://test.pypi.org/legacy/"
} else {
pypi_repo_url = "https://upload.pypi.org/legacy/"
}
if (inputs.tag_name != null) {
tag = inputs.tag_name
} else if (gh_event.release != null) {
tag = gh_event.release.tag_name
} else {
core.setFailed('Cannot determine release tag!')
}
core.setOutput('tag', tag)
core.setOutput('pypi_repo_url', pypi_repo_url)
build:
name: Build
runs-on: ubuntu-latest
needs: [ pre_job ]
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python environment
uses: actions/setup-python@v4
id: setup-python
with:
python-version: '3.11'
#----------------------------------------------
# install & configure poetry
#----------------------------------------------
- name: Install Poetry
uses: snok/install-poetry@v1
with:
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true
- name: Install poetry plugins
run: |
poetry self add "poetry-dynamic-versioning[plugin]"
#----------------------------------------------
# load cached venv if cache exists
#----------------------------------------------
- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v3
with:
path: .venv
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}
#----------------------------------------------
# install dev dependencies if cache does not exist
#----------------------------------------------
- name: Install dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install --only dev
#----------------------------------------------
# Build package
#----------------------------------------------
- name: Build package
run: poetry build
#----------------------------------------------
# Verify build version
#----------------------------------------------
- name: Install package
run: |
pip install --no-input dist/*.whl
- name: Verify build version
if: ${{ inputs.skip_version_check != 'true' }}
run: |
python -c "import pixelator; assert 'v' + str(pixelator.__version__) == '${{ needs.pre_job.outputs.tag }}', 'Version mismatch: pixelator = v%s vs tag = %s' % (pixelator.__version__, '${{ needs.pre_job.outputs.tag }}')"
VERSION="$( pixelator --version | sed 's/pixelator, version //g' )"
TAG='${{ needs.pre_job.outputs.tag }}'
if [[ ! v$VERSION =~ $TAG ]]; then
echo "Version mismatch: pixelator = v$VERSION vs tag = $TAG";
exit 1;
fi
#----------------------------------------------
# archive python package build output
#----------------------------------------------
- name: Archive production artifacts
uses: actions/upload-artifact@v3
with:
name: dist-${{ needs.pre_job.outputs.tag }}
path: |
dist
pypi-publish:
name: Upload to PyPI
runs-on: ubuntu-latest
needs: [pre_job, build]
environment: release
steps:
- name: Set up Python environment
uses: actions/setup-python@v4
id: setup-python
with:
python-version: '3.11'
- name: Download build artifacts
uses: actions/download-artifact@v3
with:
name: dist-${{ needs.pre_job.outputs.tag }}
path: dist
- name: Publish package distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
repository-url: ${{ needs.pre_job.outputs.pypi_repo_url }}
verbose: true
print-hash: true
skip-existing: true