Boost GitHub Actions Efficiency with Path Filters: Only run when specific files or paths are updated

Within your GitHub Actions, you may be running a lot of workflows continuously during a merge to the main branch – a way to mitigate the possibility of them always running is to implement path filters.

What are path filters?

Path filters in GitHub Actions allow you specify when the workflow/action runs essentially, it helps you control when your actions run based on the location of the files or folders that have been modified. This means you can set up your workflows to only execute when changes are made to specific directories or files, which can be really useful for keeping your automation workloads efficient.

For example, if you have a GitHub repository separate for your code, documentation and testing, you probably want different actions to run depending on which folder or file(s) have been updated. Using path filtering, you can do this easily, making sure the specific action(s) are only triggered when relevant changes occur – resulting in being more efficient and time saving.

Example of using path filtering

Below shows on push to main:

  • When there is a push event to the main branch, but only if the changes involve files within the example-mkdocs directory or any of its subdirectories.
    • This can also be on specific file changes such as 'example-mkdocs/mkdocs.yml'
  • When manually triggered by a user through the GitHub Actions interface (workflow_dispatch).
name: Build GitHub Pages
on:
  push:
    branches:
      - main
    paths:
      - 'example-mkdocs/**'
  workflow_dispatch:

Its very simple, but so useful!

  • This example change did not trigger any actions to run when merged to main

https://github.com/thomast1906/mkdocs-github-pages-deploy-github-action/pull/5

  • Making a change to a file within example-mkdocs/** folder(s)

https://github.com/thomast1906/mkdocs-github-pages-deploy-github-action/pull/6

Has triggered the relevant workflow as a change was made that matches the path above: https://github.com/thomast1906/mkdocs-github-pages-deploy-github-action/actions/runs/9059726318

Finishing up

That was only a quick example of how path filtering can assist you within your GitHub Actions – I highly recommend checking out this cheat sheet from GitHub on additional filter patterns to use

By specifying which files or directories trigger actions, you can ensure that your pipelines are triggered only when needed, speeding up the whole automation process!

Leave a Reply

Discover more from Thomas Thornton Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading

Discover more from Thomas Thornton Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading