How cf_branch is working?

Hi all,

I started to have a problem with pull request trigger. I guess there was some change in the end of 2022/beginning 2023 with regards to that.

Originally, I had trigger Pull Request Merged trigger with Pull Request Target brancg set to “/^main$/gi” - so pull requests to main branch triggered build. In this case, CF_BRANCH was set to main. Behavior is different now - CF_BRANCH is set to originator branch. I followed this article to change CF_BRANCH to CF_PULL_REQUEST_TARGET for pull request builds (since article was created on January - I assumed that there was some change in December 2022/January 2023).

Idea there is to cf_export CF_BRANCH variable and set it to CF_PULL_REQUEST_TARGET. As per doc - cf_export must override pipeline level variables:
The variables exported with cf_export overrides those at the pipeline-level.

However, it looks like CF_BRANCH is modified to exported value only at command level of step, i.e.:

    type: freestyle
      image: 'alpine:3.8'
        - echo $CF_EXPORT

But at step level - CF_BRANCH is still not changed (originator of PR request), i.e.:

    title: "Cloning repository"
    type: "git-clone"
    repo: "repo/name"
    revision:  ${{CF_BRANCH}}


I ended up following this article (remove underscore in front of support):

which suggests to cf_export new variable name with targer branch name for PR requests and using this new variable name in all clone and etc. steps.

Have anyone encountered this or I am missing something? I have a feeling that doc reference - overrides those at the pipeline-level - is explicitly means pipiline variables and is not impacting trigger variables (what CF_BRANCH is).

In addition, all branch conditions are also tied originator of PR request meaning branch condition like this will return false for PR request to main branch build:

          - main

But I guess this might be expected.

Thank in advance.

Hi there!
I’ve checked this internally and it seems there were no changes on our side in CF_BRANCH behavior for years. I can assume that something has changed in incoming payload from your git provider, but I need more details in order to check this suspicion.

At the same time, I want to apologies for misleading articles “How-to: Set branch for build depending on whether it’s a push or PR event” and “How-to: Clone target instead of source branch on pull request”.
The approach described there (to rewrite variables via cf_export) really work but requires specific feature flag, which should be enabled per-customer on our side. An ability to use variables defined in runtime in step definitions (as arguments for example) — is a pretty new option, so it is hidden by feature flag for backward compatibility reasons.

I wanted to thank you for bringing our attention to this: we will update these articles shortly to avoid further confusion.

Meantime, please open support ticket here so that we’re able to enable this feature for you: I believe it should efficiently solve your issue. Once enabled, you will be able to reexport CF_BRANCH via cf_export and then use it as clone revision or wherever else at your wish.

Please let us know if there anything else we can help with on this matter!

Codefresh Support Team