-
-
Notifications
You must be signed in to change notification settings - Fork 24
92 lines (78 loc) · 2.9 KB
/
publish-to-pypi.yaml
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
name: Publish To PyPi
on:
push:
branches:
- master
tags:
- '*.*.*'
jobs:
release:
runs-on: ubuntu-latest
environment: release
permissions:
id-token: write # Required for OIDC authentication
contents: write
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python 3.11.4
uses: actions/setup-python@v4
with:
python-version: "3.11.4"
- name: Install Poetry
run: |
curl -sSL https://install.python-poetry.org | python - -y
- name: Check Version
id: check-version
run: |
version=$(poetry version --short)
if [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "version=$version" >> $GITHUB_ENV
else
echo "Not a valid version bump. Skipping tag creation."
exit 0
fi
- name: Check if Tag Exists
id: tag_exists
run: |
if git rev-parse "refs/tags/${{ env.version }}" >/dev/null 2>&1; then
echo "Tag ${{ env.version }} already exists."
echo "tag_exists=true" >> $GITHUB_ENV
else
echo "tag_exists=false" >> $GITHUB_ENV
fi
- name: Create Tag
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' && env.tag_exists == 'false' }}
run: |
git config user.name "GitHub Actions"
git config user.email "[email protected]"
git tag ${{ env.version }}
git push origin ${{ env.version }}
- name: Checkout Tag
if: ${{ github.ref == 'refs/heads/master' && env.version != '' }}
run: |
git fetch --tags
git checkout "refs/tags/${{ env.version }}"
echo "Checked out tag ${{ env.version }}"
- name: Build project for distribution
run: poetry build
- name: Mint API token
id: mint-token
run: |
# retrieve the ambient OIDC token
resp=$(curl -H "Authorization: bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \
"$ACTIONS_ID_TOKEN_REQUEST_URL&audience=pypi")
oidc_token=$(jq -r '.value' <<< "${resp}")
# exchange the OIDC token for an API token
resp=$(curl -X POST https://pypi.org/_/oidc/mint-token -d "{\"token\": \"${oidc_token}\"}")
api_token=$(jq -r '.token' <<< "${resp}")
# mask the newly minted API token, so that we don't accidentally leak it
echo "::add-mask::${api_token}"
# see the next step in the workflow for an example of using this step output
echo "api-token=${api_token}" >> "${GITHUB_OUTPUT}"
- name: Publish to PyPI using minted token
env:
POETRY_PYPI_TOKEN_PYPI: ${{ steps.mint-token.outputs.api-token }}
run: |
poetry config pypi-token.pypi ${{ steps.mint-token.outputs.api-token }}
poetry publish