Reputation: 16532
I have a build script that I want to run some build steps for code on master
, features/*
and releases/*
, and then some publishing features only on master
and releases/*
branches.
I can't seem to locate any documentation for sectioning out the build script by branches. Here's a rough overview of my build script (just the tasks, leaving off params for brevity)
trigger:
- master
- releases/*
- features/*
pool:
vmImage: 'windows-latest'
name: $(Major).$(Minor).$(rev:r)
variables:
solution: '**/*.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'
Major: 1
Minor: 1
steps:
- task: NuGetCommand@2
- task: VSBuild@1
- task: VSTest@2
## I would like everything below this line to only be run on releases/* or master
- task: WhiteSource Bolt@20
- task: NuGetCommand@2
- task: PublishBuildArtifacts@1
The second half would need to run conditionally, and only if the top half is successful. I am open to splitting these out into two scripts in it makes it easier.
Upvotes: 0
Views: 1060
Reputation: 30313
You can use - ${{if...}}:
script block in your pipeline. See below example:
steps:
- powershell: echo "task 0"
displayName: task 0
- ${{ if or(eq(variables['Build.SourceBranch'],'refs/heads/master'), startsWith(variables['Build.SourceBranch'],'refs/heads/release/')) }}:
- powershell: echo "task 1"
displayName: task 1
- powershell: echo "task 2"
displayName: task 2
Note: Yaml pipeline probably will hightlight above the - ${{if...}}
script block with red wavy lines. But it will disappear after you save the pipeline. And the indentation is very important.
In above example, if the - ${{if...}}
script block is evaluated to true, then task 1 and task 2 will be executed. See below screenshot from above yaml example:
See here for more information about - ${{if...}}
script block. And here for the Expressions you can use in azure yaml pipeline.
Another workaround is to use conditions as Shayki mentioned. But i use expression startsWith
to check the branch - releases/*
. See below example:
steps:
- powershell: echo "task 0"
displayName: task 0
- powershell: echo "task 1"
condition: or(eq(variables['Build.SourceBranch'],'refs/heads/master'), startsWith(variables['Build.SourceBranch'],'refs/heads/releases/'))
displayName: task 1
- powershell: echo "task 2"
condition: or(eq(variables['Build.SourceBranch'],'refs/heads/master'), startsWith(variables['Build.SourceBranch'],'refs/heads/releases/'))
displayName: task 2
Upvotes: 1
Reputation: 41545
You can use a custom condition with that check the branch name:
and(succeeded(), in(variables['Build.SourceBranch'], 'refs/heads/master', 'refs/heads/releases/*'))
In YAML you add in the step:
condition: and(succeeded(), in(variables['Build.SourceBranch'], 'refs/heads/master', 'refs/heads/releases/*'))
Upvotes: 2