Don't run GitHub Actions workflow if a specific set of files are changed

Viewed 608

I have two workflows, wA and wB, corresponding to builds of two Docker images A and B. Image B has a base of image A, and thus has a direct dependency. wB is triggered by any changes in B using a paths filter, but is also triggered by the execution of wA through a workflow_run trigger—we want to rebuild B when its base image changes.

If a push changes files in both A and B, we run into the situation where both wA and wB are immediately queued, then wB is run again after the conclusion of wA. This isn't necessarily a dealbreaker, but that first run of wB is unnecessary since we know that it will be triggered on the conclusion of wA and will simply be superseded, and this can pose an issue for our longer-running builds. (I suppose that such a dependency would fail to trigger wB on the failure of wA when it really still could be built, but I consider that perfectly fine.)

If a push only changes files in B, we still want wB to run.

Does there exist a way to run a workflow only if a specific path filter does not match, e.g. "run wB only if (but not if and only if) there are no changes in A"? It doesn't seem like setting up a negative filter on paths is adequate here, as GitHub's documentation notes:

When all the path names match patterns in paths-ignore, the workflow will not run. If any path names do not match patterns in paths-ignore, even if some path names match the patterns, the workflow will run.

As such, ignoring changes in A in wB's paths filter wouldn't have any effect if changes exist in B.

If not, might there be a better way to encode this dependency?

1 Answers

I would suggest to use this kind of logic

on:
  push:
    paths:
      - 'templates/**'
      - 'package.json'
      - 'FileB'

But instead of paths you can use paths-ignore.

Plus add some concurrency grouping:

concurrency:
  group: ${{ github.ref }}_your_run_name
  cancel-in-progress: true
Related