"Correct" way to pass the branch name to orbs

I’m at my wits end. All I want to do is have a couple of commands in an orb tweak their behaviour when on certain branches. Yet it seems to be impossible to pass the branch name down to it without resorting to having a dynamic config shim it in.

I’ve tried the following:

  • $CIRCLE-BRANCH is a no-go since it’s using when. I don’t believe scripting around it is an option either since I’m trying to conditionally call a built-in step (save/restore cache).
  • <<pipeline.git.branch>> can’t be used directly since it’s not available in orbs.
  • Passing <<pipeline.git.branch>> from the workflow to the job as a parameter works when a branch is pushed, but causes tag pushes to error out since that parameter is not defined there:
Error calling workflow: 'my-workflow'Error calling job: 'my-job'Unknown variable(s): branch

(branch filtering at the workflow level isn’t really an option either as it results in an unmaintainable level of duplication)

If there’s really no other way to get the branch name in there, can I at least prevent tag pushes from showing up at all? Maybe by tweaking the GitHub webhook?

Hi @qcloutier-sonrai,

Thank you for sharing your question on our forum.

As you have mentioned, it is not possible to directly reference pipeline parameters in the source of an orb.

One way to make this work would be passing the pipeline parameter from the workflow to a job as you have mentioned. Knowing that pipeline.git.branch will be blank for tag pushes, and pipeline.git.tag will be blank for branches, you can string them together like this:

workflows:
  workflow:
    jobs:
      - build:
          ref: << pipeline.git.branch >><< pipeline.git.tag >>

If you do not wish to use the tag as a “branch name”, you can also specify a default value to use when pipeline.git.branch is blank. The syntax uses mustache conditionals:

workflows:
  workflow:
    jobs:
      - build:
          ref: << pipeline.git.branch >><<^ pipeline.git.branch >>defaultbranch<</ pipeline.git.branch >>

There is another example of how to use this syntax in the following article:

https://support.circleci.com/hc/en-us/articles/4417604103835-Using-mustache-conditionals-in-config-file

Could you try this out on your end, and let me know if it works for your use-case?

That works great, thank you!

It would be nice if the fact that we can use mustache conditionals was better documented… I could only find one brief mention of it after the example here.