Why won't this job wait?

Hi everyone

I have setup GitLab Flow to have 3 protected branches: main, test and production. Devs will commit code via feature branches created in issues, which will get merged to main, then main gets merged to test, and eventually test will be merged to production.

I have a rule like this:

- if: $CI_PIPELINE_SOURCE == 'merge_request_event' && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "test" && $CI_COMMIT_MESSAGE =~ '/See merge request/'

My hope was for this job to only execute under the following condition:

  • If there is a commit made via a merge request to test branch, or if a hotfix patch was made to production branch and then merged back to test (and eventually main) via cherry picking AND the code is already merged by the maintaining. Not only approved, but also merged.

Currently this code kicks runs without waiting for the merge to be complete. How can I can ensure this job only runs after it has been merged to test branch.

Thank you for your help and taking the time to read my issue.

Hey,

If you want your job to be executed after merge to test branch, then you need to add:

- if: $CI_COMMIT_BRANCH == 'test'

above or under your existing rule.

Hope this helps!

Thanks @paula.kokic . But would this not create a duplicate pipeline? Seeing as my other rules are for Merge Request pipelines? I will try this in any ways, thanks!

Well, depends on how the rest of your config looks like xD - according to that one line you’ve provided I’d say no bcos that other rule is just for MRs targeting test branch.

But in case that’s still somehow happening bcos of your main workflow config, there is an guide on docs how to avoid that.

Thanks for the response. This is my complete pipeline:

stages:
  - dryrun
  - test-topology
  - apply-topology

#### TEST

dry-run-julieops-on-kafka-tst:
  stage: dryrun
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event' && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "test"

    
  script:
    - echo "DryRun from JO on kafka-tst"

current-commit-against-test-topology:
  stage: test-topology
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event' && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "test" 

  script:
    - echo "Run current commit on docker instance with test topology"

apply-julieops-on-kafka-tst:
  stage: apply-topology
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event' && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "test" && $CI_COMMIT_MESSAGE =~ '/See merge request/'
  script:
    - echo "Run current commit on kafka-tst environment"


#### PRODUCTION

dry-run-julieops-on-kafka-prd:
  stage: dryrun
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event' && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "production"
    - when: never
  script:
    - echo "DryRun from JO on kafka-prd"
    
current-commit-against-prod-topology:
  stage: test-topology
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event' && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "production"
    - when: never
  script:
    - echo "Run current commit on docker instance with prod topology"

apply-julieops-on-kafka-prd:
  stage: apply-topology
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event' && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "production" && $CI_COMMIT_MESSAGE =~ '/See merge request/'
    - when: never
  script:
    - echo "Run current commit on kafka-prd environment"

Hi,

I stand still behind my previous answer. Currently all your jobs run on MRs, not on branches, so there is no duplication. And with my line added, there shouldn’t be, unless you are creating an MR test → production (then this branch pipeline for test will kick in, and the MR for production branch). But you can dodge this by adding:

- if: '$CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS'
  when: never

to the rules for relevant jobs.

Now, I might have misunderstood you - if you want to run only AFTER merge, then this is a standard branch pipeline for your target branch (in this case test, so for that job you should only specify the line I wrote in my previous answer). AFAIK, you cannot really be more specific about that, i.e. branch pipeline does not know from where it was merged or how, etc… perhaps you could “hack” it a bit by reading the merge commit message and add it to the rule additionally… And GitLab does not have a post-merge event (which is what you would probably want).

Maybe check out existing variables and see if you find anything useful for specifying the branch pipeline rule: Predefined CI/CD variables reference | GitLab