Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to factor `paths:` in common for push: and pull_request: in github actions?

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)

like image 230
timotheecour Avatar asked Apr 10 '26 07:04

timotheecour


1 Answers

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.

like image 162
alelom Avatar answered Apr 12 '26 21:04

alelom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!