2
votes

I'm creating a docker build and push task in an Azure Devops Build pipeline YAML file, and I'd like to tag the image with the combination of two variables, the project name and the build number, so the tag would be service_22 (service is the project, 22 is the build number).

How do I join two variables together, e.g. $(variable)_$(variable2)

- task: Docker@2
  inputs:
    containerRegistry: 'Azure'
    repository: 'jdmcontainers'
    command: 'buildAndPush'
    Dockerfile: 'Dockerfile'
    tags: |
      $(Build.BuildId)
      $(imageName)

That's the current file, the tags are added as individual separate tags.

2
did you try '$(variable)_$(variable2)' ?Rishanthakumar

2 Answers

0
votes

Try with below format:

steps:
- task: Docker@2
  displayName: buildAndPush
  inputs:
    xxxx
    tags: '$(System.TeamProject)_$(Build.Buildid)'

$(System.TeamProject) is one environment variable which can get the current project name.


enter image description here

10
votes

For those who come here and are actually looking for a way to combine two strings/variables in the job variable declaration, you can use the format() expression (docs link):

- job: Build
  variables:
    job_variable: $[format('Hey-{0}', variables['Build.Reason'])]

Hope that helps someone, at leased I searched for this for hours and found nothing. ^^