无法在 Azure DevOps 管道中激活 conda

SLN*_*SLN 5 yaml conda azure-pipelines

在由 conda 构建的 python 项目上测试 azure devops 管道

jobs:
  - job: pre_build_setup
    displayName: Pre Build Setup
    pool:
      vmImage: 'ubuntu-18.04'
    steps:
      - bash: echo "##vso[task.prependpath]$CONDA/bin"
        displayName: Add conda to PATH

  - job: build_environment
    displayName: Build Environment
    dependsOn: pre_build_setup
    steps:
      - script: conda env create --file environment.yml --name build_env
        displayName: Create Anaconda environment
      - script: conda env list
        displayName:  environment installation verification

  - job: unit_tests
    displayName: Unit Tests
    dependsOn: build_environment
    strategy:
      maxParallel: 2
    steps:
      - bash: conda activate build_env
Run Code Online (Sandbox Code Playgroud)

最后一步- bash: conda activate build_env失败,出现以下错误

Script contents:
conda activate build_env
========================== Starting Command Output ===========================
/bin/bash --noprofile --norc /home/vsts/work/_temp/d5af1b5c-9135-4984-ab16-72b82c91c329.sh

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run

    $ conda init <SHELL_NAME>

Currently supported shells are:
  - bash
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.


##[error]Bash exited with code '1'.
Finishing: Bash
Run Code Online (Sandbox Code Playgroud)

我怎样才能激活 conda?似乎路径错误,因此无法找到 conda。

Mer*_*SFT 11

CommandNotFoundError:您的 shell 尚未正确配置为使用“conda activate”。

这里的问题是您的脚本在子 shell 中运行,但conda尚未在此子 shell 中初始化。

您需要将活动脚本更改为?

steps:
  - task: Bash@3
    inputs:
      targetType: 'inline'
      script: |
        eval "$(conda shell.bash hook)"
        conda activate build_env
    displayName: Active
Run Code Online (Sandbox Code Playgroud)

此外,请不要将Add PATHcreate environment和拆分active the environment为不同的作业。

对于 Azure Devops 管道,agent job是管道运行过程的基本单元,每个代理作业都有自己独立的运行环境和工作逻辑。

有关更详细的信息,您使用托管代理在此问题场景中应用您的脚本。

当有一个代理作业开始运行时,我们的池系统将为该代理作业分配一个 VM。并且,一旦代理工作完成,这个虚拟机将被回收。当下一个代理作业开始运行时,将随机重新分配一个全新的 VM 。

dependsOn只能在作业之间共享文件和传递变量。它不能让 VM 在下一个工作中继续。

我相信你应该能够猜到你会遇到什么问题。是的,即使您可以成功应用该activate脚本,您仍将继续面对另一个error: Could not find conda environment: build_env. 那是因为这个activate脚本使用的环境是一个全新的 vm,之前build_environment作业使用的 VM已经被系统回收了。

因此,不要将创建环境和激活分成 2 个代理作业:

  - job: build_environment
    displayName: Build Environment
    dependsOn: pre_build_setup
    steps:
      - script: conda env create --file environment.yml --name build_env
        displayName: Create Anaconda environment
      - script: conda env list
        displayName:  environment installation verification
      - task: Bash@3
        inputs:
          targetType: 'inline'
          script: |
            eval "$(conda shell.bash hook)"
            conda activate build_env
        displayName: Active
Run Code Online (Sandbox Code Playgroud)