![在 azure devops 中,如何使用 git 提交代码短 ID 标记 docker 镜像?](https://linux22.com/image/773443/%E5%9C%A8%20azure%20devops%20%E4%B8%AD%EF%BC%8C%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20git%20%E6%8F%90%E4%BA%A4%E4%BB%A3%E7%A0%81%E7%9F%AD%20ID%20%E6%A0%87%E8%AE%B0%20docker%20%E9%95%9C%E5%83%8F%EF%BC%9F.png)
我看到了一些链接,我可以在 azure devops 管道中使用 ${Build.SourceVersion} 标记我的 docker 镜像。
但它使用的是提交的完整 ID。
但我只想使用短 ID。
我的意思是这个(2cc7968)而不是这个(2cc79689fc29ad69698d3062688e2a650da62b8e)
如何获得它?
我的管道:
# Deploy to Azure Kubernetes Service
# Build and push image to Azure Container Registry; Deploy to Azure Kubernetes Service
# https://docs.microsoft.com/azure/devops/pipelines/languages/docker
trigger:
- master
resources:
- repo: self
variables:
# Container registry service connection established during pipeline creation
dockerRegistryServiceConnection: "685f0716-8b46-436e-8d2a-3d0ff987fce9"
imageRepository: "azuredevopssampleapp"
containerRegistry: "aksdevopsacrtesting.azurecr.io"
dockerfilePath: "**/Dockerfile"
tag: "$(Build.BuildId)"
imagePullSecret: "aksdevopsacrtesting458647f2-auth"
# Agent VM image name
vmImageName: "ubuntu-latest"
stages:
- stage: Build
displayName: Build stage
jobs:
- job: Build
displayName: Build
pool:
vmImage: $(vmImageName)
steps:
- task: Docker@2
displayName: Build and push an image to container registry
inputs:
command: buildAndPush
repository: $(imageRepository)
dockerfile: $(dockerfilePath)
containerRegistry: $(dockerRegistryServiceConnection)
tags: |
$(tag)
- upload: pipeline_content/manifests
artifact: manifests
答案1
不确定这是否是最好的方法,但我已经通过用户变量完成了:
variables:
- name: commit
value: none
steps:
- task: Bash@3
displayName: Set GIT revisions
inputs:
targetType: 'inline'
script: echo "##vso[task.setvariable variable=commit]$(git rev-parse --short HEAD)"
- task: Docker@2
displayName: Build and publish Rest Api image
inputs:
containerRegistry: '...'
repository: '...'
command: 'buildAndPush'
Dockerfile: '...'
tags: |
$(commit)