DEV Community

Jeg
Jeg

Posted on

Setting up variables in Azure-Devops pipelines

To add a variable in the pipeline yaml files:

trigger:
- none

name: $(TeamProject)-$(SourceBranchName)-$(Build.BuildId)-$(Hours)$(Minutes)$(Seconds)-$(Date:yyyyMMdd)$(Rev:.r)

variables: 
  MyDockerTag: '$(Build.BuildId)'

pool:
  name: DemoPool
  demands:
  - Agent.Name -equals $(agentname)

stages:
- stage: Build
  displayName: Build stage
  jobs:
  - job: Build
    timeoutInMinutes: 0 
    displayName: Build
    pool:
      name: DemoPool
      demands:
      - Agent.Name -equals $(agentname)
    steps:

    - task: Docker@2
      inputs:
        containerRegistry: 'registry-name'
        repository: '$(namespace)'
        command: 'build'
        Dockerfile: '**/Dockerfile'
        tags: 'latest'

     - task: Docker@2
      inputs:
        containerRegistry: 'registry-name'
        repository: '$(namespace)-test'
        command: 'push'
        tags: '$(MyDockerTag)'
Enter fullscreen mode Exit fullscreen mode

The trigger none - says the build is not auto-triggered whenever there is code push. Variables are defined in the variables section.

$(Build.BuildId) is the system variable and that would have the build number as the value.

For agentname and namespace, the variable can be defined globally as shown below:

Image description

Top comments (0)