MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

GitHub Tutorials: Builds only when specific directories have code changes include / exclude

GitHub Actions can skip a workflow when a push or pull request changes no relevant files. You do this with the paths or paths-ignore filters on the trigger. This page explains the example above and fixes it: GitHub rejects a workflow that sets both paths and paths-ignore on the same event. It then covers negation patterns, testing, and the required-status-check problem that path filters cause.

Explanation:

  • on.push.paths: Specifies the directories/files that should trigger the workflow. In this case, changes in the src/ or scripts/ directories will trigger the build.
  • on.push.paths-ignore: Specifies the directories/files that should not trigger the workflow. If changes are only in the docs/ or tests/ directories, the build will be skipped.

How path filters decide whether a workflow runs

When a push or pull_request event fires, GitHub compares the list of changed files against the path filter on that event. If the event also has a branches filter, both filters must match before the workflow starts.

FilterWorkflow runs whenTypical use
pathsAt least one changed file matches a pattern and a later ! pattern does not exclude itBuild only when src/ or scripts/ changes
paths-ignoreAt least one changed file does not match any patternBuild on everything except docs-only changes
paths with ! patternsA changed file matches a positive pattern that a later negative pattern does not cancelInclude src/ but ignore Markdown files inside it

Patterns are globs. * matches any characters except /, and ** matches across directory levels. So src/* matches only files directly inside src/, while src/** matches everything below it. Always quote patterns in YAML, because * and ! have special meanings there.

Corrected workflow

The gist above defines paths and paths-ignore under the same push event. GitHub does not allow this and marks the workflow file as invalid with an error saying that you may not define both paths and paths-ignore for the same event. The workflow never runs. The fix is to keep only paths and add exclusions as ! patterns inside the same list.

name: Conditional Build

on:
  push:
    branches:
      - main
    paths:
      - 'src/**'
      - 'scripts/**'
      - 'package.json'
      - 'package-lock.json'
      - '.github/workflows/conditional-build.yml'
      # Exclusions must come AFTER the patterns they override
      - '!src/**/*.md'
  pull_request:
    branches:
      - main
    paths:
      - 'src/**'
      - 'scripts/**'
      - 'package.json'
      - 'package-lock.json'
      - '.github/workflows/conditional-build.yml'
      - '!src/**/*.md'

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v5

      - name: Set up Node.js
        uses: actions/setup-node@v5
        with:
          node-version: '22'
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

      - name: Run build
        run: npm run buildCode language: PHP (php)
  • The docs/ and tests/ exclusions from the original are gone. Once paths lists only src/ and scripts/, a change limited to docs/ or tests/ cannot match anyway.
  • package.json, the lockfile and the workflow file itself are included, so dependency upgrades and pipeline edits also trigger a build.
  • '!src/**/*.md' shows the correct way to exclude files: put a negative pattern after the positive pattern it overrides. Order matters. A positive pattern placed after a negative one adds the matching files back.
  • The pull_request trigger has its own filter, so pull requests get the same treatment as pushes to main.
  • npm ci installs exactly what the lockfile specifies, and cache: 'npm' reuses the npm cache between runs.

Note: Node.js 16 reached end of life in 2023, and runners have stopped supporting actions that run on old Node runtimes. Use a current LTS release of Node.js for your build, and current major versions of actions/checkout and actions/setup-node.

Using paths-ignore on its own

If you want to build on almost every change and skip only documentation-style edits, paths-ignore is simpler than a long paths list. Use it on its own, never together with paths on the same event.

on:
  push:
    branches:
      - main
    paths-ignore:
      - 'docs/**'
      - '**/*.md'
      - '.github/ISSUE_TEMPLATE/**'Code language: JavaScript (javascript)

With this filter, a commit that touches only docs/guide.md is skipped. A commit that touches docs/guide.md and src/app.js still runs, because one changed file falls outside the ignore list.

Testing the filter

  1. Commit the workflow to .github/workflows/conditional-build.yml on main. The workflow file is in the paths list, so this commit triggers a run.
  2. Change a file only under docs/ and push. No new run should appear in the Actions tab.
  3. Change a file under src/ and push. A new Conditional Build run should start.
  4. Change only a Markdown file under src/ and push. The !src/**/*.md exclusion should prevent a run.
  5. Confirm the results from the terminal with GitHub CLI, as shown below.
gh run list --workflow conditional-build.yml --limit 5

# Example output
# STATUS  TITLE              WORKFLOW           BRANCH  EVENT  ID          ELAPSED  AGE
# โœ“       Update app logic   Conditional Build  main    push   1234567890  42s      2mCode language: PHP (php)

If gh is not installed yet, see How to Install GitHub CLI (gh).

Common problems and fixes

