Variables Expansion with user defined variable in include:local:rules:if

Gitlab ci Variable Expansion in include:local:rules:if

I can’t get the job to trigger when using defined variable, CI variables seems to work fine

<.gitlab-ci.yml>

variables:
  BASE_URL: $CI_REGISTRY/$CI_PROJECT_NAMESPACE/$CI_PROJECT_NAME
  BASE_TAG: base
  DOCKER_TLS_CERTDIR: /etc/docker/certs.d
  PROD_BRANCH: main
  DOCKER_FILE: Dockerfile.builder
  IMAGE_TAG: builder

stages:
  - build_base
  - build_docker_image
# -test

build_base:
  image: debian:bullseye
  stage: build_base
  before_script:
    - apt-get update && apt-get install -y docker.io
  rules:
    - if: $CI_COMMIT_BRANCH == $PROD_BRANCH
  script:
    - echo $CI_JOB_TOKEN | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY/$CI_PROJECT_NAMESPACE/$CI_PROJECT_NAME
    - docker build -t "$BASE_URL:$BASE_TAG" . -f Dockerfile.base
    - docker push "$BASE_URL:$BASE_TAG"

include:
  - local: '/ci-utils/build-docker-image.yml'
    rules:
      - if: $CI_COMMIT_BRANCH == $PROD_BRANCH

</ci-utils/build-docker-image.yml>

build_docker_image:
  image: registry.gitlab.fpc7063.com.br/testing-ground/ci-utils:base
  stage: build_docker_image
  before_script:
    - apt-get update && apt-get install -y docker.io
  script:
    - echo $CI_JOB_TOKEN | docker login -u $CI_REGISTRY_USER --password-stdin $BASE_URL
    - docker build -t "$BASE_URL:$IMAGE_TAG" . -f $DOCKER_FILE
    - docker push "$BASE_URL:$BUILDER_TAG"

When changing my include to the job is triggered

include:
  - local: '/ci-utils/build-docker-image.yml'
    rules:
      - if: $CI_COMMIT_BRANCH == 'main'

Is this a known limitation or a bug? CI variable $CI_COMMIT_BRANCH it’s working fine, if it wasn’t it wouldn’t trigger the pipeline, but $PROD_BRANCH doesn’t seem to get expanded.
I could use at this moment $CI_DEFAULT_BRANCH like in the docs but i’m eventually going to stumble on this again.

Also, ci yaml parser doesn’t seem to think of this as an error:

Additional Info:
Gitlab Type: SelfHosted
Gitlab Version: v16.0.1-ee
Runner Version: 16.0.1
Runner Revision: 79704081
OS: Debian bullseye

EDIT 1: typo

I do not think the parser is that clever. PROD_BRANCH maybe evaluates to an empty or unset string so you get a Boolean false.
Looking at Use CI/CD configuration from other files | GitLab variables defined in the YAML file are ignored. But maybe you could set PROD_BRANCH in the project variable.

1 Like

Setting the environment variable from the CI/CD Variables on Settings does work in this case, thanks!