18

I am trying to change my yaml file to add some more tasks. This is my current yaml file:

trigger:
  - master

pool:
  vmImage: 'Ubuntu-16.04'

steps:
  - task: Maven@3
    inputs:
      mavenPomFile: 'pom.xml'
      # according to: https://github.com/MicrosoftDocs/vsts-docs/issues/3845, 
      # maven options should go to goals instead, as mavenOptions is for jvm options
      mavenOptions: '-Xmx3072m'
      javaHomeOption: 'JDKVersion'
      jdkVersionOption: '1.11'
      jdkArchitectureOption: 'x64'
      publishJUnitResults: true
      testResultsFiles: '**/surefire-reports/TEST-*.xml'
      goals: 'verify -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true --batch-mode --show-version'

I want to run one goal only if the branch that runs is master. Basically with my tests, I create a dockerfile and I want to push it to dockerhub, but I don't want that to happen every time someone opens a pull request; I want that to happen only if master is running the tests. Something like this

if branch == master 
steps: *

but I don't seem to find anything in the Azure Pipelines documentation on how to do this

Palle Due
  • 5,929
  • 4
  • 17
  • 32
Theodosis
  • 792
  • 2
  • 7
  • 22

2 Answers2

28

You can use the following condition on the task you want to condition:

eq(variables['Build.SourceBranch'], 'refs/heads/master')

https://learn.microsoft.com/en-us/azure/devops/pipelines/process/conditions?view=azure-devops&tabs=yaml#examples

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
  • 1
    thank you i found something good in multiple jobs documentation and i have one last question does that go under the ` -task: Maven@3 ` can I say something like this under the task `condition: eq(variables['Build.SourceBranch'], 'refs/heads/master')` – Theodosis Apr 30 '19 at 10:31
  • this is exactly where you would put it, under the task – 4c74356b41 Apr 30 '19 at 10:42
6

I do something like this as i find it easier to read later

${{ if contains(Build.SourceBranch, 'master') }}:
  steps:
  - task: Maven@3
    inputs:
      etcetera: 'etc.'
${{ else }}:
  steps:
  - task: Some other task