Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure pipelines run specific task if and ONLY IF branch is equal to master in yaml

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

like image 267
Theodosis Avatar asked Apr 30 '19 10:04

Theodosis


2 Answers

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

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

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

like image 60
4c74356b41 Avatar answered Oct 15 '22 09:10

4c74356b41


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
like image 32
Dragos Losonti Avatar answered Oct 15 '22 08:10

Dragos Losonti