diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml deleted file mode 100644 index 38c57d279..000000000 --- a/.github/workflows/publish.yml +++ /dev/null @@ -1,119 +0,0 @@ -name: Publish to PyPI - -on: - workflow_dispatch: - inputs: - target: - description: Index to publish to - required: true - default: TestPyPI - type: choice - options: - - TestPyPI - - PyPI - package: - description: Package(s) to publish - required: true - default: both - type: choice - options: - - both - - timenet - - timenet-connectors - -run-name: Publish ${{ inputs.package }} to ${{ inputs.target }} - -env: - BRANCH_NAME: ${{ github.head_ref || github.ref_name }} - -jobs: - publish: - runs-on: ubuntu-latest - # The `pypi` environment gates every run behind its required reviewers, so a publish only - # proceeds once one of the listed admins approves it, even though workflow_dispatch is offered - # to anyone with write access. Configure the reviewers in Settings > Environments > pypi. - environment: - name: pypi - permissions: - # Mint the OIDC token uv exchanges for a PyPI/TestPyPI trusted-publishing token. No API - # tokens are stored as repo secrets. - id-token: write - contents: read - steps: - - name: Guard PyPI to main - run: | - echo "Branch: $BRANCH_NAME" - if [ "${{ inputs.target }}" = "PyPI" ] && [ "$BRANCH_NAME" != "main" ]; then - echo "::error title=Invalid branch::PyPI releases must come from main." - exit 1 - fi - - name: Checkout - uses: actions/checkout@v5 - - name: Set up uv - uses: astral-sh/setup-uv@v7 - - name: Build - # build_dists.py records the commit inside each package, so a build can name its code. - run: | - if [ "${{ inputs.package }}" = "both" ]; then - uv run scripts/build_dists.py --all-packages - else - uv run scripts/build_dists.py --package "${{ inputs.package }}" - fi - - name: Smoke test built artifacts - # Import each built wheel and sdist in an isolated environment to catch missing files or - # broken metadata before uploading. Each artifact must also report this commit, clean, as - # the code it was built from. timenet-connectors depends on timenet, so hand it the - # wheel we just built when it is present (a `both` run); otherwise timenet resolves from the - # default index, which requires it to be published already. - run: | - set -euo pipefail - shopt -s nullglob - timenet_whl=(dist/timenet-*.whl) - smoke() { - local module="$1" dist="$2"; shift 2 - local args=() artifact - for artifact in "$@"; do args+=(--with "$artifact"); done - echo "::group::import $module" - uv run --isolated --no-project "${args[@]}" \ - python -c "import $module; print('smoke ok:', $module.__name__)" - uv run --isolated --no-project "${args[@]}" python -c " - from timenet.manifest import GitSource - from timenet.provenance import PackageSnapshot - commit = PackageSnapshot.read('$dist').source - expected = GitSource(sha='$GITHUB_SHA', contains_local_changes=False) - assert commit == expected, f'$dist records {commit}, expected {expected}' - print('commit ok:', commit)" - echo "::endgroup::" - } - for artifact in dist/*; do - case "$artifact" in - dist/timenet-*.whl | dist/timenet-*.tar.gz) - smoke timenet timenet "$artifact" ;; - dist/timenet_connectors-*.whl | dist/timenet_connectors-*.tar.gz) - smoke timenet_connectors timenet-connectors ${timenet_whl[@]+"${timenet_whl[@]}"} "$artifact" ;; - esac - done - - name: Publish - run: | - if [ "${{ inputs.target }}" = "TestPyPI" ]; then - uv publish --publish-url https://test.pypi.org/legacy/ - else - uv publish - fi - - name: Summary - if: success() - run: | - host=pypi.org - if [ "${{ inputs.target }}" = "TestPyPI" ]; then host=test.pypi.org; fi - { - echo "### Published to ${{ inputs.target }} :rocket:" - echo - for whl in dist/*.whl; do - base=$(basename "$whl") - dist_name=${base%%-*} - rest=${base#*-} - version=${rest%%-*} - project=${dist_name//_/-} - echo "- \`$project\` $version — https://$host/project/$project/$version/" - done - } >> "$GITHUB_STEP_SUMMARY" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 4f64baecb..05b5aba1e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -5,9 +5,12 @@ name: Release # 1. Run this workflow on main and select the package. It opens a release PR that sets the # versions. The PR body has the release notes between release-notes markers. Edit the notes # there. The release uses the text in the merged PR. -# 2. Merge the release PR. The same workflow then builds and tests the wheel and the sdist of each -# package. It creates the tag and the GitHub release with the built files attached. -# The upload to TestPyPI and PyPI is still done by publish.yml. +# 2. Merge the release PR. The same workflow then does these steps: +# - It builds and tests the wheel and the sdist of each package. +# - It creates the tag and the GitHub release. +# - It waits for your approval in the pypi environment, then uploads to TestPyPI. +# - It waits for your approval again, then uploads to PyPI. +# The upload steps send the built files. They do not build. # If you release both packages, the timenet floor of timenet-connectors goes up. If you release one # package, the release PR warns when the other package has changes that the release leaves out. # The release PR fixes the versions. If main needs a bigger version bump before you merge, close @@ -19,6 +22,8 @@ name: Release # - A GitHub App with write access to contents and pull requests, installed on this repository. # Set its id in the RELEASE_APP_ID variable and its key in the RELEASE_APP_PRIVATE_KEY secret. # The repository settings block PRs from GITHUB_TOKEN, and those PRs do not start CI. +# - A trusted publisher on PyPI and TestPyPI for each package: workflow release.yml, environment +# pypi. on: workflow_dispatch: @@ -120,8 +125,49 @@ jobs: done - name: Smoke test built artifacts run: uv run .github/scripts/release.py smoke + - name: Keep the built files for the publish jobs + uses: actions/upload-artifact@v4 + with: + name: dist + path: dist/ + if-no-files-found: error + overwrite: true - name: Tag and create the GitHub releases env: GH_TOKEN: ${{ github.token }} RELEASED: ${{ steps.released.outputs.released }} run: uv run .github/scripts/release.py github-release --released "$RELEASED" + + publish-testpypi: + # Wait for approval in the pypi environment. Upload the files that the release job built. + needs: release + runs-on: ubuntu-latest + environment: + name: pypi + permissions: + id-token: write + steps: + - uses: astral-sh/setup-uv@v8.2.0 + - uses: actions/download-artifact@v4 + with: + name: dist + path: dist/ + - name: Publish to TestPyPI + run: uv publish --publish-url https://test.pypi.org/legacy/ --check-url https://test.pypi.org/simple/ dist/* + + publish-pypi: + # Start after TestPyPI has the same files. Wait for approval in the pypi environment. + needs: publish-testpypi + runs-on: ubuntu-latest + environment: + name: pypi + permissions: + id-token: write + steps: + - uses: astral-sh/setup-uv@v8.2.0 + - uses: actions/download-artifact@v4 + with: + name: dist + path: dist/ + - name: Publish to PyPI + run: uv publish --check-url https://pypi.org/simple/ dist/*