ASHISH M.G
ASHISH M.G

Reputation: 802

Github actions: Run step / job in a workflow if changes happen in specific folder

Is there any way for us to control what jobs/steps to run in a workflow based on the changes in a specific folder

Eg:

I have said, following 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

Upvotes: 49

Views: 50715

Answers (2)

GuiFalourd
GuiFalourd

Reputation: 23270

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.

Here is an example

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.

Upvotes: 73

Criminal_Affair_At_SO
Criminal_Affair_At_SO

Reputation: 3443

Yes: https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows#registry_package

This is the syntax:

on:
  push:
    paths:
      - 'a/**'
      - 'b/**'

Upvotes: 11

Related Questions