Is there any way for us to control what jobs/steps to run in a workflow based on the changes in a specific folder
For example:
I have three folders in my git repo: a, b, c
On every PR merge to my branch I will trigger a workflow. The workflow will execute jobs, say, A -> B -> C. I want to run job A only if changes are present for folder "a/**", B for "b/**" and so on.
So if in the PR changes only happen in "a/**"and "b/**" workflow will skip job execution for C, making the workflow run to be A->B
You could use the paths-filter custom action with if conditions at the jobs or step levels, using a setup job as preliminary to check if your specific path has been updated, saving the result as an output.
name: Paths Filter Example
on: [push, workflow_dispatch]
jobs:
paths-filter:
runs-on: ubuntu-latest
outputs:
output1: ${{ steps.filter.outputs.workflows }}
steps:
- uses: actions/checkout@v4
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
workflows:
- '.github/workflows/**'
# run only if 'workflows' files were changed
- name: workflow tests
if: steps.filter.outputs.workflows == 'true'
run: echo "Workflow file"
# run only if not 'workflows' files were changed
- name: not workflow tests
if: steps.filter.outputs.workflows != 'true'
run: echo "NOT workflow file"
next-job:
runs-on: ubuntu-latest
# Wait from the paths-filter to be completed before starting next-job
needs: paths-filter
if: needs.paths-filter.outputs.output1 == 'true'
steps:
...
That way, you could have something like this in your jobs: A --> B or A --> C depending on the path that has been updated.