I have a build pipeline for my project setup on azure.
This project has 2 branches: master and iis.
I also have 2 different pipeline yaml files for these branches.
For master branch, I want it build with yaml1. For iis branch, it needs to grab yaml2.
Most of the parts inside these yamls file are the same, except the pool name. For master branch, I have azure hosted agent while for iis, I use our own microsoft hosted agent.
How would I achieve this without creating another build pipeline ? Thank you
Yaml1:
# ASP.NET Core
# Build and test ASP.NET Core projects targeting .NET Core.
# Add steps that run tests, create a NuGet package, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core
trigger:
- iis-dev
pool:
vmImage: 'ubuntu-latest'
variables:
buildConfiguration: 'Release'
steps:
- task: AzureKeyVault@1
inputs:
azureSubscription: 'MySubscription'
KeyVaultName: 'MyKeyVault'
SecretsFilter: '*'
- task: UseDotNet@2
displayName: 'Install .NET Core SDK'
inputs:
packageType: 'sdk'
version: '6.x'
# Node.js tool installer v0
# Finds or downloads and caches the specified version spec of Node.js and adds it to the PATH.
- task: NodeTool@0
displayName: 'Install npm'
inputs:
versionSource: 'spec' # 'spec' | 'fromFile'. Required. Source of version. Default: spec.
versionSpec: '18.x' # string. Optional. Use when versionSource = spec. Version Spec. Default: 6.x.
#versionFilePath: # string. Optional. Use when versionSource = fromFile. Path to the .nvmrc file.
#checkLatest: false # boolean. Check for Latest Version. Default: false.
#force32bit: false # boolean. Use 32 bit version on x64 agents. Default: false.
- task: Bash@3
displayName: 'Install Cake.Tool'
inputs:
targetType: 'inline'
script: 'dotnet tool install --global Cake.Tool | echo "Already installed"'
workingDirectory: 'src/App/Build'
- task: Bash@3
displayName: 'Execute dotnet cake command'
inputs:
targetType: 'inline'
script: 'dotnet cake --dockerRegistry="$(dockerRegistry)" --dockerRegistryUsername="$(dockerRegistryUsername)" --dockerRegistryPassword="$(dockerRegistryPassword)"'
workingDirectory: 'src/App/Build'
- task: PublishBuildArtifacts@1
displayName: 'Publish Build Artifacts'
inputs:
PathtoPublish: 'artifacts'
ArtifactName: 'Artifact'
publishLocation: 'Container'
Yaml2:
# ASP.NET Core
# Build and test ASP.NET Core projects targeting .NET Core.
# Add steps that run tests, create a NuGet package, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core
trigger:
- iis-dev
pool:
name: 'Default'
vmImage: 'ubuntu-latest'
variables:
buildConfiguration: 'Release'
steps:
- task: AzureKeyVault@1
inputs:
azureSubscription: 'MySubscription'
KeyVaultName: 'MyKeyVault'
SecretsFilter: '*'
- task: UseDotNet@2
displayName: 'Install .NET Core SDK'
inputs:
packageType: 'sdk'
version: '6.x'
# Node.js tool installer v0
# Finds or downloads and caches the specified version spec of Node.js and adds it to the PATH.
- task: NodeTool@0
displayName: 'Install npm'
inputs:
versionSource: 'spec' # 'spec' | 'fromFile'. Required. Source of version. Default: spec.
versionSpec: '18.x' # string. Optional. Use when versionSource = spec. Version Spec. Default: 6.x.
#versionFilePath: # string. Optional. Use when versionSource = fromFile. Path to the .nvmrc file.
#checkLatest: false # boolean. Check for Latest Version. Default: false.
#force32bit: false # boolean. Use 32 bit version on x64 agents. Default: false.
- task: Bash@3
displayName: 'Install Cake.Tool'
inputs:
targetType: 'inline'
script: 'dotnet tool install --global Cake.Tool | echo "Already installed"'
workingDirectory: 'src/App/Build'
- task: Bash@3
displayName: 'Execute dotnet cake command'
inputs:
targetType: 'inline'
script: 'dotnet cake --dockerRegistry="$(dockerRegistry)" --dockerRegistryUsername="$(dockerRegistryUsername)" --dockerRegistryPassword="$(dockerRegistryPassword)"'
workingDirectory: 'src/App/Build'
- task: PublishBuildArtifacts@1
displayName: 'Publish Build Artifacts'
inputs:
PathtoPublish: 'artifacts'
ArtifactName: 'Artifact'
publishLocation: 'Container'
2
Answers
I ended up not to use template, because that would require me to have another 2 yaml files. Instead, I use if else expression in pipeline variables to determine which pool that I should run based on the branch. That way I still have only one yaml file :)
Consider using of templates and switch them with
if
.In this case for steps, you may have something like that: