SLN
SLN

Reputation: 5082

How to retain environment for different jobs in azure pipeline

Stage can keep all the running environments for its jobs, but I have several different jobs that logically cannot grouped into the same stage. all of these jobs has the same running environment, I don't want to repeat the following code to each jobs, how can I abstract all this steps into some sort of functions and call by each job. Or how can I create a shared environment for those jobs. Or how can I retain the environment instead of groups them all into the same stage?

 steps:
      - bash: echo "##vso[task.prependpath]$CONDA/bin"
        displayName: Add Conda to PATH

      - bash: conda env update -f environment.yml --name $(Agent.Id)
        displayName: Create Conda Environment

      - bash: export PYTHONPATH="src/"
        displayName: Add /src to PYTHONAPTH

      - bash: source activate $(Agent.Id)
        displayName: Active Test Environment

Upvotes: 0

Views: 907

Answers (1)

Levi Lu-MSFT
Levi Lu-MSFT

Reputation: 30313

You can put above steps into a template yaml file. And use step templates to reference it in your main pipeline Yaml file.

For example, create a template yaml file setEnv.yml with above codes.

#File: setEnv.yml

steps:
  - bash: echo "##vso[task.prependpath]$CONDA/bin"
    displayName: Add Conda to PATH

  - bash: conda env update -f environment.yml --name $(Agent.Id)
    displayName: Create Conda Environment
    ...

Use template to reference the above template file.

 # File: azure-pipelines.yml
  stages:
  - stage: A
    jobs:
    - job: macOS
      pool:
        vmImage: 'macOS-10.14'
      steps:
      - template: setEnv.yml # Template reference
      - othertasks: 
 - stage: B
   jobs:
   - job: Linux
     pool:
       vmImage: 'ubuntu-16.04'
     steps:
     - template: setEnv.yml # Template reference
     - othertasks: 

Check the document here for more information.

Upvotes: 1

Related Questions