SymptomCauseFix
Workflow never runs and the Actions tab reports an invalid workflow fileBoth paths and paths-ignore are set on one eventKeep one of them and use ! patterns for exclusions
Pull request stuck on "Expected โ€” Waiting for status to be reported"A required status check comes from a workflow that path filtering skippedUse job-level filtering (next section) so the check always reports
Exclusion has no effectThe ! pattern comes before the positive pattern it should overrideMove negative patterns to the end of the list
src/* misses nested files* does not cross /Use src/**
Workflow runs on tag pushes despite filtersPath filters are not evaluated for tag pushesRestrict triggers with branches or handle tags in a separate workflow
Large pushes behave unexpectedlyGitHub evaluates only a limited number of changed files (300) when applying path filtersKeep pushes smaller, or run a job-level check that computes the diff itself

Job-level filtering for required checks and monorepos

Trigger-level filters skip the whole workflow, so no status is reported. If branch protection requires that check, the pull request cannot be merged. A common workaround is to let the workflow always start and decide per job. A job skipped through if: reports as skipped, and GitHub treats a skipped job as passing for required checks. The third-party dorny/paths-filter action is widely used for this. Review it like any other third-party action, and consider pinning it to a commit SHA.

on:
  pull_request:
    branches: [main]

jobs:
  changes:
    runs-on: ubuntu-latest
    permissions:
      pull-requests: read
    outputs:
      app: ${{ steps.filter.outputs.app }}
    steps:
      - uses: dorny/paths-filter@v3
        id: filter
        with:
          filters: |
            app:
              - 'src/**'
              - 'scripts/**'
              - 'package*.json'

  build:
    needs: changes
    if: needs.changes.outputs.app == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - uses: actions/setup-node@v5
        with:
          node-version: '22'
          cache: 'npm'
      - run: npm ci
      - run: npm run buildCode language: JavaScript (javascript)

In a monorepo, the same approach scales: define one filter per service (for example api and web) and attach a separate job to each output.

Best practices

  • Include the workflow file and dependency manifests in paths, or pipeline and dependency changes will go untested.
  • Avoid path filters on security scans and release workflows that must always run.
  • Keep the push and pull_request filters identical so the two triggers don't drift apart.
  • Add workflow_dispatch: so you can run a filtered workflow manually when needed.
  • Set a minimal permissions: block on each workflow.

For a broader introduction to triggers, jobs and runners, see GitHub Actions Essentials โ€” Learn CI/CD in 2 Hours.

Frequently asked questions

Can I use paths and paths-ignore together in GitHub Actions?

Not on the same event. GitHub rejects the workflow file. Use paths with ! negation patterns instead, or use paths on one event and paths-ignore on another.

Why does my pull request wait forever for a required check when the workflow was skipped?

A workflow that trigger-level path filters skip never reports a status, so the required check stays pending. Run the workflow on every pull request and skip individual jobs with if: conditions. Skipped jobs satisfy required checks.

Do path filters work for workflow_dispatch or scheduled runs?

No. paths and paths-ignore apply to push, pull_request and pull_request_target. Manual and scheduled runs always start.

What is the difference between src/* and src/** in a path filter?

src/* matches only files directly inside src/. src/** matches all files at any depth below src/.

Does the order of patterns in paths matter?

Yes. Patterns are evaluated in order, so a ! exclusion only takes effect if it comes after the positive pattern it should override.

Related tutorials

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

How to Build Dynamic Websites Using GitHub Pages, REST APIs and GitHub Actions

Featured image โ€” GitHub Pages dynamic website tutorial. A practical guide for developers, DevOps engineers, technical bloggers and open-source maintainers who want interactive websites and automated publishing…

Read More

How to Build a CI/CD Pipeline for an MVP Without Slowing the Launch

A CI/CD pipeline for an MVP is the smallest automated path from a merged commit to a running staging environment: build, test, deploy, and a way to…

Read More

GitHub Actions: paths-filter complete Guide

dorny/paths-filter is a GitHub Action that checks which files changed in a pull request or push. It sets a true/false output for each named filter, so later…

Read More

Bare Metal vs Cloud for CI/CD Runners and Build Servers

Why it matters where your pipelines run Most teams pick a CI runner once and never revisit it. The usual default is the hosted runner that comes…

Read More

What Is GitHub Actions Runner Controller?

GitHub Actions is a CI/CD platform that automates tasks such as building applications, running tests, creating Docker images, and deploying software. Every GitHub Actions job requires a…

Read More

CI/CD Pipeline Fundamentals for DevOps Teams: Workflow, Tooling, and Starter Configs

This guide explains how to design a practical CI/CD pipeline for DevOps teams, from commit checks to production rollout. It includes a workflow diagram, tool comparison table, and starter pipeline examples you can adapt.

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x