How to add custom build tags for docker images in azure-pipelines.yml file?

Viewed 9134

We're using Azure DevOps for our project. I've azure-pipelines.yml file which uses the build id to the docker image tag. But, We're looking to manually add the docker image id as part of the build definition.

Is it possible to pass the runtime arguments from build queue rest api to azure-pipelines.yml file?

azure-pipelines.yml

trigger:
 - master

pool:
  vmImage: 'Ubuntu-16.04'

resources:
 - repo: self

variables:
  imageName: 'sampleapp1'
  dockerhubNS: 'kumaresh' 

steps:
 - task: Docker@2
   displayName: Login to Docker Hub
   inputs:
     command: login
     containerRegistry: dh-connection
- task: Docker@2
  displayName: Build and Push an image
  inputs:
    command: buildAndPush
    dockerfile: Dockerfile
    containerRegistry: dh-connection
    repository: $(dockerhubNS)/$(imageName)
    tag: $(appBuildNumber)

Request Body for Build Queue

"definition": {
    "id": Build_Definition_Id,
    "name": extractDefinitionName,
    "type": "build"
 },
 "templateParameters": {
     "tag": "1"
 }
1 Answers

Is it possible to pass the runtime arguments from build queue rest api to azure-pipelines.yml file?

Not sure what exactly mean about runtime argument. But I guess what you are looking for is queue build with rest api along with passing dynamic variable to pipeline at same time.

Sample scenario:

Here is my azure-pipeline.yml:

parameters:
- name: tag
  type: string
  default: 'aaa'

trigger:
- master

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: Docker@2
  displayName: build
  inputs:
    containerRegistry: DockerHub
    repository: {my docker repos}
    command: build
    Dockerfile: Docker/TestWebApi/Dockerfile
    tags: '${{ parameters.tag }}'

- task: Docker@2
  displayName: push
  inputs:
    containerRegistry: DockerHub
    repository: {my docker repos}
    command: push
    tags: '${{ parameters.tag }}'

You can see, the dynamic image tag it needed is parameter tag. It will be passed a new value when I queue this pipeline with rest api:

https://dev.azure.com/{org}/{project}/_apis/pipelines/{definition id}/runs?api-version=5.1-preview.1

Body:

{
  "resources": {
    "repositories": {
      "self": {
        "refName": "refs/heads/master"
      }
    }
  },
  "templateParameters": {
    "tag": "new"
  }
}

Just configure the variable you want pass to in request body.

enter image description here


Added:

  "variables": {
    "myVariable": {
      "value": "0325ApiQueue"
    }
Related