Path-filtering to reduce build cost in branches

I’ve started looking into the path-filtering orb. We have a mono repository that also has the documentation build and publish pipeline in it. I tried to build a path filter controlling the docs and source code workflows more granular.

The logic I came up with is having two parameters one as a workflow guard for building the source code and another one for just building the docs. If we have only changes in the docs folder we can run now a very optimized and cheap workflow and as soon we have changes somewhere else we run the more expensive workflow for building and testing the code.

The path filtering configuration looks like this here:

- path-filtering/filter:
    base-revision: feature/path-filter
    config-path: .circleci/config.yml
    mapping: |
      ^((?!docs/).)*$ trigger-src true
      docs.* trigger-docs true

When I run it in my branch given with base-revision it works as expected. If I change anything outside of the docs folder trigger-src=true if have changes in the docs folder trigger-docs=true. The parameter variables are initialized as false.

My question is now, how can I get this working for any branch someone creates in our repository? As far I’ve understood, I have to specifically use the base-revision parameter. If it’s not set it defaults to the branch name main.

The docs for this orb explains base-revision as:

The revision to compare the current one against for the purpose of determining changed files.

So it seems like as soon someone makes a branch, the person has to update the config.yml and needs to set the base-revision for his branch manually? Ideally, it would be nice if the behavior works in any newly created branch without touching the config.yml file.

Any ideas or hints are welcome and thank you in advance.

Hi @indigo423,

It should be possible to do something like this:

- path-filtering/filter:
    base-revision: << pipeline.git.branch >>
    config-path: .circleci/config.yml
    mapping: |
      ^((?!docs/).)*$ trigger-src true
      docs/.* trigger-docs true

https://circleci.com/docs/2.0/pipeline-variables/#pipeline-values

The pipeline variable pipeline.git.branch will contain the value of the branch name that we get from the webhook payload Github sends us.

Could you try the above, and let me know if it works for your use case?

Best Regards

1 Like

@aaronclark that was the missing piece. It works as expected and you clearly made my day :slight_smile:

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.