skip to Main Content

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


  1. Chosen as BEST ANSWER

    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 :)

    # 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
    ---
    variables:
      - name: buildConfiguration
        value: Release
      - "${{ if eq(variables['Build.SourceBranchName'], 'dev') }}":
          - name: agentPool
            value: Azure Pipelines
      - "${{ if eq(variables['Build.SourceBranchName'], 'iis-dev') }}":
          - name: agentPool
            value: Default
    trigger:
      - master
      - dev
      - iis-dev
    jobs:
      - job: null
        displayName: Build and Publish Artifacts
        pool:
          name: $(agentPool)
          vmImage: ubuntu-latest    
        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. Consider using of templates and switch them with if.

    In this case for steps, you may have something like that:

    steps:
    ${{ if eq(variables['Build.SourceBranchName'], 'master') }}:
      - template: templates/for-master.yml
    ${{ if eq(variables['Build.SourceBranchName'], 'iis-dev') }}:
      - template: templates/iis-dev.yml
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search