Azure DevOps管道Docker任务如何提取BuildNumber前缀作为镜像标签
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

