Trigger a GitHub Actions workflow when a folder changes
Recently I've been working with GitHub and creating a lot of workflows. I have a couple of GitHub repositories that store both software code and infrastructure as code.
Every time I made a change to the software code, it would trigger the infrastructure part as well, and I was not too fond of the fact that my infrastructure was re-deploying every time I made a change to the software.
I found a solution to my problem - GitHub Actions triggers!
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.
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:
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. 😉