I’d like to factor the common paths in this:
on:
push:
# Run only on changes on these files
paths:
- 'lib/**.nim'
- 'doc/**.rst'
- 'doc/nimdoc.css'
- '.github/workflows/ci_docs.yml'
pull_request:
# Run only on changes on these files
paths:
- 'lib/**.nim'
- 'doc/**.rst'
- '.github/workflows/ci_docs.yml'
documentation on github mentions [push, pull_request] but it doesn’t work if we have paths node. What is the syntax to avoid code duplication?
(apologies in advance for cross posting with https://github.community/t/how-to-factor-paths-in-common-for-push-and-pull-request/115967, if I get any answer I'll update on the other end)
You can achieve what you want using YAML anchors, which let you define a list once and reuse it in multiple places.
Support for YAML anchors in GitHub actions was added in August 2025 (it was one of the key criticism/downsides against e.g. GitLab).
In the following example, both the push: and pull_request: sections share the same set of paths defined in the push: section.
name: Test Integration
on:
workflow_dispatch:
push:
branches:
- main
paths: &integration_paths
- 'src/integration/**'
- 'src/_shared/**'
- 'src/models/_shared_m/**'
- 'src/models/Object_detection/**'
- 'src/models/Information_extraction/**'
- 'pyproject.toml'
- '.github/workflows/test-integration.yml' # monitor changes to the action itself.
- '!**_SS/**' # do not match superseded _SS folders.
pull_request:
branches:
- '**'
paths: *integration_paths
Anchors can be placed in any YAML section that is looked at by GitHub actions -- i.e. you cannot create your own my_variable: section to store some variable linked with anchors, because that's not interpreted by actions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With