In GitLab CI, I'd like to use CI_COMMIT_REF_NAME
or CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
as the image tag according to which is not empty.
I tried image: "registry/group/project:${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME:-$CI_COMMIT_REF_NAME}"
, but the tag is empty.
What is the good syntax to do this please ?
The image
field uses GitLab runners internal variables expansion (in this case Go's os.Expand), which does not support parameter expansion.
You can use workflow:rules to set the variable to use for the image tag based on available variables. For example:
stages:
- build
- use
variables:
IMAGE_BASE: ${CI_REGISTRY}/${CI_PROJECT_PATH}/hello
workflow:
rules:
- if: $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
variables:
IMAGE_TAG: $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
- if: $CI_COMMIT_REF_NAME
variables:
IMAGE_TAG: $CI_COMMIT_REF_NAME
build image:
stage: build
before_script:
- docker login -u ${CI_REGISTRY_USER} -p ${CI_JOB_TOKEN} ${CI_REGISTRY}
script:
- docker build . -t ${IMAGE_BASE}:${IMAGE_TAG}
- docker push ${IMAGE_BASE}:${IMAGE_TAG}
use image:
stage: use
image: ${IMAGE_BASE}:${IMAGE_TAG}
script:
- echo ${CI_JOB_IMAGE}