You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure DevOps管道Docker任务如何提取BuildNumber前缀作为镜像标签

Solution

The issue occurs because the Docker@2 task's tags field doesn’t natively support executing shell commands directly—unlike a Shell/Bash task, it only handles variable substitution. To fix this, extract the desired tag value in a separate step and store it as a pipeline variable, then reference that variable in the Docker task.

Step 1: Add a Bash task to set the tag variable

Insert this task before your Docker@2 task to compute and store the date-revision segment as a reusable variable:

- task: Bash@3
  inputs:
    targetType: 'inline'
    script: |
      # Extract the first part of BuildNumber (date.revision format)
      docker_tag=$(echo "$(Build.BuildNumber)" | cut -d'_' -f1)
      # Save as a pipeline variable for subsequent tasks
      echo "##vso[task.setvariable variable=dockerTag;]$docker_tag"

Step 2: Use the variable in the Docker@2 task

Update your Docker build task to reference the $(dockerTag) variable in the tags field:

- task: Docker@2
  inputs:
    containerRegistry: 'your-registry-service-connection'
    repository: 'your-target-repo'
    command: 'build'
    Dockerfile: '**/Dockerfile'
    tags: |
      $(dockerTag)

This method ensures the tag is precomputed in a shell environment (where your original command works) and passed to the Docker task as a valid, plain-text value, eliminating the invalid tag format error.

内容的提问来源于stack exchange,提问作者SMendi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 23:59:56