Trigger a GitHub Actions workflow when a folder changes

Step into my world of GitHub wizardry, where every line of code counts and every workflow tweak matters.

Picture this: I'm knee-deep in GitHub repositories, juggling software code and infrastructure as code with finesse.

But there's a hitch—I noticed that each software tweak triggered an unwanted infrastructure overhaul.

Frustration set in, until I stumbled upon a game-changing solution: GitHub Actions triggers!

Join me as I unravel the magic of GitHub Actions and unveil how to wield its power to trigger workflows precisely when folders change.

What is an GitHub Actions workflow?

Within GitHub, a GitHub Actions workflow is an automated process that can be set up within your repositories. A GitHub Actions workflow can automate tasks and jobs for you. It can help you build your Continuous Integration/Continuous Delivery (CI/CD) pipelines.

GitHub Actions triggers

When you create a GitHub Action, you can specify when it runs, this is called a trigger.

You can identify triggers, such as when new code is pushed to the repository or when a pull request is actioned, the workflow will spin into action.

Usually, these triggers are set to kick off when a change happens to a specific branch. But you can take that down another level. You can specify if a change occurs to a particular directory or path within a branch, and the workflow will kick start.

In this example below, you can see the start of a workflow where the workflow will be triggered when changes are pushed into the folder "MyFirstFolder" within the central repository.


name: Workflow example

on:

  push:
    branches:
    - main
    paths:
    - MyFirstFolder
    

jobs:

  build:
 

If changes are made out with the "MyFirstFolder", the workflow won't be triggered.

What this means is you can have workflows for different purposes within your repository. As I mentioned earlier means, I can store my software code and infrastructure as code within a GitHub repository, with different workflows for each only being triggered when I make a change to that specific code.

Wildcard Triggers

But you can also be less specific than using a folder name. Below is a small example where you specify a file type, this time an Azure Bicep file, to be the trigger for the workflow.

name: Workflow example

on:

  push:
    paths:
    - '**.bicep'
    

jobs:

  build:

Excluding paths

You could do it another way by excluding folders. In the example below on, push events that include at least one file outside the MyFirstFolder directory.

name: Workflow example

on:

  push:
    paths-ignore:
      - 'MyFirstFolder/**'
    

jobs:

  build:

There is a lot of flexibility with GitHub Action workflows and making them trigger. So try it out, experiment and make workflows work for you. 😉