dorny/paths-filter

repository·master·Indexed 25 days ago

https://github.com/dorny/paths-filter

A GitHub Action that enables conditional execution of workflow steps and jobs based on which files were modified in a pull request, feature branch, or recent commit. It is designed to help monorepos avoid running expensive tasks when unrelated components are changed by detecting file changes and setting output variables for use in GitHub Actions 'if' conditionals or dynamic matrix jobs.

Tokens
4K
Snippets
8
Records
30
Agent score
83%

What's inside dorny/paths-filter

  1. Quickstart: Run steps conditionally based on changed files

    master

    Use dorny/paths-filter@v4 to detect file changes and set an output variable. You can then use this output in a GitHub Actions if conditional to run specific steps or jobs only when certain files (e.g., in a src folder) have been modified.

    - uses: dorny/paths-filter@v4
      id: changes
      with:
        filters: |
          src:
            - 'src/**'
    
    # run only if some file in 'src' folder was changed
    - if: steps.changes.outputs.src == 'true'
      run: ...
  2. Configure a matrix job using change detection

    master

    You can use the changes output to dynamically populate a GitHub Actions matrix. This allows you to run jobs only for the specific packages that were modified.

    1. Create a changes job that runs the filter.
    2. Map the steps.filter.outputs.changes to a job output.
    3. Use fromJSON() in the downstream job's matrix strategy to parse the array.
    jobs:
      changes:
        runs-on: ubuntu-latest
        permissions:
          pull-requests: read
        outputs:
          packages: ${{ steps.filter.outputs.changes }}
        steps:
        - uses: dorny/paths-filter@v4
          id: filter
          with:
            filters: |
              package1: src/package1
              package2: src/package2
    
      build:
        needs: changes
        strategy:
          matrix:
            package: ${{ fromJSON(needs.changes.outputs.packages) }}
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v6
          - ...
  3. Supported workflow trigger scenarios

    master

    The action supports various GitHub event types:

    • Pull requests (pull_request, pull_request_target): Detects changes against the PR base branch using the GitHub REST API. Requires pull-requests: read permission.
    • Feature branches (push or other events): Detects changes against the merge-base with the configured base branch using git commands. Requires the repository to be checked out.
    • Merge queue (merge_group): Detects changes using git commands. base and ref default to commit hashes from the event.
    • Long-lived branches (push): If base is the same as the triggering branch, changes are detected against the most recent commit before the push.
    • Local changes: If base is set to HEAD, changes are detected against the current HEAD (untracked files are ignored).
  4. Detect changes against different bases

    master

    Depending on your workflow trigger, you can specify the base to compare changes against:

    • Pull Requests: Automatically detects changes against the PR base branch (no base config needed).
    • Feature Branches: Use base: <branch_name> to compare against a specific branch (e.g., develop).
    • Long-lived Branches: Use base: ${{ github.ref }} to compare against the most recent commit on the same branch.
    • Local Changes: Use base: HEAD to detect staged and unstaged local changes.
  5. Configure paths-filter inputs

    master

    The dorny/paths-filter@v4 action accepts several configuration parameters to control how changes are detected and how results are formatted:

    • filters: Defines the rules. Can be an inline YAML string or a path to a .yaml file. Each filter has a name and a list of glob patterns.
    • base: The branch, tag, or commit SHA to compare against. Defaults to the repository's default branch.
    • ref: The Git reference (e.g., branch name) to detect changes from. Useful for repository_dispatch events. Defaults to ${{ github.ref }}.
    • initial-fetch-depth: Number of commits to fetch from the base branch. Defaults to 100.
    • list-files: Format for the list of matching files output. Options: none (default), csv, json, shell, escape.
    • working-directory: The relative path under $GITHUB_WORKSPACE where the repo was checked out.
    • token: Personal access token for GitHub REST API (used for pull_request events). Defaults to ${{ github.token }}. If set to an empty string, it falls back to git commands.
    • predicate-quantifier: Controls matching logic. some (default) matches if at least one pattern matches. every matches only if all patterns match.
  6. Configure list-files output format

    master

    You can control how the ${FILTER_NAME}_files output is formatted using the list-files option:

    • shell: Paths are escaped and space-delimited. Ideal for passing as command-line arguments in a Linux shell.
    • json: Paths are formatted as a JSON array. Ideal for passing to other GitHub Actions that consume JSON lists.
    - uses: dorny/paths-filter@v4
      id: filter
      with:
        list-files: shell
        filters: |
          markdown:
            - '*.md'
  7. Troubleshoot Git 'dubious ownership' in containers

    master

    If running in a container job, you might encounter Git dubious ownership errors. The action attempts to handle this automatically by retrying with a temporary HOME.

    If your workflow relies on credentials in ~/.git-credentials or ~/.netrc, you must manually mark the repository as safe in a preceding step:

    git config --global --add safe.directory "$GITHUB_WORKSPACE"
  8. Execute steps conditionally based on file changes

    master

    To run a specific step only when files in a certain directory change, use the filter's output in an if conditional:

    - uses: dorny/paths-filter@v4
      id: filter
      with:
        filters: |
          backend:
            - 'backend/**'
    
    - name: backend tests
      if: steps.filter.outputs.backend == 'true'
      run: npm test --prefix backend
    jobs:
      tests:
        runs-on: ubuntu-latest
        steps:
        - uses: actions/checkout@v6
        - uses: dorny/paths-filter@v4
          id: filter
          with:
            filters: |
              backend:
                - 'backend/**'
              frontend:
                - 'frontend/**'
    
        # run only if 'backend' files were changed
        - name: backend tests
          if: steps.filter.outputs.backend == 'true'
          run: ...
    
        # run only if 'frontend' files were changed
        - name: frontend tests
          if: steps.filter.outputs.frontend == 'true'
          run: ...
    
        # run if 'backend' or 'frontend' files were changed
        - name: e2e tests
          if: steps.filter.outputs.backend == 'true' || steps.filter.outputs.frontend == 'true'
          run: ...
  9. Configure the predicate quantifier for file filtering

    master

    The predicateQuantifier determines the logic used when evaluating multiple patterns within a single filter rule. This is configured via the FilterConfig object passed to the Filter constructor.

    • SOME: (Default) A file matches the rule if it satisfies at least one of the patterns. This corresponds to the some logic quantifier.
    • EVERY: A file matches the rule only if it satisfies all of the patterns. This corresponds to the every logic quantifier.
  10. Configure predicate-quantifier for filter matching

    master

    The predicate-quantifier input determines the logic used to decide if a filter group matches.

    • some: The filter matches if at least one file in the changed set matches any of the rules in the filter group. (Default)
    • all: The filter matches only if all files in the changed set match the rules in the filter group.
  11. List changed files in different formats

    master

    By setting the list-files input, you can retrieve a list of all files that matched your filters via the <id>.outputs.paths_<filter_name> output variable. Supported formats include:

    • csv: Comma-separated list, wrapping filenames with unsafe characters in double quotes.
    • json: A JSON array of file paths.
    • shell: Space-delimited list for Linux shells, wrapping filenames with single or double quotes.
    • escape: Space-delimited list for Linux shells using backslash escapes for unsafe characters.