Skip to content

Commit 802e944

Browse files
Refactor GitHub Actions workflow to mint PyPI API token dynamically and update publishing step to trusted publisher
1 parent 9166b2c commit 802e944

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

.github/workflows/publish-pypi.yml

+20-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ jobs:
1818
details:
1919
name: Extract Tag Details and Pre-checks
2020
runs-on: ubuntu-latest
21-
environment: release-with-approval
2221
permissions:
2322
contents: read
2423
outputs:
@@ -95,8 +94,6 @@ jobs:
9594
needs: [setup_and_build]
9695
runs-on: ubuntu-latest
9796
permissions:
98-
contents: read
99-
actions: write
10097
id-token: write
10198
steps:
10299
- name: Download Artifacts
@@ -105,7 +102,25 @@ jobs:
105102
name: dist
106103
path: dist/
107104

105+
- name: Mint API Token
106+
id: mint-token
107+
run: |
108+
# Retrieve the OIDC token
109+
resp=$(curl -H "Authorization: bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \
110+
"$ACTIONS_ID_TOKEN_REQUEST_URL&audience=pypi")
111+
oidc_token=$(jq -r '.value' <<< "${resp}")
112+
113+
# Exchange the OIDC token for a PyPI API token
114+
resp=$(curl -X POST https://pypi.org/_/oidc/mint-token -d "{\"token\": \"${oidc_token}\"}")
115+
api_token=$(jq -r '.token' <<< "${resp}")
116+
117+
# Mask the API token to prevent leaks
118+
echo "::add-mask::${api_token}"
119+
120+
# Store API token as an output
121+
echo "api-token=${api_token}" >> "${GITHUB_OUTPUT}"
122+
108123
- name: Publish Distribution
109-
uses: pypa/gh-action-pypi-publish@67339c736fd9354cd4f8cb0b744f2b82a74b5c70
124+
uses: pypa/gh-action-pypi-publish@release/v1
110125
with:
111-
password: ${{ secrets.PYPI_TOKEN }}
126+
password: ${{ steps.mint-token.outputs.api-token }}

0 commit comments

Comments
 (0)