Reputation: 2994
name: $(Build.BuildId)-${{ variables['Build.SourceBranchName']}}
pool:
${{ if eq(variables['Build.SourceBranchName'], 'merge') }}:
name: 'Default' # run on rwb's machine (plus whatever else is in the default pool).
${{ if ne(variables['Build.SourceBranchName'], 'merge') }}:
vmImage: 'windows-latest' # run on the MS hosted pool (limited to 1800 hours per month).
variables:
-name: isHosted
${{ if eq(variables['Build.SourceBranchName'], 'merge') }}:
value: false
${{ if ne(variables['Build.SourceBranchName'], 'merge') }}:
value: true
but
/azure-pipelines.yml (Line: 4, Col: 3): A template expression is not allowed in this context
/azure-pipelines.yml (Line: 6, Col: 3): A template expression is not allowed in this context
It's also a mess having to repeat the conditions.
Can this be made to work?
Update
Tidied this up. Had to add a job, irritatingly. The variable seems to be getting set correctly, but the job always runs on the Hosted Agent;
the condition ${{ if eq(variables.useSelfHostedAgent, 'True') }}
is ignored but the condition ${{ if eq(variables['Build.SourceBranchName'], 'merge') }}
works!
(This makes no sense -- which seems to be normal for Azure pipeline YML.)
name: $(Build.BuildId)-${{ variables['Build.SourceBranchName']}}
variables:
useSelfHostedAgent: $[eq(variables['Build.SourceBranchName'], 'merge')]
trigger:
- '*'
###############################################################################
resources:
repositories:
- repository: AzurePipelineTemplates
type: git
name: AzurePipelineTemplates/AzurePipelineTemplates
jobs:
- job: Product
pool:
#${{ if eq(variables.useSelfHostedAgent, 'True') }}: # is ignored
${{ if eq(variables['Build.SourceBranchName'], 'merge') }}:
name: 'Default' # run on rwb's machine (plus whatever else is in the default pool).
${{ else }}:
vmImage: 'windows-latest' # run on the MS hosted pool (limited to 1800 hours per month).
steps:
- task: PowerShell@2
displayName: Condition
condition: eq(variables.useSelfHostedAgent, 'True')
inputs:
targetType: 'inline'
script: |
Write-Host "Now is the time for all good men to come to the aid of the party."
- task: PowerShell@2
displayName: Get pool name
inputs:
targetType: 'inline'
script: |
Write-Host "Agent.Name is $(Agent.Name)"
Write-Host "useSelfHostedAgent is $(useSelfHostedAgent)"
- template: BuildTestPackPublish.yml@AzurePipelineTemplates
parameters:
projectsToTest: '**/Company.Product.Tests.csproj'# Not SeleniumTests.
Upvotes: 0
Views: 873
Reputation: 2994
jobs:
- job: ${{ parameters.jobName }}
pool:
${{ if or(eq(variables['Build.SourceBranchName'], 'master'), eq(variables['Build.SourceBranchName'], 'main'), false) }}:
vmImage: 'windows-latest' # Always build master on the MS hosted pool (limited to 1800 hours per month).
${{ else }}:
name: 'Default'
${{ if or(and(parameters.symbols, ne(variables['Build.SourceBranchName'], 'merge')), parameters.migrations) }}:
demands:
- Agent.OS -equals Windows_NT # PushSymbols task in pack.yml only works on Windows.
It is impossible to use Build.BuildId
in the ${{ if
.
Upvotes: 0
Reputation: 3406
From the warning information, it seems like it isn't allowed if the pool definition is higher up(e.g. at pipeline level) but it works fine if the pool definition is inside a job definition.
You can use else
expression to replace ${{ if ne(variables['Build.SourceBranchName'], 'merge') }}
that keeps your code simple.
For example:
jobs:
- job: example
pool:
${{ if eq(variables['Build.SourceBranchName'], 'merge') }}:
name: 'Default'
${{ else }}:
vmImage: 'windows-latest'
BTW you can use Use parameters in pipelines
to select which pool you want to run.
Please refer to doc:Use parameters in pipelines
UPDATE
the condition ${{ if eq(variables.useSelfHostedAgent, 'True') }} is ignored
Because you define the variable useSelfHostedAgent
as runtime expressions,runtime expressions can be used in variables and conditions. It can't be recognized in if expression.
Please refer to doc:Specify conditions
Upvotes: 1
Reputation: 59065
You have a syntax error.
-name: isHosted
should be - name: isHosted
Upvotes: 1