首页 > 解决方案 > Azure DevOps Pipeline reference current tag in docker push task

问题描述

Here is my pipeline configuration file:

trigger:
- master

pool:
  vmImage: 'Ubuntu-16.04'

variables:
  imageName: 'pipelines-kotlin-docker'
  service-connection: 'service-connection'

steps:
# Gradle
# Build using a Gradle wrapper script
- task: Gradle@2
  inputs:
    tasks: 'build' # A list of tasks separated by spaces, such as 'build test'

- task: Docker@2
  displayName: Build an image
  inputs:
    repository: $(imageName)
    command: build
    Dockerfile: Dockerfile

- task: Docker@2
  displayName: Push image
  inputs:
    containerRegistry: |
      $(service-connection)
    repository: $(imageName)
    command: push
    tags: |
      test1

When running the pipeline I get the following error.

[error]tag does not exist: pipelines-kotlin-docker:test1

The build task yields the following output: Successfully tagged pipelines-kotlin-docker:396

I replaced test1 with $(Build.BuildNumber) in the push task and got the following error.

[error]invalid reference format

How can I reference the tag of the image which has been built by the build task in the push task?

标签: dockerazure-devopstags

解决方案


The Default tag for Docker build task is $(Build.BuildId). Check here for more information about Build variables.

You should replace tag:test1 to $(Build.BuildId) in docker push task. You check here for Docker task parameters.

- task: Docker@2
  displayName: Push image
  inputs:
    containerRegistry: |
      $(service-connection)
    repository: $(imageName)
    command: push
    tags: |
      $(Build.BuildId)

Or you can also specify the tag: test1 for docker build task. and keep the tag: test1 for docker push task.

- task: Docker@2
  displayName: Build an image
  inputs:
    repository: $(imageName)
    command: build
    Dockerfile: Dockerfile
    tags: |
      test1

推荐阅读