Search code examples
continuous-integrationyamlgitlab-cigitlab-ci-runner

Gitlab CI run when commit message matches the regex


I am trying to only trigger the pipeline when commit message has the conditional phrase. I know this has been asked a lot of times and there are helpful answers available. I have also checked gitlab ci documentation and it also provide the right ways to do it.

Still the stage is built no matter the required phrase is in commit message or not. Here is the .yml code.

before_script:
  - export LC_ALL=en_US.UTF-8
  - export LANG=en_US.UTF-8
  - export BUILD_TIME=$(date '+%Y-%m-%d %H:%M:%S')
  - echo $branch
  
stages:
  - build
  
build_job:
  stage: build
  only:
    variables:
      - $branch
      - $CI_COMMIT_MESSAGE =~ /\[ci build]/
  script:
    - bundle fastlane
    - fastlane build

Anyone have any idea that what is wrong with it?


Solution

  • Maybe you can remove the variable $branch and use only: refs here some example

    before_script:
      - export LC_ALL=en_US.UTF-8
      - export LANG=en_US.UTF-8
      - export BUILD_TIME=$(date '+%Y-%m-%d %H:%M:%S')
      
    stages:
      - build
      
    build_job:
      stage: build
    
      script:
        - bundle fastlane
        - fastlane build
      only:
        variables:
          - $CI_COMMIT_MESSAGE =~ /\[ci build]/
        refs:
          - /^develop*.*$/
    

    you can use regex in refs , in my example meaning : when branch name contain develop and commit message contain [ci build] then run the stages

    you can modify thats regex.

    thats method used in my production.