在 azure devops 中,如何使用 git 提交代码短 ID 标记 docker 镜像?

在 azure devops 中,如何使用 git 提交代码短 ID 标记 docker 镜像?

我看到了一些链接,我可以在 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)

相关内容