2

Is it possible to determine within a single YAML Pipeline whether the agent is running self-hosted or in a cloud build? If so, how would one go about determining that?

I need my pipeline to work in both locations but certain steps must only occur if I'm building on one or the other.

Joe
  • 5,394
  • 3
  • 23
  • 54
  • Thank you Leo. I've have no time the past 2 days to do anything but comment out steps. Now that I am able to return to this, I think I am going to go with Bartosz agent-variable approach. That appears to align most closely with what I am trying to do – Joe Jul 23 '20 at 16:40

2 Answers2

1

I can't find directly solution, but it could be done using conditions and agent variables.

For example I have Hosted Agent in my agents pool:

enter image description here

And in YAML I can use this name to condition running step depents on it:

steps:
- script: dotnet build --configuration $(buildConfiguration)
  condition: eq(variables['Agent.Name'],'Hosted Agent') 
  displayName: 'dotnet build $(buildConfiguration)'
Bartosz Pelikan
  • 935
  • 7
  • 18
1

Is it possible to determine within a single YAML Pipeline whether the agent is running self-hosted or in a cloud build? If so, how would one go about determining that?

In the YAML Pipeline, there is pool keyword, which pool to use for a job of the pipeline.

So, we could use this pool keyword to determine within a single YAML Pipeline whether the agent is running self-hosted or in a cloud build:

If you use a self-Hosted agent:

pool:
  name: string  # name of the pool to run this job in

If you use a Hosted agent:

pool:
  vmImage: string # name of the VM image you want to use; valid only in the Microsoft-hosted pool

And check available virtual machine image for some more details.

Besides, if we do not add the pool keyword, Azure pipeline will use the default agent. You could check and change it from the More action -> Triggers-> YAML of pipeline:

enter image description here enter image description here

I need my pipeline to work in both locations but certain steps must only occur if I'm building on one or the other

Just as Bartosz answered, we could use the condition with Agent variables to achieve this request.

Bartosz Pelikan
  • 935
  • 7
  • 18
Leo Liu
  • 71,098
  • 10
  • 114
  • 135