{"id":47161,"date":"2026-11-24T22:04:41","date_gmt":"2026-11-24T22:04:41","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=47161"},"modified":"2026-11-24T22:04:41","modified_gmt":"2026-11-24T22:04:41","slug":"github-tutorials-builds-only-when-specific-directories-have-code-changes-include-exclude","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/github-tutorials-builds-only-when-specific-directories-have-code-changes-include-exclude\/","title":{"rendered":"GitHub Tutorials: Builds only when specific directories have code changes include \/ exclude"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">GitHub Actions can skip a workflow when a push or pull request changes no relevant files. You do this with the <code>paths<\/code> or <code>paths-ignore<\/code> filters on the trigger. This page explains the example above and fixes it: GitHub rejects a workflow that sets both <code>paths<\/code> and <code>paths-ignore<\/code> on the same event. It then covers negation patterns, testing, and the required-status-check problem that path filters cause.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/a480edf607d685fd38469b1bc917f9cf.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>on.push.paths<\/code>: Specifies the directories\/files that should trigger the workflow. In this case, changes in the <code>src\/<\/code> or <code>scripts\/<\/code> directories will trigger the build.<\/li>\n\n\n\n<li><code>on.push.paths-ignore<\/code>: Specifies the directories\/files that should <strong>not<\/strong> trigger the workflow. If changes are only in the <code>docs\/<\/code> or <code>tests\/<\/code> directories, the build will be skipped.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">How path filters decide whether a workflow runs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When a <code>push<\/code> or <code>pull_request<\/code> event fires, GitHub compares the list of changed files against the path filter on that event. If the event also has a <code>branches<\/code> filter, both filters must match before the workflow starts.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Filter<\/th><th>Workflow runs when<\/th><th>Typical use<\/th><\/tr><\/thead><tbody><tr><td><code>paths<\/code><\/td><td>At least one changed file matches a pattern and a later <code>!<\/code> pattern does not exclude it<\/td><td>Build only when <code>src\/<\/code> or <code>scripts\/<\/code> changes<\/td><\/tr><tr><td><code>paths-ignore<\/code><\/td><td>At least one changed file does <strong>not<\/strong> match any pattern<\/td><td>Build on everything except docs-only changes<\/td><\/tr><tr><td><code>paths<\/code> with <code>!<\/code> patterns<\/td><td>A changed file matches a positive pattern that a later negative pattern does not cancel<\/td><td>Include <code>src\/<\/code> but ignore Markdown files inside it<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Patterns are globs. <code>*<\/code> matches any characters except <code>\/<\/code>, and <code>**<\/code> matches across directory levels. So <code>src\/*<\/code> matches only files directly inside <code>src\/<\/code>, while <code>src\/**<\/code> matches everything below it. Always quote patterns in YAML, because <code>*<\/code> and <code>!<\/code> have special meanings there.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Corrected workflow<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The gist above defines <code>paths<\/code> and <code>paths-ignore<\/code> under the same <code>push<\/code> event. GitHub does not allow this and marks the workflow file as invalid with an error saying that you may not define both <code>paths<\/code> and <code>paths-ignore<\/code> for the same event. The workflow never runs. The fix is to keep only <code>paths<\/code> and add exclusions as <code>!<\/code> patterns inside the same list.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">name: Conditional Build\n\non:\n  push:\n    branches:\n      - main\n    paths:\n      - <span class=\"hljs-string\">'src\/**'<\/span>\n      - <span class=\"hljs-string\">'scripts\/**'<\/span>\n      - <span class=\"hljs-string\">'package.json'<\/span>\n      - <span class=\"hljs-string\">'package-lock.json'<\/span>\n      - <span class=\"hljs-string\">'.github\/workflows\/conditional-build.yml'<\/span>\n      <span class=\"hljs-comment\"># Exclusions must come AFTER the patterns they override<\/span>\n      - <span class=\"hljs-string\">'!src\/**\/*.md'<\/span>\n  pull_request:\n    branches:\n      - main\n    paths:\n      - <span class=\"hljs-string\">'src\/**'<\/span>\n      - <span class=\"hljs-string\">'scripts\/**'<\/span>\n      - <span class=\"hljs-string\">'package.json'<\/span>\n      - <span class=\"hljs-string\">'package-lock.json'<\/span>\n      - <span class=\"hljs-string\">'.github\/workflows\/conditional-build.yml'<\/span>\n      - <span class=\"hljs-string\">'!src\/**\/*.md'<\/span>\n\njobs:\n  build:\n    runs-on: ubuntu-latest\n    steps:\n      - name: Checkout code\n        uses: actions\/checkout@v5\n\n      - name: Set up Node.js\n        uses: actions\/setup-node@v5\n        with:\n          node-version: <span class=\"hljs-string\">'22'<\/span>\n          cache: <span class=\"hljs-string\">'npm'<\/span>\n\n      - name: Install dependencies\n        run: npm ci\n\n      - name: Run build\n        run: npm run build<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>docs\/<strong><\/code> and <code>tests\/<\/strong><\/code> exclusions from the original are gone. Once <code>paths<\/code> lists only <code>src\/<\/code> and <code>scripts\/<\/code>, a change limited to <code>docs\/<\/code> or <code>tests\/<\/code> cannot match anyway.<\/li>\n\n\n<li><code>package.json<\/code>, the lockfile and the workflow file itself are included, so dependency upgrades and pipeline edits also trigger a build.<\/li>\n\n\n<li><code>&#039;!src\/**\/*.md&#039;<\/code> 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.<\/li>\n\n\n<li>The <code>pull_request<\/code> trigger has its own filter, so pull requests get the same treatment as pushes to <code>main<\/code>.<\/li>\n\n\n<li><code>npm ci<\/code> installs exactly what the lockfile specifies, and <code>cache: &#039;npm&#039;<\/code> reuses the npm cache between runs.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> 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 <code>actions\/checkout<\/code> and <code>actions\/setup-node<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using paths-ignore on its own<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If you want to build on almost every change and skip only documentation-style edits, <code>paths-ignore<\/code> is simpler than a long <code>paths<\/code> list. Use it on its own, never together with <code>paths<\/code> on the same event.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">on:\n  push:\n    branches:\n      - main\n    paths-ignore:\n      - <span class=\"hljs-string\">'docs\/**'<\/span>\n      - <span class=\"hljs-string\">'**\/*.md'<\/span>\n      - <span class=\"hljs-string\">'.github\/ISSUE_TEMPLATE\/**'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">With this filter, a commit that touches only <code>docs\/guide.md<\/code> is skipped. A commit that touches <code>docs\/guide.md<\/code> and <code>src\/app.js<\/code> still runs, because one changed file falls outside the ignore list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Testing the filter<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Commit the workflow to <code>.github\/workflows\/conditional-build.yml<\/code> on <code>main<\/code>. The workflow file is in the <code>paths<\/code> list, so this commit triggers a run.<\/li>\n\n\n<li>Change a file only under <code>docs\/<\/code> and push. No new run should appear in the <strong>Actions<\/strong> tab.<\/li>\n\n\n<li>Change a file under <code>src\/<\/code> and push. A new <strong>Conditional Build<\/strong> run should start.<\/li>\n\n\n<li>Change only a Markdown file under <code>src\/<\/code> and push. The <code>!src\/**\/*.md<\/code> exclusion should prevent a run.<\/li>\n\n\n<li>Confirm the results from the terminal with GitHub CLI, as shown below.<\/li>\n<\/ol>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">gh run <span class=\"hljs-keyword\">list<\/span> --workflow conditional-build.yml --limit <span class=\"hljs-number\">5<\/span>\n\n<span class=\"hljs-comment\"># Example output<\/span>\n<span class=\"hljs-comment\"># STATUS  TITLE              WORKFLOW           BRANCH  EVENT  ID          ELAPSED  AGE<\/span>\n<span class=\"hljs-comment\"># \u2713       Update app logic   Conditional Build  main    push   1234567890  42s      2m<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">If <code>gh<\/code> is not installed yet, see <a href=\"https:\/\/www.devopsschool.com\/blog\/how-to-install-github-cli-gh-on-macos-windows-and-linux-complete-step-by-step-guide\/\">How to Install GitHub CLI (gh)<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common problems and fixes<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Symptom<\/th><th>Cause<\/th><th>Fix<\/th><\/tr><\/thead><tbody><tr><td>Workflow never runs and the Actions tab reports an invalid workflow file<\/td><td>Both <code>paths<\/code> and <code>paths-ignore<\/code> are set on one event<\/td><td>Keep one of them and use <code>!<\/code> patterns for exclusions<\/td><\/tr><tr><td>Pull request stuck on &quot;Expected \u2014 Waiting for status to be reported&quot;<\/td><td>A required status check comes from a workflow that path filtering skipped<\/td><td>Use job-level filtering (next section) so the check always reports<\/td><\/tr><tr><td>Exclusion has no effect<\/td><td>The <code>!<\/code> pattern comes before the positive pattern it should override<\/td><td>Move negative patterns to the end of the list<\/td><\/tr><tr><td><code>src\/*<\/code> misses nested files<\/td><td><code>*<\/code> does not cross <code>\/<\/code><\/td><td>Use <code>src\/**<\/code><\/td><\/tr><tr><td>Workflow runs on tag pushes despite filters<\/td><td>Path filters are not evaluated for tag pushes<\/td><td>Restrict triggers with <code>branches<\/code> or handle tags in a separate workflow<\/td><\/tr><tr><td>Large pushes behave unexpectedly<\/td><td>GitHub evaluates only a limited number of changed files (300) when applying path filters<\/td><td>Keep pushes smaller, or run a job-level check that computes the diff itself<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Job-level filtering for required checks and monorepos<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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 <code>if:<\/code> reports as skipped, and GitHub treats a skipped job as passing for required checks. The third-party <code>dorny\/paths-filter<\/code> action is widely used for this. Review it like any other third-party action, and consider pinning it to a commit SHA.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">on:\n  pull_request:\n    branches: &#91;main]\n\n<span class=\"hljs-attr\">jobs<\/span>:\n  changes:\n    runs-on: ubuntu-latest\n    <span class=\"hljs-attr\">permissions<\/span>:\n      pull-requests: read\n    <span class=\"hljs-attr\">outputs<\/span>:\n      app: ${{ steps.filter.outputs.app }}\n    <span class=\"hljs-attr\">steps<\/span>:\n      - uses: dorny\/paths-filter@v3\n        <span class=\"hljs-attr\">id<\/span>: filter\n        <span class=\"hljs-attr\">with<\/span>:\n          filters: |\n            app:\n              - <span class=\"hljs-string\">'src\/**'<\/span>\n              - <span class=\"hljs-string\">'scripts\/**'<\/span>\n              - <span class=\"hljs-string\">'package*.json'<\/span>\n\n  <span class=\"hljs-attr\">build<\/span>:\n    needs: changes\n    <span class=\"hljs-attr\">if<\/span>: needs.changes.outputs.app == <span class=\"hljs-string\">'true'<\/span>\n    runs-on: ubuntu-latest\n    <span class=\"hljs-attr\">steps<\/span>:\n      - uses: actions\/checkout@v5\n      - uses: actions\/setup-node@v5\n        <span class=\"hljs-attr\">with<\/span>:\n          node-version: <span class=\"hljs-string\">'22'<\/span>\n          <span class=\"hljs-attr\">cache<\/span>: <span class=\"hljs-string\">'npm'<\/span>\n      - run: npm ci\n      - run: npm run build<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In a monorepo, the same approach scales: define one filter per service (for example <code>api<\/code> and <code>web<\/code>) and attach a separate job to each output.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Best practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Include the workflow file and dependency manifests in <code>paths<\/code>, or pipeline and dependency changes will go untested.<\/li>\n\n\n<li>Avoid path filters on security scans and release workflows that must always run.<\/li>\n\n\n<li>Keep the <code>push<\/code> and <code>pull_request<\/code> filters identical so the two triggers don&#039;t drift apart.<\/li>\n\n\n<li>Add <code>workflow_dispatch:<\/code> so you can run a filtered workflow manually when needed.<\/li>\n\n\n<li>Set a minimal <code>permissions:<\/code> block on each workflow.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For a broader introduction to triggers, jobs and runners, see <a href=\"https:\/\/www.devopsschool.com\/blog\/github-actions-essentials-learn-ci-cd-in-2-hours\/\">GitHub Actions Essentials \u2014 Learn CI\/CD in 2 Hours<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently asked questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Can I use paths and paths-ignore together in GitHub Actions?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Not on the same event. GitHub rejects the workflow file. Use <code>paths<\/code> with <code>!<\/code> negation patterns instead, or use <code>paths<\/code> on one event and <code>paths-ignore<\/code> on another.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why does my pull request wait forever for a required check when the workflow was skipped?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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 <code>if:<\/code> conditions. Skipped jobs satisfy required checks.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Do path filters work for workflow_dispatch or scheduled runs?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. <code>paths<\/code> and <code>paths-ignore<\/code> apply to <code>push<\/code>, <code>pull_request<\/code> and <code>pull_request_target<\/code>. Manual and scheduled runs always start.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is the difference between src\/* and src\/** in a path filter?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>src\/*<\/code> matches only files directly inside <code>src\/<\/code>. <code>src\/**<\/code> matches all files at any depth below <code>src\/<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does the order of patterns in paths matter?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Patterns are evaluated in order, so a <code>!<\/code> exclusion only takes effect if it comes after the positive pattern it should override.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related tutorials<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/github-actions-essentials-learn-ci-cd-in-2-hours\/\">GitHub Actions Essentials \u2014 Learn CI\/CD in 2 Hours<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/github-actions-ci-cd-automation-devops-engineering\/\">GitHub Actions \u2014 CI\/CD Automation &amp; DevOps Engineering<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/github-repository-settings-essentials-2-hour-tutorial\/\">GitHub Repository Settings Essentials \u2014 2-Hour Tutorial<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/category\/devops\/github\/\">More GitHub tutorials<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5204],"tags":[26194],"class_list":["post-47161","post","type-post","status-publish","format-standard","hentry","category-github","tag-github-actions"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/47161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=47161"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/47161\/revisions"}],"predecessor-version":[{"id":80188,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/47161\/revisions\/80188"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=47161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=47161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=47161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}