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.
Navigate to your team project on Azure DevOps in a new browser tab. Before digging into the YAML pipelines, you will want to disable the existing build pipeline. Navigate to Pipelines. Select the existing PartsUnlimitedE2E pipeline. From the dropdown, select Pause pipeline. Navigate to the Pipelines hub. Click New pipeline.
Azure DevOps CLI In the most common case, you set the variables and use them within the YAML file. This allows you to track changes to the variable in your version control system. You can also define variables in the pipeline settings UI (see the Classic tab) and reference them in your YAML.
When this variable is added, it will then be made available a global variable in the pipeline and can be overridden by the same variable name in the YAML file. Finally, when a pipeline processes a YAML file and gets down to the steps that require script execution, the pipeline is in the compile “phase”.
You can use a variable group to make variables available across multiple pipelines. You can use templates to define variables that are used in multiple pipelines in one file. In addition to user-defined variables, Azure Pipelines has system variables with predefined values.
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. ^^
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With