2022-12-07 07:02:01 +08:00
|
|
|
#! /bin/bash
|
|
|
|
|
2022-12-07 07:07:43 +08:00
|
|
|
if [[ -n "${DEBUG}" ]]
|
|
|
|
then
|
|
|
|
set -x
|
|
|
|
fi
|
|
|
|
|
2019-08-21 04:48:52 +08:00
|
|
|
set -Eeuo pipefail
|
|
|
|
|
2019-09-12 20:38:56 +08:00
|
|
|
|
2022-12-07 04:40:38 +08:00
|
|
|
# NOTE: These variables are needed to combat GitHub passing broken env vars
|
|
|
|
# NOTE: from the runner VM host runtime.
|
|
|
|
# Ref: https://github.com/pypa/gh-action-pypi-publish/issues/112
|
2022-12-07 09:41:32 +08:00
|
|
|
export HOME="/root" # So that `python -m site` doesn't get confused
|
2022-12-07 07:07:20 +08:00
|
|
|
export PATH="/usr/bin:${PATH}" # To find `id`
|
2022-12-07 06:55:06 +08:00
|
|
|
. /etc/profile # Makes python and other executables findable
|
2022-12-07 04:40:38 +08:00
|
|
|
export PATH="$(python -m site --user-base)/bin:${PATH}"
|
|
|
|
export PYTHONPATH="$(python -m site --user-site):${PYTHONPATH}"
|
|
|
|
|
|
|
|
|
2023-03-11 08:18:41 +08:00
|
|
|
function get-normalized-input() {
|
|
|
|
local var_name=${1}
|
|
|
|
python -c \
|
|
|
|
'
|
|
|
|
from os import getenv
|
|
|
|
from sys import argv
|
|
|
|
envvar_name = f"INPUT_{argv[1].upper()}"
|
2023-03-11 10:06:39 +08:00
|
|
|
print(
|
|
|
|
getenv(envvar_name) or getenv(envvar_name.replace("-", "_")) or "",
|
|
|
|
end="",
|
|
|
|
)
|
2023-03-11 08:18:41 +08:00
|
|
|
' \
|
|
|
|
"${var_name}"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
INPUT_REPOSITORY_URL="$(get-normalized-input 'repository-url')"
|
|
|
|
INPUT_PACKAGES_DIR="$(get-normalized-input 'packages-dir')"
|
|
|
|
INPUT_VERIFY_METADATA="$(get-normalized-input 'verify-metadata')"
|
|
|
|
INPUT_SKIP_EXISTING="$(get-normalized-input 'skip-existing')"
|
|
|
|
INPUT_PRINT_HASH="$(get-normalized-input 'print-hash')"
|
2024-09-01 08:50:29 +08:00
|
|
|
INPUT_ATTESTATIONS="$(get-normalized-input 'attestations')"
|
2023-03-11 08:18:41 +08:00
|
|
|
|
2024-09-05 23:25:58 +08:00
|
|
|
REPOSITORY_NAME="$(echo ${GITHUB_REPOSITORY} | cut -d'/' -f2)"
|
|
|
|
WORKFLOW_FILENAME="$(echo ${GITHUB_WORKFLOW_REF} | cut -d'/' -f5- | cut -d'@' -f1)"
|
|
|
|
PACKAGE_NAMES=()
|
|
|
|
while IFS='' read -r line; do PACKAGE_NAMES+=("$line"); done < <(python /app/print-pkg-names.py "${INPUT_PACKAGES_DIR%%/}")
|
|
|
|
|
2024-05-16 23:07:28 +08:00
|
|
|
PASSWORD_DEPRECATION_NUDGE="::error title=Password-based uploads disabled::\
|
|
|
|
As of 2024, PyPI requires all users to enable Two-Factor \
|
|
|
|
Authentication. This consequently requires all users to switch \
|
2023-11-23 06:28:02 +08:00
|
|
|
to either Trusted Publishers (preferred) or API tokens for package \
|
2023-11-06 12:53:52 +08:00
|
|
|
uploads. Read more: \
|
|
|
|
https://blog.pypi.org/posts/2023-05-25-securing-pypi-with-2fa/"
|
|
|
|
|
2023-07-10 23:44:56 +08:00
|
|
|
TRUSTED_PUBLISHING_NUDGE="::warning title=Upgrade to Trusted Publishing::\
|
|
|
|
Trusted Publishers allows publishing packages to PyPI from automated \
|
|
|
|
environments like GitHub Actions without needing to use username/password \
|
|
|
|
combinations or API tokens to authenticate with PyPI. Read more: \
|
|
|
|
https://docs.pypi.org/trusted-publishers"
|
|
|
|
|
2024-09-01 08:50:29 +08:00
|
|
|
ATTESTATIONS_WITHOUT_TP_WARNING="::warning title=attestations input ignored::\
|
|
|
|
The workflow was run with the 'attestations: true' input, but an explicit \
|
|
|
|
password was also set, disabling Trusted Publishing. As a result, the \
|
|
|
|
attestations input is ignored."
|
|
|
|
|
|
|
|
ATTESTATIONS_WRONG_INDEX_WARNING="::warning title=attestations input ignored::\
|
|
|
|
The workflow was run with 'attestations: true' input, but the specified \
|
|
|
|
repository URL does not support PEP 740 attestations. As a result, the \
|
|
|
|
attestations input is ignored."
|
|
|
|
|
2024-09-05 23:25:58 +08:00
|
|
|
MAGIC_LINK_MESSAGE="::warning title=Create a Trusted Publisher::\
|
|
|
|
A new Trusted Publisher for the currently running publishing workflow can be created \
|
|
|
|
by accessing the following link(s) while logged-in as an owner of the package(s):"
|
|
|
|
|
|
|
|
if [[ ! "${INPUT_REPOSITORY_URL}" =~ pypi\.org || ${#PACKAGE_NAMES[@]} -eq 0 ]] ; then
|
|
|
|
TRUSTED_PUBLISHING_MAGIC_LINK_NUDGE=""
|
|
|
|
else
|
|
|
|
if [[ "${INPUT_REPOSITORY_URL}" =~ test\.pypi\.org ]] ; then
|
|
|
|
INDEX_URL="https://test.pypi.org"
|
|
|
|
else
|
|
|
|
INDEX_URL="https://pypi.org"
|
|
|
|
fi
|
|
|
|
ALL_LINKS=""
|
|
|
|
for PACKAGE_NAME in "${PACKAGE_NAMES[@]}"; do
|
|
|
|
LINK="- ${INDEX_URL}/manage/project/${PACKAGE_NAME}/settings/publishing/?provider=github&owner=${GITHUB_REPOSITORY_OWNER}&repository=${REPOSITORY_NAME}&workflow_filename=${WORKFLOW_FILENAME}"
|
|
|
|
ALL_LINKS+="$LINK"$'\n'
|
|
|
|
done
|
|
|
|
TRUSTED_PUBLISHING_MAGIC_LINK_NUDGE="${MAGIC_LINK_MESSAGE}"$'\n'"${ALL_LINKS}"
|
|
|
|
echo "${MAGIC_LINK_MESSAGE}" >> $GITHUB_STEP_SUMMARY
|
|
|
|
fi
|
|
|
|
|
2024-09-01 08:50:29 +08:00
|
|
|
[[ "${INPUT_USER}" == "__token__" && -z "${INPUT_PASSWORD}" ]] \
|
|
|
|
&& TRUSTED_PUBLISHING=true || TRUSTED_PUBLISHING=false
|
|
|
|
|
|
|
|
if [[ "${INPUT_ATTESTATIONS}" != "false" ]] ; then
|
|
|
|
# Setting `attestations: true` without Trusted Publishing indicates
|
|
|
|
# user confusion, since attestations (currently) require it.
|
|
|
|
if ! "${TRUSTED_PUBLISHING}" ; then
|
|
|
|
echo "${ATTESTATIONS_WITHOUT_TP_WARNING}"
|
|
|
|
INPUT_ATTESTATIONS="false"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Setting `attestations: true` with an index other than PyPI or TestPyPI
|
|
|
|
# indicates user confusion, since attestations are not supported on other
|
|
|
|
# indices presently.
|
|
|
|
if [[ ! "${INPUT_REPOSITORY_URL}" =~ pypi\.org ]] ; then
|
|
|
|
echo "${ATTESTATIONS_WRONG_INDEX_WARNING}"
|
|
|
|
INPUT_ATTESTATIONS="false"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
if "${TRUSTED_PUBLISHING}" ; then
|
2023-03-07 04:03:34 +08:00
|
|
|
# No password supplied by the user implies that we're in the OIDC flow;
|
|
|
|
# retrieve the OIDC credential and exchange it for a PyPI API token.
|
2023-12-05 09:27:16 +08:00
|
|
|
echo "::debug::Authenticating to ${INPUT_REPOSITORY_URL} via Trusted Publishing"
|
2023-03-07 04:03:34 +08:00
|
|
|
INPUT_PASSWORD="$(python /app/oidc-exchange.py)"
|
2023-03-22 22:41:35 +08:00
|
|
|
elif [[ "${INPUT_USER}" == '__token__' ]]; then
|
|
|
|
echo \
|
2023-12-05 09:27:16 +08:00
|
|
|
'::debug::Using a user-provided API token for authentication' \
|
2023-03-22 22:41:35 +08:00
|
|
|
"against ${INPUT_REPOSITORY_URL}"
|
2023-07-11 00:11:56 +08:00
|
|
|
|
|
|
|
if [[ "${INPUT_REPOSITORY_URL}" =~ pypi\.org ]]; then
|
|
|
|
echo "${TRUSTED_PUBLISHING_NUDGE}"
|
2024-09-05 23:25:58 +08:00
|
|
|
echo "${TRUSTED_PUBLISHING_MAGIC_LINK_NUDGE}"
|
2023-07-11 00:11:56 +08:00
|
|
|
fi
|
2023-03-22 22:41:35 +08:00
|
|
|
else
|
|
|
|
echo \
|
2023-12-05 09:27:16 +08:00
|
|
|
'::debug::Using a username + password pair for authentication' \
|
2023-06-08 20:56:32 +08:00
|
|
|
"against ${INPUT_REPOSITORY_URL}"
|
2023-07-11 00:11:56 +08:00
|
|
|
|
|
|
|
if [[ "${INPUT_REPOSITORY_URL}" =~ pypi\.org ]]; then
|
2023-11-06 12:53:52 +08:00
|
|
|
echo "${PASSWORD_DEPRECATION_NUDGE}"
|
2023-07-11 00:11:56 +08:00
|
|
|
echo "${TRUSTED_PUBLISHING_NUDGE}"
|
2024-09-05 23:25:58 +08:00
|
|
|
echo "${TRUSTED_PUBLISHING_MAGIC_LINK_NUDGE}"
|
2024-05-16 23:07:28 +08:00
|
|
|
exit 1
|
2023-07-11 00:11:56 +08:00
|
|
|
fi
|
2023-03-07 04:03:34 +08:00
|
|
|
fi
|
2023-03-11 08:18:41 +08:00
|
|
|
|
2019-09-12 20:38:56 +08:00
|
|
|
if [[
|
|
|
|
"$INPUT_USER" == "__token__" &&
|
|
|
|
! "$INPUT_PASSWORD" =~ ^pypi-
|
|
|
|
]]
|
|
|
|
then
|
2023-02-24 00:11:08 +08:00
|
|
|
if [[ -z "$INPUT_PASSWORD" ]]; then
|
|
|
|
echo \
|
|
|
|
::warning file='# >>' PyPA publish to PyPI GHA'%3A' \
|
|
|
|
EMPTY TOKEN \
|
|
|
|
'<< ':: \
|
|
|
|
It looks like you have not passed a password or it \
|
|
|
|
is otherwise empty. Please verify that you have passed it \
|
|
|
|
directly or, preferably, through a secret.
|
|
|
|
else
|
|
|
|
echo \
|
|
|
|
::warning file='# >>' PyPA publish to PyPI GHA'%3A' \
|
|
|
|
POTENTIALLY INVALID TOKEN \
|
|
|
|
'<< ':: \
|
|
|
|
It looks like you are trying to use an API token to \
|
|
|
|
authenticate in the package index and your token value does \
|
|
|
|
not start with '"pypi-"' as it typically should. This may \
|
|
|
|
cause an authentication error. Please verify that you have \
|
|
|
|
copied your token properly if such an error occurs.
|
|
|
|
fi
|
2019-09-12 20:38:56 +08:00
|
|
|
fi
|
|
|
|
|
2020-06-28 17:43:30 +08:00
|
|
|
if ( ! ls -A ${INPUT_PACKAGES_DIR%%/}/*.tar.gz &> /dev/null && \
|
|
|
|
! ls -A ${INPUT_PACKAGES_DIR%%/}/*.whl &> /dev/null )
|
2019-09-12 23:53:53 +08:00
|
|
|
then
|
2020-06-04 07:06:14 +08:00
|
|
|
echo \
|
2020-06-04 07:23:32 +08:00
|
|
|
::warning file='# >>' PyPA publish to PyPI GHA'%3A' \
|
|
|
|
MISSING DISTS \
|
|
|
|
'<< ':: \
|
2019-09-16 19:01:16 +08:00
|
|
|
It looks like there are no Python distribution packages to \
|
2020-06-04 07:21:51 +08:00
|
|
|
publish in the directory "'${INPUT_PACKAGES_DIR%%/}/'". \
|
2020-06-04 07:06:14 +08:00
|
|
|
Please verify that they are in place should you face this \
|
|
|
|
problem.
|
2019-09-12 23:53:53 +08:00
|
|
|
fi
|
|
|
|
|
2020-06-03 23:04:52 +08:00
|
|
|
if [[ ${INPUT_VERIFY_METADATA,,} != "false" ]] ; then
|
2020-06-03 23:40:16 +08:00
|
|
|
twine check ${INPUT_PACKAGES_DIR%%/}/*
|
2020-06-02 23:08:43 +08:00
|
|
|
fi
|
|
|
|
|
2024-05-16 23:14:58 +08:00
|
|
|
TWINE_EXTRA_ARGS=--disable-progress-bar
|
2020-06-20 03:30:53 +08:00
|
|
|
if [[ ${INPUT_SKIP_EXISTING,,} != "false" ]] ; then
|
2024-05-16 23:14:58 +08:00
|
|
|
TWINE_EXTRA_ARGS="${TWINE_EXTRA_ARGS} --skip-existing"
|
2020-06-20 03:30:53 +08:00
|
|
|
fi
|
|
|
|
|
2020-09-26 06:42:02 +08:00
|
|
|
if [[ ${INPUT_VERBOSE,,} != "false" ]] ; then
|
2020-09-15 12:31:21 +08:00
|
|
|
TWINE_EXTRA_ARGS="--verbose $TWINE_EXTRA_ARGS"
|
|
|
|
fi
|
2019-09-12 20:38:56 +08:00
|
|
|
|
2024-09-01 08:50:29 +08:00
|
|
|
if [[ ${INPUT_ATTESTATIONS,,} != "false" ]] ; then
|
|
|
|
# NOTE: Intentionally placed after `twine check`, to prevent attestation
|
|
|
|
# NOTE: generation on distributions with invalid metadata.
|
|
|
|
echo "::notice::Generating and uploading digital attestations"
|
|
|
|
python /app/attestations.py "${INPUT_PACKAGES_DIR%%/}"
|
|
|
|
|
|
|
|
TWINE_EXTRA_ARGS="--attestations $TWINE_EXTRA_ARGS"
|
|
|
|
fi
|
|
|
|
|
2022-01-09 07:05:27 +08:00
|
|
|
if [[ ${INPUT_PRINT_HASH,,} != "false" || ${INPUT_VERBOSE,,} != "false" ]] ; then
|
2022-01-13 12:50:40 +08:00
|
|
|
python /app/print-hash.py ${INPUT_PACKAGES_DIR%%/}
|
2022-01-08 12:12:15 +08:00
|
|
|
fi
|
|
|
|
|
2019-08-23 19:17:10 +08:00
|
|
|
TWINE_USERNAME="$INPUT_USER" \
|
|
|
|
TWINE_PASSWORD="$INPUT_PASSWORD" \
|
|
|
|
TWINE_REPOSITORY_URL="$INPUT_REPOSITORY_URL" \
|
2020-06-20 03:30:53 +08:00
|
|
|
exec twine upload ${TWINE_EXTRA_ARGS} ${INPUT_PACKAGES_DIR%%/}/*
|