Pipeline stage getting skipped during merge request

I am running into an issue where my pipeline is skipping the stage where it generates a build number. This only happens during a merge request. This causes the pipeline to fail later on because it is used to tag the build in the container registry. The pipeline runs fine on the source branch and if I force the merge to the main branch and run it there, the init stage runs fine and generates the build number. It only fails when triggered by a merge request.

I have broken the GitLab stage into separate files, and there are some rule, but there are zero rules on the generate-build-number step, so I believe it should run every build.

Any ideas what could be causing this?

init stage code

generate-build-number:
  stage: init
  script:
    - echo "BUILD_TIME=$(date +%Y.%m.%d.%H%M%S)" >> build.env
  artifacts:
    reports:
      dotenv: build.env

gitlab.ci file

include: 
  - '/.gitlab-ci/init.yml'
  - '/.gitlab-ci/analyze.yml'
  - '/.gitlab-ci/build.yml'
  - '/.gitlab-ci/deploy.yml'
  - '/.gitlab-ci/push.yml'

services:
  - docker:dind

stages:
  - init
  - analyze
  - build
  - push
  - deploy

Edit:
It seems to be caused by rules in the other stages, for example, I have rules like this:

build-api:
  image: mcr.microsoft.com/dotnet/sdk:6.0
  stage: build
  script:
    - dotnet build "Api/Api.csproj" -c Release -o $CI_PROJECT_DIR/build/Api
    - dotnet publish "Api/Api.csproj" -c Release -o $CI_PROJECT_DIR/publish/Api /p:UseAppHost=false
  rules:
    - changes:
      - Api/**/*
      - Core/**/*
      - Database/**/*
  artifacts:
    paths:
      - $CI_PROJECT_DIR/publish/Api
  dependencies:
    - analysis

push-api:
  image: docker:latest
  stage: push
  script:
    - docker login -u user -p $CI_JOB_TOKEN https://registry.gitlab.com/
    - docker build -t registry.gitlab.com/api:$BUILD_TIME . -f Api/Dockerfile.cicd
    - docker tag registry.gitlab.com/api:$BUILD_TIME registry.gitlab.com/api:stage
    - docker push registry.gitlab.com/api:$BUILD_TIME
    - docker push registry.gitlab.com/api:stage
  rules:
    - changes:
      - Api/**/*
      - Core/**/*
  dependencies:
    - build-api
    - generate-build-number

This seems to be causing my init stage to not get triggered along side those jobs. I thought rules were used to exclude/include, but if no rules were present, they would be run on every pipeline. If I add this “rule” to my generate-build-number job, it works, but it creates two pipelines, one for the commit and one for the merge request. Plus, I am not too keen on having such a weird work around. Am I missing something about how rules are supposed to work? I used this setup in GitLab on-prem, and this did not happen until we moved to the cloud. Forgot to mention that eariler.

rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event" || $CI_PIPELINE_SOURCE != "merge_request_event"
1 Like