Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Not a workflow: GitHub reads the generated-release-notes configuration from this path and
# nowhere else. The release itself is cut by the workflow under `.github/workflows/`.
#
# The generated list is appended under the hand-written CHANGELOG.md section, so it exists to
# credit contributors and link every pull request, not to describe the release. Dependency and
# tooling bumps are grouped so they do not sit between the real changes.
changelog:
exclude:
authors:
- github-actions[bot]
categories:
- title: Changes
labels:
- "*"
exclude:
labels:
- dependencies
- github_actions
- pre_commit
- title: Dependency and tooling updates
labels:
- dependencies
- github_actions
- pre_commit
64 changes: 64 additions & 0 deletions .github/workflows/changelog.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Changelog

# pull_request rather than pull_request_target: this needs the PR's changed-file list and no
# secrets, so the read-only token a fork PR receives is enough, and no PR content is checked
# out or executed. `labeled`/`unlabeled` are here so applying the skip label re-runs the check
# rather than leaving a stale failure behind.
on:
pull_request:
types:
- opened
- reopened
- synchronize
- labeled
- unlabeled
branches:
- main

permissions:
contents: read
pull-requests: read

jobs:
changelog-entry:
name: Changelog entry
runs-on: ubuntu-latest
steps:
# The job runs unconditionally and decides inside the step, rather than skipping on an
# `if`: a skipped job reports as neither passing nor failing, and the reason a PR is
# exempt belongs in the log where the next reader will look for it.
- name: Require a changelog entry beside a user-visible change
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPOSITORY: ${{ github.repository }}
PR_NUMBER: ${{ github.event.pull_request.number }}
# contains() matches an array element exactly, so a label carrying a comma or a
# name this one is a prefix of cannot be mistaken for it
SKIP_LABELLED: ${{ contains(github.event.pull_request.labels.*.name, 'no-changelog') }}
IS_DEPENDABOT: ${{ github.event.pull_request.user.login == 'dependabot[bot]' }}
run: |
set -euo pipefail

if [ "${SKIP_LABELLED}" = "true" ]; then
echo "Labelled no-changelog; entry not required."
exit 0
fi
if [ "${IS_DEPENDABOT}" = "true" ]; then
echo "Dependency bump; the generated release notes list these under their own heading."
exit 0
fi

changed="$(gh api --paginate "repos/${REPOSITORY}/pulls/${PR_NUMBER}/files" --jq '.[].filename')"

if ! printf '%s\n' "${changed}" | grep -qE '^(purepdb/|pyproject.toml$)'; then
echo "No shipped files changed; entry not required."
exit 0
fi

if printf '%s\n' "${changed}" | grep -qx 'CHANGELOG.md'; then
echo "CHANGELOG.md touched beside the change."
exit 0
fi

echo "::error file=CHANGELOG.md::This PR changes shipped files but does not touch CHANGELOG.md. Add an entry under ## [Unreleased], or apply the no-changelog label if the change genuinely needs no entry."
exit 1
271 changes: 271 additions & 0 deletions .github/workflows/publish-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
name: Publish release

# Pushing a vX.Y.Z tag is the release. A manual run rehearses the same path against TestPyPI
# and stops before the GitHub release, so the whole workflow can be exercised without
# consuming a version number on PyPI.
on:
push:
tags:
- "v*"
workflow_dispatch:

permissions:
contents: read

# A cancelled publish can leave PyPI holding some files of a version and not others, and the
# version number cannot be reused afterwards.
concurrency:
group: release-${{ github.ref }}
cancel-in-progress: false

defaults:
run:
shell: bash

env:
PYTHON_VERSION: "3.11"

jobs:
verify:
name: Verify the tag
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
contents: read
actions: read
issues: read
outputs:
version: ${{ steps.guard.outputs.version }}
prerelease: ${{ steps.guard.outputs.prerelease }}
steps:
- name: Refuse anything that is not a tag
env:
REF_TYPE: ${{ github.ref_type }}
REF_NAME: ${{ github.ref_name }}
run: |
if [ "${REF_TYPE}" != "tag" ]; then
echo "::error::Run this from a tag, not ${REF_TYPE} ${REF_NAME}."
exit 1
fi

- name: Checkout the tag
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
persist-credentials: false

- name: Set up Python
uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
with:
python-version: ${{ env.PYTHON_VERSION }}

- name: Check the tag against the packaged version and the changelog
id: guard
env:
TAG: ${{ github.ref_name }}
run: |
python .github/workflows/scripts/release_guard.py \
--tag "${TAG}" --notes release-notes.md --github-output "${GITHUB_OUTPUT}"

- name: Check the tag is on main
env:
TAG: ${{ github.ref_name }}
run: |
git fetch --quiet origin main
if ! git merge-base --is-ancestor "${TAG}" origin/main; then
echo "::error::${TAG} is not an ancestor of main; releases are cut from main."
exit 1
fi

- name: Check CI passed on this commit
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
SHA: ${{ github.sha }}
run: |
conclusion=$(gh api \
"repos/${REPO}/actions/workflows/ci.yml/runs?head_sha=${SHA}&status=completed" \
--jq '.workflow_runs[0].conclusion // ""')
if [ -z "${conclusion}" ]; then
echo "::error::No completed CI run for ${SHA}. Wait for main to go green, then re-run."
exit 1
fi
if [ "${conclusion}" != "success" ]; then
echo "::error::CI concluded '${conclusion}' for ${SHA}. Fix main before releasing."
exit 1
fi
echo "CI passed on ${SHA}"

