5

So I'm setting up a new build pipeline, which is quite complex, it is multi stage and inside the stages multi job.

But I run into problems because when I try to save some tasks by relying on the second and third job of one stage being executed in the same agent working folder. Of course this doesn't really work. It actually works sometimes, but sometimes not. I would expect the different jobs to either always run in the same folder as the job before or never, but not sometimes.

Anyway, so I was wondering if there is a way of restraining the entire stage or pipeline to a single working directory. Anybody any ideas? Thanks!!!

DanDan
  • 1,038
  • 4
  • 15
  • 28
  • The obvious suggestion is to combine the multiple jobs into a single job. Is that an option for you? – Vince Bowdren Dec 30 '21 at 09:55
  • It is an option of course, but that's what I was hoping to avoid – DanDan Dec 30 '21 at 10:10
  • Can I ask why? If you want to keep using the same working directory, that sounds like you're already committing to using *only a single agent*, so I guess you aren't running the jobs in parallel. – Vince Bowdren Dec 30 '21 at 11:51
  • The pipeline is quite complicated, I'm trying to do this mainly for better visibility. I'm running tests on different components (stages) and I'm testing different test levels (jobs). It certainly is doable to do everything in a single job, but I'm trying to avoid it – DanDan Dec 30 '21 at 14:01
  • Hi @DanDan were you able to solve it? We have self-hosted agent and dealing with same issue. – Petr Nymsa Nov 08 '22 at 14:34

2 Answers2

4

Its quite old question but i still would like to answer in case it helps someone else . Yes there is a way all you need to do is to store the agent name of yo first job in a variable and then for any other job you need to ask for same agent. Its done like this -

 jobs:
- job: A
  steps:
  - bash: |
     echo "##vso[task.setvariable variable=AgentName;isoutput=true]$(Agent.Name)"
    name: passOutput
- job: B
  dependsOn: A
  variables:
    AgentName: $[ dependencies.A.outputs['passOutput.AgentName'] ]  
  pool:
    name: pool1
    demands: Agent.Name -equals $(Agent.Name)   
  steps:
  - bash: |
     echo $(AgentName)

Worked like charm for me.

3

is there a way of restraining the entire stage or pipeline to a single working directory

No, there isn't. The agent gets to pick what subdirectory of its working directory is used as the workspace for any given job, and that's not controllable.

Also, relying on the same workspace being used for each job assumes that the same agent is used for each job, which is not how jobs and agents are designed to work. Each job is assumed to be logically independent, and capable of running on whichever agent presents itself (from the pool) as available.

unless there is only a single agent in the pool that meets the demands, there is no guarantee that subsequent jobs will use the same agent as previous jobs.

Vince Bowdren
  • 8,326
  • 3
  • 31
  • 56