-
Notifications
You must be signed in to change notification settings - Fork 3
215 lines (187 loc) · 7.19 KB
/
pypi-release.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# **what?**
# After releasing to GitHub, release to PyPI
#
# Inputs:
# version_number: The release version number (i.e. 1.0.0b1, 1.2.3rc2, 1.0.0)
# test_run : Test run (true - release to Test PyPI, false - release to PyPI)
#
# **why?**
# Automate the release process
#
# **when?**
# After successfully releasing to GitHub
#
# Assumptions
# 1. The name of the repository is the name of the package on PyPI
#
# Validation Checks
# 1. If the provided version is not uploaded to PyPI yet, release it.
# 2. Release to test or prod package index, depending on the value of test_run input.
# 3. Check PyPI at the end to validate that the version has been uploaded to package index.
name: PyPI release
on:
workflow_call:
inputs:
version_number:
description: "The tag for the release (ie. v1.0.0b1)"
required: true
type: string
test_run:
description: ""
required: true
type: boolean
# pass through secrets for both PyPi Test and Prod so they're always there
secrets:
PYPI_API_TOKEN:
description: PyPI API token
required: true
TEST_PYPI_API_TOKEN:
description: Test PyPI API token
required: true
permissions:
contents: read
env:
NOTIFICATION_PREFIX: "[PyPI Release]"
jobs:
log-inputs:
runs-on: ubuntu-latest
steps:
- name: "[DEBUG] Print Variables"
run: |
echo The release version number: ${{ inputs.version_number }}
echo Release to test PyPI: ${{ inputs.test_run }}
sanitize-package-name:
runs-on: ubuntu-latest
outputs:
name: ${{ steps.package-name.outputs.name }}
steps:
- name: "Sanitize Package Name"
id: package-name
run: |
repo_name=${{ github.event.repository.name }}
test_suffix="-release-test"
name=${repo_name%"$test_suffix"}
echo "name=$name" >> $GITHUB_OUTPUT
check-package-exists-pypi:
runs-on: ubuntu-latest
needs: [sanitize-package-name]
outputs:
exists: ${{ steps.version_existence.outputs.is_exists }}
steps:
- name: "Audit Version And Parse Into Parts"
id: semver
uses: dbt-labs/actions/parse-semver@main
with:
version: ${{ inputs.version_number }}
- name: "Fetch PyPI Info For ${{ steps.semver.outputs.version }} Package"
id: pypi_info
uses: dbt-labs/actions/py-package-info@main
with:
package: ${{ needs.sanitize-package-name.outputs.name }}
version: ${{ steps.semver.outputs.version }}
check-test-index: ${{ inputs.test_run }}
retries: 1
- name: "Set Version Existence For Subsequent Jobs"
# The above step will just use the latest version if the input version
# is not found. So to validate the version we want to release exists
# we need to compare the output version.
id: version_existence
run: |
is_exists=false
if [[ ${{ steps.pypi_info.outputs.version }} == ${{ steps.semver.outputs.version }} ]]
then
is_exists=true
fi
echo "is_exists=$is_exists" >> $GITHUB_OUTPUT
skip-pypi-release:
runs-on: ubuntu-latest
needs: [sanitize-package-name, check-package-exists-pypi]
if: needs.check-package-exists-pypi.outputs.exists == 'true'
steps:
- name: "[Notification] Package Version Already Live In Package Index. Skip Upload."
run: |
title="Package version already live in package index"
version=${{ inputs.version_number }}
package_name=${{ needs.sanitize-package-name.outputs.name }}
package_index="PyPI"
if [[ ${{ inputs.test_run }} == true ]]
then
package_index="Test PyPi"
fi
message="The version $version of package $package_name already lives in the $package_index. The upload to the package index will be skipped."
echo "::notice title=${{ env.NOTIFICATION_PREFIX }}: $title::$message"
test-pypi-release:
runs-on: ubuntu-latest
needs: [sanitize-package-name, check-package-exists-pypi]
if: >-
needs.check-package-exists-pypi.outputs.exists == 'false' &&
inputs.test_run == true
environment: PypiTest
steps:
- name: "Download Build Artifact - ${{ inputs.version_number }}"
uses: actions/download-artifact@v4
with:
name: ${{ inputs.version_number }}
path: .
- name: "Publish ${{ needs.sanitize-package-name.outputs.name }} v${{ inputs.version_number }} To Test PyPI"
uses: pypa/[email protected]
with:
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
repository_url: https://test.pypi.org/legacy/
prod-pypi-release:
runs-on: ubuntu-latest
needs: [sanitize-package-name, check-package-exists-pypi]
if: >-
needs.check-package-exists-pypi.outputs.exists == 'false' &&
inputs.test_run == false
environment: PypiProd
steps:
- name: "Download Build Artifact - ${{ inputs.version_number }}"
uses: actions/download-artifact@v4
with:
name: ${{ inputs.version_number }}
path: .
- name: "Publish ${{ needs.sanitize-package-name.outputs.name }} v${{ inputs.version_number }} To PyPI"
uses: pypa/[email protected]
with:
password: ${{ secrets.PYPI_API_TOKEN }}
validate-package-available-pypi:
runs-on: ubuntu-latest
needs: [sanitize-package-name, test-pypi-release, prod-pypi-release]
# always run this step because one of the needs are always skipped.
if: always() && contains(needs.*.result, 'success')
steps:
- name: "Audit Version And Parse Into Parts"
id: semver
uses: dbt-labs/actions/parse-semver@main
with:
version: ${{ inputs.version_number }}
- name: "Fetch PyPI Info For ${{ needs.sanitize-package-name.outputs.name }} Package"
id: pypi_info
uses: dbt-labs/actions/py-package-info@main
with:
package: ${{ needs.sanitize-package-name.outputs.name }}
version: ${{ steps.semver.outputs.version }}
check-test-index: ${{ inputs.test_run }}
retries: 8
- name: "Validate PyPI Info"
id: is-version-available
run: |
is_available=false
if [[ ${{ steps.pypi_info.outputs.version }} == ${{ steps.semver.outputs.version }} ]]
then
is_available=true
fi
echo "is_available=$is_available" >> $GITHUB_OUTPUT
- name: "Set Workflow Status"
run: |
title="Availability Validation"
if [[ ${{ steps.is-version-available.outputs.is_available }} == true ]]
then
message="The ${{ needs.sanitize-package-name.outputs.name }} v${{ steps.semver.outputs.version }} version available in PyPI."
echo "::notice title=${{ env.NOTIFICATION_PREFIX }}: $title::$message"
else
message="The info about ${{ needs.sanitize-package-name.outputs.name }} v${{ steps.semver.outputs.version }} version is not available in PyPI. Manual intervention required."
echo "::error title=${{ env.NOTIFICATION_PREFIX }}: $title::$message"
exit 1
fi