Azure Devops not supporting build-args with Docker@2

Viewed 1607

I need to pass the build id parameter from Azure Devops to a dockerfile from a yaml pipeline. Unfortunately Azure Devops doesn't support the docker build-args parameter as below.

Be aware that if you use value buildAndPush for the command parameter, then the arguments property will be ignored. source

Is there any other way to pass arguments from Azure Devops through to a dockerfile?

2 Answers

Is there any other way to pass arguments from Azure Devops through to a dockerfile?

Yes. The BuildAndPush Command doesn't support adding argument.

But the Build command support it.

You could split buildandpush into Docker build task and docker push task.

Here is an example:

steps:
- task: Docker@2
  displayName: Docker Build 
  inputs:
    command: build
    repository: $(imageRepository)
    containerRegistry: $(dockerRegistryServiceConnection)
    dockerfile: $(dockerfilePath)
    tags: 5.12.4
    arguments: '--build-arg test_val="$(build.buildid)" '
- task: Docker@2
  displayName: Docker Push
  inputs:
    command: push
    repository: $(imageRepository)
    containerRegistry: $(dockerRegistryServiceConnection)
    tags: 5.12.4

Docker Build Step Result:

enter image description here

- task: Docker@2
  displayName: 'Build'
  inputs:
    command: build
    containerRegistry: 'MY Container Registry'
    repository: 'my-repo/my-image'
    Dockerfile: dockerfile.api
    tags: |
     latest
     $(Build.BuildId)
    arguments: '--build-arg major=$(BuildMajor) --build-arg minor=$(BuildMinor) --build-arg build=$(Build.BuildId) --build-arg revision=$(BuildRevision)'

- task: Docker@2
  displayName: Push
  inputs:
    command: push
    containerRegistry: 'MY Container Registry'
    repository: 'my-repo/my-image'
    tags: |
     latest
     $(Build.BuildId)

If you are looking for multiple build args, thats how you can do it.

Related