在 gitlab CI 期间激活 conda 环境

EuR*_*rth 11 python continuous-integration gitlab anaconda conda

我的.gitlab-ci.yml文件看起来像这样:

anomalydetector:
  image: continuumio/miniconda:4.7.10
  stage: build
  tags:
    - docker
  script:
    - conda env create -f environment.yml
    - conda activate my-env
    - pytest tests/.
Run Code Online (Sandbox Code Playgroud)

在 Gitlab 上,这项工作开始正常,并且日志读取

$ conda env create -f environment.yml
Collecting package metadata (repodata.json): ...working... done
Solving environment: ...working... done


==> WARNING: A newer version of conda exists. <==
  current version: 4.7.10
  latest version: 4.7.11
Run Code Online (Sandbox Code Playgroud)

好的,所以我使用的是conda4.4 之后的版本,所以conda activate应该可以工作。但是,作业失败并显示以下内容:

# To activate this environment, use
#
#     $ conda activate my-env
#
# To deactivate an active environment, use
#
#     $ conda deactivate

$ conda activate my-env

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

    $ conda init <SHELL_NAME>
Run Code Online (Sandbox Code Playgroud)

然后我尝试编辑我的.gitlab-ci.yml文件,以便有一个命令

conda init bash
Run Code Online (Sandbox Code Playgroud)

但随后收到消息

==> For changes to take effect, close and re-open your current shell. <==
Run Code Online (Sandbox Code Playgroud)

如何在 gitlab CI 过程中激活我的 conda 环境?

Tom*_*mmy 13

conda init触及.bashrc文件。要重新初始化外壳,您可以使用它:

  - conda create --name myenv
  - conda init bash
  - source ~/.bashrc    # <- !!!
  - conda activate myenv 
Run Code Online (Sandbox Code Playgroud)

source activate myenv我猜这比单独讨论好还是坏。


小智 5

与汤米的回答类似,这也需要为 Windows Powershell 完成。与 bash 相反,conda activate myenv在 powershell 中不会失败。没有调用就没有任何效果(即环境没有切换),conda init powershell这使得它更加尴尬。在 powershell 中重新加载配置文件更加复杂,因为其中有六个 [1]。我用了:

 - conda create --name myenv
 - conda init powershell
 - "if (test-path $PROFILE.CurrentUserAllHosts) { & $PROFILE.CurrentUserAllHosts}"
 - conda activate myenv 
Run Code Online (Sandbox Code Playgroud)

为什么 Conda 使用 $PROFILE.CurrentUserAllHosts问题 [2] 中已询问配置文件。

参考:

[1] https://devblogs.microsoft.com/scripting/understanding-the-six-powershell-profiles/

[2] https://github.com/conda/conda/issues/8608