Skip to content

Commit f2d4b3e

Browse files
committed
Use twine's built-in Trusted Publishing support
1 parent db8f07d commit f2d4b3e

File tree

5 files changed

+18
-297
lines changed

5 files changed

+18
-297
lines changed

Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ COPY LICENSE.md .
2828
COPY twine-upload.sh .
2929
COPY print-hash.py .
3030
COPY print-pkg-names.py .
31-
COPY oidc-exchange.py .
3231
COPY attestations.py .
3332

3433
RUN chmod +x twine-upload.sh

oidc-exchange.py

Lines changed: 0 additions & 277 deletions
This file was deleted.

requirements/runtime.in

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
-c runtime-constraints.in # limits known broken versions
22

3-
# NOTE: v6.1 is needed to support metadata v2.4 including PEP 639
3+
# NOTE: v6.1 is needed to support metadata v2.4 including PEP 639,
4+
# and also uploading via Trusted Publishing
45
twine >= 6.1
56

6-
# NOTE: Used to detect an ambient OIDC credential for OIDC publishing,
7-
# NOTE: as well as PEP 740 attestations.
8-
id ~= 1.0
9-
10-
# NOTE: This is pulled in transitively through `twine`, but we also declare
11-
# NOTE: it explicitly here because `oidc-exchange.py` uses it.
12-
# Ref: https://github.com/di/id
13-
requests
14-
157
# NOTE: Used to generate attestations.
168
pypi-attestations ~= 0.0.15
179
sigstore ~= 3.5.1

requirements/runtime.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ hyperframe==6.1.0
3636
# via h2
3737
id==1.5.0
3838
# via
39-
# -r runtime.in
4039
# sigstore
4140
# twine
4241
idna==3.10
@@ -104,7 +103,6 @@ readme-renderer==44.0
104103
# via twine
105104
requests==2.32.3
106105
# via
107-
# -r runtime.in
108106
# id
109107
# pypi-attestations
110108
# requests-toolbelt

twine-upload.sh

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,10 @@ fi
118118

119119
if "${TRUSTED_PUBLISHING}" ; then
120120
# No password supplied by the user implies that we're in the OIDC flow;
121-
# retrieve the OIDC credential and exchange it for a PyPI API token.
121+
# call twine with an empty password, since twine will detect it's running
122+
# inside CI/CD, retrieve the OIDC token and exchange it for a PyPI API token.
122123
echo "::debug::Authenticating to ${INPUT_REPOSITORY_URL} via Trusted Publishing"
123-
INPUT_PASSWORD="$(python /app/oidc-exchange.py)"
124+
INPUT_PASSWORD=""
124125
elif [[ "${INPUT_USER}" == '__token__' ]]; then
125126
echo \
126127
'::debug::Using a user-provided API token for authentication' \
@@ -145,7 +146,8 @@ fi
145146

146147
if [[
147148
"$INPUT_USER" == "__token__" &&
148-
! "$INPUT_PASSWORD" =~ ^pypi-
149+
! "$INPUT_PASSWORD" =~ ^pypi- &&
150+
"${TRUSTED_PUBLISHING}" == false
149151
]]
150152
then
151153
if [[ -z "$INPUT_PASSWORD" ]]; then
@@ -208,7 +210,14 @@ if [[ ${INPUT_PRINT_HASH,,} != "false" || ${INPUT_VERBOSE,,} != "false" ]] ; the
208210
python /app/print-hash.py ${INPUT_PACKAGES_DIR%%/}
209211
fi
210212

211-
TWINE_USERNAME="$INPUT_USER" \
212-
TWINE_PASSWORD="$INPUT_PASSWORD" \
213-
TWINE_REPOSITORY_URL="$INPUT_REPOSITORY_URL" \
214-
exec twine upload ${TWINE_EXTRA_ARGS} ${INPUT_PACKAGES_DIR%%/}/*
213+
# Using Trusted Publishing with twine requires not setting the password env var.
214+
if "${TRUSTED_PUBLISHING}" ; then
215+
TWINE_USERNAME="$INPUT_USER" \
216+
TWINE_REPOSITORY_URL="$INPUT_REPOSITORY_URL" \
217+
exec twine upload ${TWINE_EXTRA_ARGS} ${INPUT_PACKAGES_DIR%%/}/*
218+
else
219+
TWINE_USERNAME="$INPUT_USER" \
220+
TWINE_PASSWORD="$INPUT_PASSWORD" \
221+
TWINE_REPOSITORY_URL="$INPUT_REPOSITORY_URL" \
222+
exec twine upload ${TWINE_EXTRA_ARGS} ${INPUT_PACKAGES_DIR%%/}/*
223+
fi

0 commit comments

Comments
 (0)