Search code examples
azure-devopsazure-pipelinesazure-pipelines-yaml

Azure DevOps Server with YAML - build directory changes with every job?


Since moving everything YAML to a separate Git repo, I have an odd problem: Now, with every job in the pipeline, the directories on the agents change. The first job uses _work\1, the second _work\2, etc., meaning every job except the first fails, as the directories are empty.

I'm using a product-specific pipeline that extends a base-pipeline (thanks to @rui-jarimba for the excellent suggestion!).

Slightly shortened code:

# FOO-pipeline.yml

name: QEC-${{ parameters.productVersion }}-$(Date:yyMMdd)$(Rev:rr)

resources:
  repositories:
    - repository: FOO
      type: git
      name: FOO
      ref: main
      trigger:
        branches:
          include:
          - main
    - repository: YAML
      type: git
      name: YAML-Automation
      ref: main
      trigger:
        - none

extends:
  template: /Base-pipeline.yml
  parameters:
    productName: FOO

Stages Build_with_analysis / Build_without_analysis use _work\1 on both agents, everything is working perfectly well (after I combined differnt build jobs into a single one).

Stage Test_All then uses _work\2, the next stage uses _work\3, etc.

# Base-pipleine.yml

parameters:
  - name: productName
    displayName: Name of the product to build
    type: string

stages:
  - stage: Build_without_analysis
    #      ======================
    displayName: 'Build ${{ parameters.productName }}, no analysis'
    pool:
      demands: BuildServer
    jobs:
    - template: /Jobs/jobBuild-All.yml
      parameters:
        productName: ${{ parameters.productName }}
        configuration: Debug
        staticAnalysis: false


  - stage: Build_with_analysis
    #      ===================
    displayName: 'Build ${{ parameters.productName }}, with analysis'
    dependsOn: []
    pool:
      demands: AnalysisServer
    jobs:
    - template: /Jobs/jobBuild-All.yml
      parameters:
        productName: ${{ parameters.productName }}
        configuration: Release
        staticAnalysis: true


  - stage: Test_All
    #      ========
    displayName: 'All tests ${{ parameters.productName }}'
    dependsOn: Build_without_analysis    
    pool:
      demands: BuildServer    
    jobs:
    - template: /Jobs/jobTests-All.yml
      parameters:
        productName: ${{ parameters.productName }}
# jobTests-All.yml

parameters:
- name: productName
  type: string
  default: FOO

jobs:

- job: Run_all_unit_tests
  #    ==================
  displayName: 'All Unit Tests'
  steps:
    - checkout: none  
    - template: /Jobs/${{ parameters.productName }}/jobTests-${{ parameters.productName }}-UnitTests.yml
      parameters:    
        configuration: Debug

What might be the reason?

Haven't been able to find anything related in the Microsoft documentation, or anywhere else online.


Solution

  • As mentioned by @jessehouwing, Jobs are assigned to agents without any assumptions.

    Since you are using On-Prem DevOps server, and have different repository resources(FOO, YAML).Checked on my side, if you have different repositories checkout in different jobs in same agent, it could use different folder (_work\1, _work\2) as workspace.

    If you would like to get the workingdirectory from previous jobs, one possible workaround is to set variable on previous stage and transfer to next stage. Please note it doesn't change the default working directory on new stage, but jump into the directory in previous stage for tasks like powershell, bash tasks..

    For example, on jobs in stage Build_without_analysis, set variable:

        - powershell: echo "##vso[task.setvariable variable=directory;isOutput=true]$(Build.SourcesDirectory)"
          name: setvarStep
    

    enter image description here

    In the dependent stage Test_All, get the variable:

        variables:
           myVarfromStageA: $[ stageDependencies.Build_without_analysis.jobBuildAll.outputs['setvarStep.directory'] ]
    

    enter image description here

    In the jobs, you can go into that directory to have the code.

    enter image description here

    You can check the doc for the variable usage.