最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

variables - Use CI_COMMIT_REF_NAME or CI_MERGE_REQUEST_SOURCE_BRANCH_NAME in image_name - Stack Overflow

programmeradmin9浏览0评论

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 ?

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 ?

Share Improve this question asked Feb 5 at 9:39 mookymooky 1196 bronze badges 2
  • I believe shell parameter expansion does not work here, so gitlab is looking for a variable called CI_MERGE_REQUEST_SOURCE_BRANCH_NAME:-$CI_COMMIT_REF_NAME which does not exist... – Michael Kotzjan Commented Feb 6 at 14:41
  • Here (docs.gitlab.com/ee/ci/variables/…) it says "can be expanded ? yes" – mooky Commented Feb 7 at 15:53
Add a comment  | 

1 Answer 1

Reset to default 1

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}
发布评论

评论列表(0)

  1. 暂无评论