174 lines
8.3 KiB
YAML
174 lines
8.3 KiB
YAML
name: Publish accepted release to PyPI
|
|
|
|
on:
|
|
workflow_run:
|
|
workflows: [Validate published release for PyPI]
|
|
types: [completed]
|
|
workflow_dispatch:
|
|
inputs:
|
|
version:
|
|
description: Published stable release version (without v)
|
|
required: true
|
|
type: string
|
|
destination:
|
|
description: Package index
|
|
required: true
|
|
type: choice
|
|
default: testpypi
|
|
options:
|
|
- testpypi
|
|
- pypi
|
|
|
|
permissions: {}
|
|
|
|
concurrency:
|
|
group: publish-pypi-${{ github.event_name == 'workflow_run' && github.event.workflow_run.head_branch || format('v{0}', inputs.version) }}
|
|
cancel-in-progress: false
|
|
|
|
jobs:
|
|
verify:
|
|
if: >-
|
|
github.repository == 'HKUDS/nanobot' && github.ref == 'refs/heads/main' &&
|
|
(github.event_name == 'workflow_dispatch' ||
|
|
(github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success' &&
|
|
github.event.workflow_run.event == 'release' &&
|
|
github.event.workflow_run.head_repository.full_name == github.repository &&
|
|
(github.event.workflow_run.path == '.github/workflows/trigger-pypi.yml' ||
|
|
startsWith(github.event.workflow_run.path, '.github/workflows/trigger-pypi.yml@'))))
|
|
runs-on: ubuntu-latest
|
|
timeout-minutes: 30
|
|
permissions:
|
|
contents: read
|
|
actions: read
|
|
outputs:
|
|
artifact_id: ${{ steps.staged.outputs.artifact-id }}
|
|
upload_required: ${{ steps.reconcile.outputs.upload_required }}
|
|
release_version: ${{ steps.release.outputs.release_version }}
|
|
destination: ${{ steps.release.outputs.destination }}
|
|
steps:
|
|
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
|
with:
|
|
persist-credentials: false
|
|
- uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5
|
|
with:
|
|
python-version: '3.11'
|
|
- name: Install locked verification dependencies
|
|
run: |
|
|
python -m pip install --require-hashes --only-binary=:all: -r .github/release-signing-requirements.txt
|
|
- name: Match the public release to the accepted candidate
|
|
id: release
|
|
env:
|
|
GH_TOKEN: ${{ github.token }}
|
|
VERSION_INPUT: ${{ inputs.version }}
|
|
DESTINATION_INPUT: ${{ inputs.destination }}
|
|
UPSTREAM_TAG: ${{ github.event.workflow_run.head_branch }}
|
|
UPSTREAM_SHA: ${{ github.event.workflow_run.head_sha }}
|
|
shell: bash
|
|
run: |
|
|
set -euo pipefail
|
|
if [[ "$GITHUB_EVENT_NAME" == workflow_run ]]; then
|
|
[[ "$UPSTREAM_TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] || { echo 'Invalid release tag.' >&2; exit 1; }
|
|
VERSION="${UPSTREAM_TAG#v}"
|
|
destination=pypi
|
|
else
|
|
VERSION="$VERSION_INPUT"
|
|
destination="$DESTINATION_INPUT"
|
|
fi
|
|
[[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || { echo 'Expected a final X.Y.Z version.' >&2; exit 1; }
|
|
[[ "$destination" == pypi || "$destination" == testpypi ]] || { echo 'Invalid destination.' >&2; exit 1; }
|
|
tag="v$VERSION"
|
|
gh release view "$tag" --repo HKUDS/nanobot --json tagName,isDraft,isPrerelease \
|
|
| jq -e --arg tag "$tag" '.tagName == $tag and .isDraft == false and .isPrerelease == false' >/dev/null
|
|
git fetch --no-tags origin "refs/tags/$tag:refs/tags/$tag"
|
|
commit="$(git rev-parse "refs/tags/$tag^{commit}")"
|
|
if [[ "$GITHUB_EVENT_NAME" == workflow_run ]]; then
|
|
[[ "$UPSTREAM_SHA" == "$commit" ]] || { echo 'Release tag no longer matches the trigger.' >&2; exit 1; }
|
|
fi
|
|
gh release download "$tag" --repo HKUDS/nanobot --dir "$RUNNER_TEMP/release" \
|
|
--pattern 'release-manifest.json' \
|
|
--pattern 'nanobot_ai-*.whl' --pattern 'nanobot_ai-*.tar.gz' \
|
|
--pattern 'nanobot-tui-*.zip' --pattern 'nanobot-tui-*.zip.sha256'
|
|
# The build report alone isn't acceptance: require the final gate from the same attempt.
|
|
gh api --paginate --method GET 'repos/HKUDS/nanobot/actions/workflows/release-candidate.yml/runs' \
|
|
-f head_sha="$commit" -f branch=main -f event=workflow_dispatch -f status=success -F per_page=100 \
|
|
--jq '.workflow_runs[] | select(.head_repository.full_name == "HKUDS/nanobot") | [.id, .run_number, .run_attempt] | @tsv' \
|
|
> "$RUNNER_TEMP/candidate-runs"
|
|
while IFS=$'\t' read -r run number attempt; do
|
|
accepted="$(gh api "repos/HKUDS/nanobot/actions/runs/$run/attempts/$attempt/jobs?per_page=100" \
|
|
--jq 'any(.jobs[]; .name == "Candidate required" and .conclusion == "success")')"
|
|
[[ "$accepted" == true ]] || continue
|
|
report="candidate-report-$number-$attempt"
|
|
if ! gh run download "$run" --repo HKUDS/nanobot --name "$report" --dir "$RUNNER_TEMP/$report"; then
|
|
continue
|
|
fi
|
|
manifest="$RUNNER_TEMP/$report/release-artifacts/release-manifest.json"
|
|
cmp -s "$manifest" "$RUNNER_TEMP/release/release-manifest.json" || continue
|
|
python -m scripts.prepare_pypi_release --release-dir "$RUNNER_TEMP/release" \
|
|
--accepted-manifest "$manifest" --output "$RUNNER_TEMP/pypi" --version "$VERSION" --tag-commit "$commit"
|
|
echo "release_version=$VERSION" >> "$GITHUB_OUTPUT"
|
|
echo "destination=$destination" >> "$GITHUB_OUTPUT"
|
|
exit 0
|
|
done < "$RUNNER_TEMP/candidate-runs"
|
|
echo 'No matching accepted main candidate report. Check the original run, tag and manifest before retrying.' >&2
|
|
exit 1
|
|
- name: Check for an interrupted upload
|
|
id: reconcile
|
|
env:
|
|
VERSION: ${{ steps.release.outputs.release_version }}
|
|
DESTINATION: ${{ steps.release.outputs.destination }}
|
|
shell: bash
|
|
run: |
|
|
set -euo pipefail
|
|
python -m scripts.reconcile_pypi_release --directory "$RUNNER_TEMP/pypi" \
|
|
--version "$VERSION" --destination "$DESTINATION"
|
|
if [[ -n "$(find "$RUNNER_TEMP/pypi" -type f -print -quit)" ]]; then
|
|
echo 'upload_required=true' >> "$GITHUB_OUTPUT"
|
|
else
|
|
echo 'upload_required=false' >> "$GITHUB_OUTPUT"
|
|
echo 'The complete release is already present with matching hashes.'
|
|
fi
|
|
- name: Transfer verified PyPI files
|
|
if: steps.reconcile.outputs.upload_required == 'true'
|
|
id: staged
|
|
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
|
with:
|
|
name: verified-pypi-${{ github.run_id }}-${{ github.run_attempt }}
|
|
path: ${{ runner.temp }}/pypi/*
|
|
if-no-files-found: error
|
|
retention-days: 1
|
|
|
|
publish:
|
|
needs: verify
|
|
if: needs.verify.outputs.upload_required == 'true'
|
|
runs-on: ubuntu-latest
|
|
environment: ${{ needs.verify.outputs.destination }}
|
|
timeout-minutes: 20
|
|
permissions:
|
|
id-token: write
|
|
steps:
|
|
- name: Require a fresh run
|
|
shell: bash
|
|
run: |
|
|
# Reruns can reuse previous staging outputs instead of reconciling the current index.
|
|
[[ "$GITHUB_RUN_ATTEMPT" == 1 ]] || { echo 'To retry, start a new workflow run for the same version and destination.' >&2; exit 1; }
|
|
- uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
|
|
with:
|
|
artifact-ids: ${{ needs.verify.outputs.artifact_id }}
|
|
path: dist
|
|
merge-multiple: true
|
|
- name: Publish with PyPI Trusted Publishing
|
|
if: needs.verify.outputs.destination == 'pypi'
|
|
uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # release/v1
|
|
- name: Publish to TestPyPI
|
|
if: needs.verify.outputs.destination == 'testpypi'
|
|
uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # release/v1
|
|
with:
|
|
repository-url: https://test.pypi.org/legacy/
|
|
- name: Show safe retry instructions
|
|
if: failure()
|
|
shell: bash
|
|
run: |
|
|
message='To resume, use "Run workflow" on main with the same version and destination. Do not rerun the old run. Preserve the original approved release files.'
|
|
echo "::notice title=Retry the complete publication workflow::$message"
|
|
printf '### Resume publication\n\n%s\n' "$message" >> "$GITHUB_STEP_SUMMARY"
|