DEV Community

loading...

Azure DevOps YAML pipeline : Use conda environment

kenakamu profile image Kenichiro Nakamura ・1 min read

Azure DevOps hosted agents have many software so that you don't have to install them every time.

However, in case you need to use some python package, you can do:

  • Install them via pip install
  • Use conda environment and use environment.yaml

pip install

The easiest way to install python package is to use pip install.
I can simply use script task to run:

- script: pip install <package> <package> ..
Enter fullscreen mode Exit fullscreen mode

Use conda environment

If there are multiple pipelines which require same package again and again, it's better to manage them at once. In this scenario, you can use either requirements.txt for pip install or conda environment.

Run pipelines with Anaconda environments

Hosted agents has conda installed by default, so I just need to run following commands.

- script: conda env create --quiet --file environment.yml
  displayName: Create Anaconda environment
Enter fullscreen mode Exit fullscreen mode

One caveat is that I need to activate it in every build step.

Each build step runs in its own process. When you activate an Anaconda environment, it will edit PATH and make other changes to its current process. Therefore, an Anaconda environment must be activated separately for each step.

I use it with Azure CLI task but it works same way.

- task: AzureCLI@1
  inputs:
    azureSubscription: '$(my_subscription_connection)'
    scriptLocation: inlineScript
    workingDirectory: $(Build.SourcesDirectory)
    inlineScript: |
      # Activate conda environment
      source activate myenvironment
      export SUBSCRIPTION_ID=$(az account show --query id -o tsv)
      # Invoke any azure cli command
Enter fullscreen mode Exit fullscreen mode

Sample in GitHub

GitHub: https://github.com/MicrosoftDocs/pipelines-anaconda has more detail information.

Discussion (0)

pic
Editor guide