I am having difficulty in achieving a requirement. I need to determine the Agent pool based on Build.Reason. If the reason is PullRequest, I need to assign the value of agent pool using value of a Library variable or else assign the value based on user selection for Manual builds. Also, when the value of the effective selected agent based on the above comparison matches a particular pattern, I need to set value of few other variables.
Attaching a sample pipeline yml snippets which I am trying to achieve the same. The logic is that when it is a MacOS based agent, I need to set JFrog variables to use private endpoint values and when it is not MacOS, it needs to use public end point. Also, for all pull requests, the agent pool needs to be fetched from Library variable which can also be a macos based agent or non macos based agent which I can update based on various other factors.
It looks like the time of evaluating the library variable is causing difficulty in adding expression for the JFrog variables. I have tried several approaches, but it is difficult to analyze the exact value that is used in various expressions which makes it difficult to find where the mistake is and fix the patterns.
azure-pipelines.yml
parameters:
- name: AgentName
type: string
default: 'macos-latest'
- name: JFrogPrivateEndpoint
displayName: 'Use JFrog Private Endpoint'
type: boolean
default: false
jobs:
- job: BuildTestDeploy
variables:
- group: 'GroupVariable'
# Set agent name variable based on build reason
# DefaultAgentPool can be macos-latest or windows-latest or ubuntu-latest and set via library variable
- name: EffectiveAgentName
${{ if eq(variables['Build.Reason'], 'PullRequest') }}:
value: '$(DefaultAgentPool)'
${{ else }}:
value: '${{ parameters.AgentName }}'
# Include JFrog variables from the shared template
- template: jfrog-variables.yml
parameters:
JFrogPrivateEndpoint: ${{ parameters.JFrogPrivateEndpoint }}
JFrogAgentName: ${{ variables.EffectiveAgentName }}
# Where the pipeline should be ran on
pool:
vmImage: ${{ variables.EffectiveAgentName }}
workspace:
clean: all
steps:
- task: PowerShell@2
displayName: 'Display Conditional Variables'
inputs:
targetType: 'inline'
script: |
Write-Output "Selected Agent Name: '${{ parameters.AgentName }}'"
Write-Output "Default Agent Pool: '$(DefaultAgentPool)'"
Write-Output "Effective Agent Pool: '$(EffectiveAgentName)'"
Write-Output "Build.Reason: '$(Build.Reason)'"
Write-Output "Using Private Endpoint: $(usePrivateEndpoint)"
Write-Output "Using JFrog URI: $(ArtifactoryURI)"
jfrog-variables.yml
parameters:
- name: JFrogPrivateEndpoint
type: boolean
- name: JFrogAgentName
type: string
variables:
# Define private endpoint logic for JFrog connections
- name: usePrivateEndpoint
${{ if or(parameters.JFrogPrivateEndpoint, contains(parameters.JFrogAgentName, 'macos-')) }}:
value: 'true'
${{ else }}:
value: 'false'
# JFrog URI variables based on the same condition as usePrivateEndpoint
- name: ArtifactoryURI
${{ if or(parameters.JFrogPrivateEndpoint, contains(parameters.JFrogAgentName, 'macos-')) }}:
value: '(JFrogBaseURI_Private)'
${{ else }}:
value: '(JFrogBaseURI)'
When the DefaultAgentPool value is macos-latest, I am getting ArtifactoryURI as (JFrogBaseURI) instead of (JFrogBaseURI_Private)