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:

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.