# Milestones here are named for the tag they ship in, so an open item is work that was
# assigned to this release and did not make it. A repository that does not track a release
# this way has no milestone for the tag, which is not an error.
- name: Check the milestone for this tag is finished
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
TAG: ${{ github.ref_name }}
run: |
open_items=$(gh api "repos/${REPO}/milestones?state=all&per_page=100" \
--jq "[.[] | select(.title == \"${TAG}\")] | .[0].open_issues // \"none\"")
if [ "${open_items}" = "none" ]; then
echo "::notice::No milestone named ${TAG}; releasing without one."
elif [ "${open_items}" != "0" ]; then
echo "::error::Milestone ${TAG} still has ${open_items} open item(s)."
exit 1
else
echo "Milestone ${TAG} reads 100%"
fi

- name: Keep the release notes for the later jobs
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: release-notes
path: release-notes.md
if-no-files-found: error

build:
name: Build and check the distribution
needs: verify
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- name: Checkout the tag
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false

- name: Set up Python
uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
with:
python-version: ${{ env.PYTHON_VERSION }}

# Isolated: the wheel is a product of the declared build requirements, not of the runner.
# The tool versions come from the dev group, the one place they are pinned.
- name: Build the sdist and the wheel
run: |
python -m pip install --upgrade pip
python -m pip install --group dev
python -m build
python -m twine check --strict dist/*

# The artifact is tested, not the tree it came from: a clean environment carrying only the
# declared runtime dependencies has to import the package and run its entry point. `-P`
# keeps the checkout in the working directory off sys.path, so the import cannot fall back
# to the source tree and pass for a wheel that is missing something.
- name: Smoke-test the built wheel
env:
VERSION: ${{ needs.verify.outputs.version }}
run: |
python -m venv /tmp/smoke
/tmp/smoke/bin/python -m pip install --upgrade pip
/tmp/smoke/bin/python -m pip install dist/purepdb-"${VERSION}"-py3-none-any.whl
/tmp/smoke/bin/python -P - <<'PY'
import os
import pathlib

import purepdb

installed = pathlib.Path(purepdb.__file__).parent
assert "site-packages" in str(installed), f"imported a source tree: {installed}"
assert purepdb.__version__ == os.environ["VERSION"], purepdb.__version__
assert (installed / "py.typed").exists(), "py.typed did not ship"
print(f"purepdb {purepdb.__version__} imported from {installed}")
PY
/tmp/smoke/bin/purepdb --help >/dev/null

- name: Keep the distribution for the later jobs
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: distribution
path: dist/
if-no-files-found: error

publish:
name: Publish to ${{ github.event_name == 'workflow_dispatch' && 'TestPyPI' || 'PyPI' }}
needs: [verify, build]
runs-on: ubuntu-latest
timeout-minutes: 15
environment:
name: ${{ github.event_name == 'workflow_dispatch' && 'testpypi' || 'pypi' }}
url: ${{ github.event_name == 'workflow_dispatch' && 'https://test.pypi.org/p/purepdb' || 'https://pypi.org/p/purepdb' }}
permissions:
# mandatory for trusted publishing, and it must be the only credential in the job
id-token: write
steps:
- name: Fetch the distribution
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: distribution
path: dist

- name: Publish
uses: pypa/gh-action-pypi-publish@dc37677b2e1c63e2034f94d8a5b11f265b73ba33 # v1.14.2
with:
# empty resolves to PyPI: the canonical input carries no default and the action falls
# through to the deprecated alias, which defaults to the real index
repository-url: ${{ github.event_name == 'workflow_dispatch' && 'https://test.pypi.org/legacy/' || '' }}
# a PyPI collision means the version is already public, so only a rehearsal skips it
skip-existing: ${{ github.event_name == 'workflow_dispatch' }}
attestations: true
print-hash: true

release:
name: Create the GitHub release
needs: [verify, build, publish]
if: github.event_name != 'workflow_dispatch'
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
contents: write
issues: write
steps:
- name: Fetch the distribution
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: distribution
path: dist

- name: Fetch the release notes
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: release-notes

# --generate-notes appends its list under --notes-file rather than replacing it, so the
# changelog section leads and `.github/release.yml` groups what follows. A pre-release
# is marked as such and never becomes "latest".
- name: Create the release
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
TAG: ${{ github.ref_name }}
PRERELEASE: ${{ needs.verify.outputs.prerelease }}
run: |
shopt -s failglob
flags=(--latest)
if [ "${PRERELEASE}" = "true" ]; then
flags=(--prerelease --latest=false)
fi
gh release create "${TAG}" dist/* \
--repo "${REPO}" \
--title "${TAG}" \
--notes-file release-notes.md \
--generate-notes \
--verify-tag \
"${flags[@]}"

# Last, so a failure here leaves a released version with an open milestone rather than a
# closed milestone and no release.
- name: Close the milestone for this tag
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
TAG: ${{ github.ref_name }}
run: |
number=$(gh api "repos/${REPO}/milestones?state=open&per_page=100" \
--jq "[.[] | select(.title == \"${TAG}\")] | .[0].number // \"\"")
if [ -z "${number}" ]; then
echo "No open milestone named ${TAG}; nothing to close."
else
gh api -X PATCH "repos/${REPO}/milestones/${number}" -f state=closed >/dev/null
echo "Closed milestone ${TAG}"
fi
Loading