Search code examples
gitgitlabgitlab-ci

GitLab CI Pipeline Incorrectly Triggers on All Branches Despite Specific Workflow Rules


I've set up a GitLab CI configuration intended to trigger pipelines only on the develop and main branches when the commit message starts with "deploy" and the event is a push. Despite this, the pipeline is unexpectedly executing on push events to any branch, including ones not specified in the configuration, such as feature/partially_fill. Here's my current .gitlab-ci.yml setup:

workflow:
  rules:
    - if: '$CI_COMMIT_BRANCH == "develop" && $CI_COMMIT_MESSAGE =~ /^deploy.*/ && $CI_PIPELINE_SOURCE == "push"'
    - if: '$CI_COMMIT_BRANCH == "main" && $CI_COMMIT_MESSAGE =~ /^deploy.*/ && $CI_PIPELINE_SOURCE == "push"'
    
include:
  - local: '/.gitlab-ci-develop.yml'
    rules:
      - if: '$CI_COMMIT_BRANCH == "develop"'
        when: always

  - local: '/.gitlab-ci-main.yml'
    rules:
      - if: '$CI_COMMIT_BRANCH == "main"'
        when: always

The intention is clear: pipelines should only run on develop and main branches under specific conditions. However, it's not behaving as expected.

Questions:

  1. Why is the GitLab CI pipeline being triggered on branches other than develop and main, despite the explicit conditions set in the workflow rules?
  2. Is there a mistake in my configuration that's causing the CI pipeline to ignore the specified branch conditions?
  3. How can I adjust my GitLab CI configuration to ensure that pipelines only run on develop and main branches when the commit message starts with "deploy" and the source is a push event?

Any insights or suggestions to correct this behavior would be greatly appreciated. Thank you in advance for your help!


Solution

  • The problem was with the using the Include function don't know why but it was fixed using the following CI:

    stages:
      - trigger-services
    
    workflow:
      rules:
        - if: $CI_COMMIT_BRANCH == "develop" && $CI_COMMIT_MESSAGE =~ /^deploy.*/
        - if: $CI_COMMIT_BRANCH == "main" && $CI_COMMIT_MESSAGE =~ /^deploy.*/
    
    develop:
      stage: trigger-services
      rules:
        - if: $CI_COMMIT_BRANCH == "develop"
      trigger:
        include: '/.gitlab-ci-develop.yml'
        strategy: depend
    
    production:
      stage: trigger-services
      rules:
        - if: $CI_COMMIT_BRANCH == "main"
      trigger:
        include: '/.gitlab-ci-production.yml'
        strategy: depend