1

I saw few links where I can tag my docker image using ${Build.SourceVersion} in azure devops pipeline.

But it is using the complete ID of the commit.

But I want to use only the short ID.

I mean this (2cc7968) instead of this (2cc79689fc29ad69698d3062688e2a650da62b8e)

How to get this?

My pipeline:

# 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
Sara June
  • 389
  • 4
  • 15

1 Answers1

0

Not sure if it is the best way, but I've done it through user variables:

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)
Tommy
  • 1
  • 1