Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine variables in Azure Devops Build Pipeline YAML

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.

like image 908
JamesMatson Avatar asked Apr 13 '20 01:04

JamesMatson


People also ask

How do I create a YAML pipeline in Azure DevOps?

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.

How do I use variables in Azure DevOps CLI?

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.

What happens when a YAML file is added to a pipeline?

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”.

How do I use multiple variables in azure pipelines?

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.


2 Answers

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. ^^

like image 57
Kalaschni Avatar answered Sep 18 '22 13:09

Kalaschni


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

like image 35
Mengdi Liang Avatar answered Sep 22 '22 13:09

Mengdi Liang