azure-devopsazure-devops-self-hosted-agent

How to conditionally assign a variable in azure devops?


I have a build pipeline defined as below. However, when I run the build on azure devops, I got an error message regarding the syntax that I have in the file, specifically in the if else condition that I use in the variables section.

The logic is very simple. If the branch is from dev OR dev-ttcwebpd0 OR dev-ttcdbtst, we will use a self-hosted agent from My Server Pool. For anything else, use the one from azure pipelines.

Can anyone point me to the right direction and help me fix this syntax ? Thank you

---
variables:
  - name: buildConfiguration
    value: Release  
  "${{ if or(eq(variables['Build.SourceBranchName'], 'dev'), eq(variables['Build.SourceBranchName'], 'dev-ttcwebpd01'), eq(variables['Build.SourceBranchName'], 'dev-ttcdbtst')) }}":
  - name: agentPool
    value: "My Servers"
  "${{ else }}":
  - name: agentPool
    value: Azure Pipelines
trigger:
  - main
  - dev
  - dev-ttcwebpd01
  - dev-ttcdbtst
  - feature/*
jobs:
  - job: null
    displayName: Build and Publish Artifacts
    pool:
      name: $(agentPool)
      vmImage: ubuntu-latest    
    steps:    
    - checkout: self
      fetchDepth: 0
    - 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: 'Check what account is running'
      inputs:
        targetType: 'inline'
        script: 'whoami'
        workingDirectory: 'src/Api/Build'
    - task: Bash@3
      displayName: 'Install Cake.Tool'
      inputs:
        targetType: 'inline'
        script: 'dotnet tool install --global Cake.Tool | echo "Already installed"'
        workingDirectory: 'src/Api/Build'
    - task: Bash@3
      displayName: 'Execute dotnet cake command'
      inputs:
        targetType: 'inline'
        script: 'dotnet cake'
        workingDirectory: 'src/Api/Build'  
    - task: PublishBuildArtifacts@1
      displayName: 'Publish Build Artifacts'
      inputs:
        PathtoPublish: 'artifacts'
        ArtifactName: 'Artifact'
        publishLocation: 'Container'

Solution

  • The variables should look like

    variables:
      - name: buildConfiguration
        value: Release  
      - ${{ if or(eq(variables['Build.SourceBranchName'], 'dev'), eq(variables['Build.SourceBranchName'], 'dev-ttcwebpd01'), eq(variables['Build.SourceBranchName'], 'dev-ttcdbtst')) }}:
        - name: agentPool
          value: "My Servers"
      - ${{ else }}:
        - name: agentPool
          value: Azure Pipelines
    

    Then you add if you have to indent everything what is inside also you don't need " around if.

    More docs about it here


    EDIT:

    The syntax above that setting the 'if' statement before the 'name' key commonly is used to conditionally enable a variable with a particular value. Anyway, it is also okay to conditionally set the value of a variable by this syntax.

    Another commonly syntax to conditionally set the value of a variable can be like as below:

    variables:
    - name: agentPool
      ${{ if or(eq(variables['Build.SourceBranchName'], 'dev'), eq(variables['Build.SourceBranchName'], 'dev-ttcwebpd01'), eq(variables['Build.SourceBranchName'], 'dev-ttcdbtst')) }}:
        value: 'My Servers'
      ${{ else }}:
        value: 'Azure Pipelines'