gittriggersgitlabcontinuous-integrationdevops

Pushing new branch GitLab


I have GitlabCI like this.

STAGE:
  stage: deploy
  image:
    name: bitnami/kubectl:1.26
    entrypoint: [""]
  script:
     ...
  rules:
    - changes:
      - .gitlab-ci.yml
    when: never
    - if: $CI_COMMIT_REF_NAME =~ /^release\/\d+\.\d+\.\d+$/

This job is for branches like release/x.y.z The thing is that the CI is not triggered when I push the branch for the first time. As I saw from the GitLab logs it compares the last commit of the branch with the null commit (0000000000000000000000000000000000000000) and therefore it is not triggered. But it would be nice if it was triggered for first time the branch is pushed. What is the best practice in this situation?

I expect the CI to be triggered when I push the branch for the first time.


Solution

  • Long time passed since the question I have posted here. I solved it back then by adding the configuration in rules section below

    rules:
        - if: $CI_COMMIT_REF_NAME =~ /^release\/\d+\.\d+\.\d+$/ && $CI_COMMIT_BEFORE_SHA == "0000000000000000000000000000000000000000"
          when: always
        - if: $CI_COMMIT_REF_NAME =~ /^release\/\d+\.\d+\.\d+$/
          when: always
    

    Now the CI will be triggered when new branch is pushed.
    This two ifs can be combined.

    rules:
        - if: $CI_COMMIT_REF_NAME =~ /^release\/\d+\.\d+\.\d+$/ && $CI_COMMIT_BEFORE_SHA == "0000000000000000000000000000000000000000" || $CI_COMMIT_REF_NAME =~ /^release\/\d+\.\d+\.\d+$/
          when: always
    

    This should resolve the problem.

    BTW I could find a video in youtube describing my exact issue.
    Here is the link --> https://www.youtube.com/watch?v=77Q0xykWzOI&ab_channel=vlogize