github-actions

GIthub action : Trigger a job only when there is a change in particular directory


I have created a deployment.yaml, which builds my code every time I make a push to the master branch.

This is my project structure.

project1
|-subproject1
|-subproject2
|-subproject3

Now, I have 3 different jobs that builds each of this subprojects.

can I add a condition in such a way.. that if I make changes only in subproject2, then only that particular job should run.

I found that the jobs have an if property.

https://docs.github.com/en/actions/using-jobs/using-conditions-to-control-job-execution

So, in short I want to write something like ...

jobs:
  deploy:
    if: old_subproject1 != new_subproject1

...
jobs:
  deploy:
    if: old_subproject2 != new_subproject2
...
jobs:
  deploy:
    if: old_subproject3 != new_subproject3

Thanks in advance !


Solution

  • You can do that by controlling the workflow's triggering with path filter:

    on:
      push:
        branches:
          - 'master'
        paths:
          - 'project1/subproject1/**/*'
    

    See: https://docs.github.com/en/actions/using-workflows/triggering-a-workflow#using-filters-to-target-specific-paths-for-pull-request-or-push-events

    If you need a different path configuration for each job, I think that the best solution would be to create one workflow per job. It seems that you can't detect the changes with the conditional statement at the job level.