How to start a CI pipe in main after a merged PR?

I already have a CI pipe that runs on the feature branch when a PR is opened, reopened and synced.
Now that run the same pipe in main when PR is merged.
How do I configure a trigger for this?

Hello @dmarinss and welcome to the Codefresh community.

It depends on whether you want to run the pipeline on the branch where the pull request was opened, or the branch where the pull request was merged to.

Also if you are trying to replicate Trunk based development with Codefresh there is a full guide here Pull Requests and Branches · Codefresh | Docs

Hi Kotis. Thanks for the return.

I’m running github flow.

I want to run the CI on the target (main) branch, which the pull request was merged into.

I configured my trigger like this:

I tried using this step, but it didn’t work for me:

Actually, writing this, I think the trigger is already configured correctly, because when merging the PR, the pipe is started.

The real problem is getting the clone step to understand that it needs to checkout the feature branch first and then main when the PR is merged.

Hey @dmarinss

If you use CF_BRANCH as explained in Variables · Codefresh | Docs then it should work like you expect it.

Can you post your clone step here?

Hi Kotis. Apologies for the delay.

I managed to solve it using the “CF_REVISION” variable instead of the “CF_BRANCH” variable in the “Revision” property of the clone step.

I also removed the previous step that updated the “CF_BRANCH” variable.

So my step was like this:

repo_clone:
     stage: setup
     title: 'Clonning repository'
     type: git-clone
     repo: '${{CF_REPO_OWNER}}/${{CF_REPO_NAME}}'
     git: github
     revision: '${{CF_REVISION}}'

yarn_install: ...
lint: ...
unit_tests: ...
build"...
call_cd_pipeline: ...

With the “CF_BRANCH” variable, the checkout was performed by looking at the branch that had already been deleted (by policy) after the merge.

Great. I am glad that you found a solution!

Hello Kostis. Hope you’re well.
I was completely wrong, I couldn’t solve the last way I reported in the thread. So I reread the article you gave me a few times (Pull Requests and Branches · Codefresh | Docs) and followed the instructions. Explaining better:

  • I have 2 pipelines configured, CI and CD.
  • The CI executes in 2 moments, before the change enters main and when the change enters main (at that moment, the CD will be started as a child of the CI).
  • The CI has 2 triggers, one configured to start the pipe when a PR is opened, reopened and synchronized and when the PR’s target extension is main or master (in my flow, it will always be). The other trigger is set when the PR is merged, with the “push commit” event and when the target branch is main or master, that is, modified code has been inserted into main.

This already works and suits my flow perfectly, however I have a side effect on my CD.

On the CD there is a step that versions the project, generates changelog, generates git tag and commits. Then another step pushes commtis into the main. Once this is done, a new CI is started, as the event configured in one of the triggers is “push commits”. (remembering that the repository already has locks for commits in main, only the codefresh user can do this). I already tried the merged PR event and my CI didn’t run with it.

I already put a condition that only starts the first step of CI when there is no “skip ci” in the commit message (the step that versiona uses a message with this). But this prevents me from manually starting the CI (imagine I’m validating other steps in the pipe).

Finally the question… How can I have the same behavior of initialization of the pipes, without starting a CI (unexpected) after confirming my version increase on the CD and not getting stuck when starting the CI manually?

My triggers (terraform):

    trigger {
      branch_regex = "/.*/gi"
      pull_request_target_branch_regex = "/^((main|master)$).*/g"
      context      = "github"
      description  = "Run CI pipeline on feature branch"
      disabled     = false
      events = [
        "pullrequest.opened",
        "pullrequest.reopened",
        "pullrequest.synchronize",
      ]
      modified_files_glob = ""
      name                = "ci_feature_branch/${var.project_name}"
      provider            = "github"
      repo                = "${var.project_name}"
      type                = "git"
    }
    trigger {
      branch_regex                     = "/^((main|master)$).*/g"
      context                          = "github"
      description                      = "Run CI pipeline on main/master branch"
      disabled                         = false
      events = [
        "push.heads",
      ]
      modified_files_glob = ""
      name                = "ci_main_branch/${var.project_name}"
      provider            = "github"
      repo                = "${var.project_name}"
      type                = "git"
    }

Hello

This is very hard to answer without looking at the pipelines (and not just the triggers).

Please open a ticket with us and be sure to include the URLs of some example builds so that our support team can dive deeper