Reusable GitHub workflow used in the HyperSpy community
This repository centralises the shared CI workflows, composite actions, and AI policy enforcement tooling that the HyperSpy organisation uses across its repositories. Adopting the AI policy in a downstream repository is a four-step process described below.
doc.yml- builds the documentation site.push_doc.yml- publishes the built documentation to thegh-pagesbranch.push_doc_preview.yml- publishes a preview of the documentation for a pull request.package_and_test.yml- builds sdist/wheel packages and runs the test suite.integration_tests.yml- runs the integration test matrix against multiple dependency versions.download_exspy_GOS.yml- downloads the eXSpy GOS test data artefacts.download_rsciio_test_data.yml- downloads the RosettaSciIO test data artefacts.release_pure_python.yml- builds and publishes a pure-Python release. PyPI trusted publishing does not currently support publishing from a reusable workflow; put the publishing job in the consumer's top-level workflow when using trusted publishing.
create-github-release/- creates a GitHub release from a tag.download-artifact/- downloads a workflow run artefact.extract-release-notes/- extracts release notes from a changelog fragment tree.
AI-POLICY.md- the canonical HyperSpy AI policy text, including the sentinel-delimited block that must be pinned in every consumer'sAGENTS.md.scripts/- the Python tools that the reusable workflow and the pre-commit hooks run:hyperspy_ai_patterns.py- the single source of truth for the AICo-authored-byregexes.check-ai-co-author.py- commit-msg hook that rejects prohibitedCo-authored-byAI trailers.ci-check-ai-trailers.py- CI-side scan over a commit range.check-agents-policy.py- verifies (and with--fixinserts/refreshes) the pinned policy block inAGENTS.md.ci-check-changelog.py- towncrier-aware check that a changelog fragment is present when package source files change.
.pre-commit-hooks.yaml- the manifest that publishes thecheck-ai-co-authorhook to consumers that add this repository to theirrepos:list..github/workflows/compliance.yml- read-only reusable workflow with theai-trailers,changelog, andagents-policychecks..github/workflows/update-ai-policy.yml- separate reusable workflow that updates the policy pin and opens or updates a pull request..github/workflows/update-ai-policy-schedule.yml- daily caller for this repository's policy updates..github/workflows/self-test.yml- runs the test suite and smoke checks for the policy pin, trailer scanner and commit-msg hook on every pull request.
The examples below use main for the shared tooling while the AI policy is
under development. To test changes that have not yet been merged to main,
see Testing changes in a fork.
Add (or extend) the consumer's .pre-commit-config.yaml with the following
block:
default_install_hook_types: [pre-commit, commit-msg]
repos:
- repo: https://github.com/hyperspy/.github
rev: main
hooks:
- id: check-ai-co-authorThe check-ai-co-author hook ships from this repository's
.pre-commit-hooks.yaml and runs at the commit-msg stage. It rejects commits
that carry a prohibited AI Co-authored-by: trailer.
pre-commit does not support moving refs for automatic updates: it caches the
hook at installation, so rev: main does not update by itself. To refresh it,
run pre-commit clean and reinstall the hooks; this clears all cached
pre-commit repositories. See
pre-commit's guidance on latest versions.
Install the hooks in the consumer repository before the first commit:
pre-commit install --install-hooksCreate a root AGENTS.md if the repository does not already have one. Insert
the entire sentinel-delimited block from AI-POLICY.md at the
same tooling ref used below, including both sentinel lines. Put it below the
MANUAL marker if the file has one. Alternatively, from the consumer
repository, run the script in a local checkout of the shared tooling:
python /path/to/shared-tooling/scripts/check-agents-policy.py --fix AGENTS.md
python /path/to/shared-tooling/scripts/check-agents-policy.py AGENTS.mdReplace /path/to/shared-tooling with the checkout path. Use Python 3 from
your active environment; on systems that name the command python3, use that
instead. The target file must already exist. The first command returns exit
code 1 when it changes the file; the second checks that the result matches the
policy in the tooling checkout.
Create .github/workflows/compliance.yml in the consumer repository:
name: Compliance
on:
pull_request:
push:
branches-ignore: ['dependabot/**', 'pre-commit-ci-update-config']
permissions:
contents: read
jobs:
compliance:
uses: hyperspy/.github/.github/workflows/compliance.yml@main
with:
tooling_ref: main
source_dirs: <package dir> # omit to derive from [tool.towncrier]The uses: ref selects the reusable workflow; tooling_repo and
tooling_ref separately select the scripts and policy it checks out. These
examples use main for both while the policy is under development. For a
repository without package source or towncrier settings, set
check_changelog: false or configure the appropriate source and changelog
directories.
Add a thin caller workflow to schedule policy synchronization daily:
name: Schedule AI policy updates
on:
schedule:
- cron: "17 3 * * *"
workflow_dispatch:
permissions:
contents: write
pull-requests: write
concurrency:
group: ai-policy-update-${{ github.repository }}
cancel-in-progress: false
jobs:
update-policy:
uses: hyperspy/.github/.github/workflows/update-ai-policy.yml@main
with:
tooling_ref: mainThe scheduled caller invokes the separate update-ai-policy.yml workflow and
grants write permissions to that job chain. The compliance workflow needs
only read access. If your scheduled caller previously invoked compliance.yml
with update_policy: true, switch its uses: reference to
update-ai-policy.yml and remove that input.
The policy update uses a fixed ai-policy-update branch and opens or updates
a ready-to-review pull request containing only AGENTS.md changes. It does
not commit directly to the default branch. The target AGENTS.md must already
exist. Both callers should use the same tooling_repo, tooling_ref, and
agents_file, so the normal policy check validates the version and target
used by the updater. With tooling_ref: main, both use the latest policy and
scripts on each run. The schedule checks daily at 03:17 UTC; use
workflow_dispatch to check sooner. The update takes effect in the consumer
repository when its policy pull request is merged, and the compliance check
can report a stale block in the meantime.
The workflow also accepts policy_branch and policy_pr_title inputs. The
pull-request action uses the repository's GITHUB_TOKEN by default, so no
additional token or repository secret is needed. The caller and update job
need contents: write and pull-requests: write, as shown above, and the
repository's Actions settings
must allow GitHub Actions to create pull requests. GitHub requires a user
with write access to approve the resulting pull_request workflow runs before
they start; see events that trigger workflows.
In the consumer repository's branch protection settings, mark the enabled status checks as required:
compliance / ai-trailerscompliance / changelogcompliance / agents-policy
These names use the caller job id (compliance) and the shared workflow job
ids. GitHub does not make them required automatically. The changelog check
runs on pull requests only; the trailer check fails pull requests but reports
push violations as warnings.
tooling_repo and tooling_ref select the checkout containing the shared
scripts and policy; the other inputs control the compliance checks.
| Input | Type | Default | Description |
|---|---|---|---|
tooling_repo |
string | hyperspy/.github |
Repository holding the shared compliance scripts and AI-POLICY.md. |
tooling_ref |
string | main |
Git ref of tooling_repo to use (tag, branch, or commit SHA). |
pyproject |
string | pyproject.toml |
Path to the pyproject.toml used to derive towncrier settings. |
source_dirs |
string | '' |
Space-separated source directories whose .py changes require a changelog fragment. Empty = derive from [tool.towncrier]. |
changelog_dir |
string | '' |
Changelog fragment directory. Empty = derive from [tool.towncrier].directory. |
check_changelog |
boolean | true |
Run the changelog job. |
check_agents_policy |
boolean | true |
Run the agents-policy job. |
agents_file |
string | AGENTS.md |
Root AGENTS.md that must carry the pinned policy block. |
The policy-update workflow accepts the same tooling_repo, tooling_ref, and
agents_file inputs and defaults as the compliance workflow, plus:
| Input | Type | Default | Description |
|---|---|---|---|
policy_branch |
string | ai-policy-update |
Fixed branch used for policy update pull requests. |
policy_pr_title |
string | Update pinned AI policy block |
Title for the policy update pull request. |
The workflows can be exercised against a fork before changes are merged to
main. Point both uses: and the tooling inputs at the fork and branch in
the consumer's caller:
jobs:
compliance:
uses: <org>/<fork-of-dot-github>/.github/workflows/compliance.yml@<branch-on-fork>
with:
tooling_repo: <org>/<fork-of-dot-github>
tooling_ref: <branch-on-fork>When testing policy updates, point the scheduled caller's uses: to the
fork's update-ai-policy.yml at the same branch, and apply the same tooling
inputs. For hook testing, point the pre-commit repo: at the fork and set
rev: to the branch being tested; the cache behaviour described above still
applies.
After cloning, install the local hooks:
pre-commit install --install-hooksThe Python tooling is exercised by:
python -m pytest -q tests- the Python test suite.pre-commit run --all-files- the configured pre-commit-stage checks..github/workflows/self-test.yml- pytest, Ruff and smoke tests for the hooks and CI scripts on this repository's pushes and pull requests.