在gitlab CI中激活conda环境

时间:2019-09-02 09:17:47

标签: python continuous-integration anaconda gitlab 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/.

在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

好吧,所以我使用的conda版本晚于4.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>

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

conda init bash

但随后收到消息

==> For changes to take effect, close and re-open your current shell. <==

如何在gitlab CI流程中激活conda环境?

4 个答案:

答案 0 :(得分:3)

与Tommy的回答类似,Windows Powershell也需要这样做。与bash SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession(); try{ LOG.info("Records Deleted: "+sqlSession.getMapper(Mapper.class).deleteEntry(userId,appId)); } finally { sqlSession.close(); } 相反,在powershell中不会失败。如果不调用RewriteEngine On RewriteCond %{HTTPS} off # First rewrite to HTTPS: # Don't put www. here. If it is already there it will be included, if not # the subsequent rule will catch it. RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] # Now, rewrite any request to the wrong domain to use www. # [NC] is a case-insensitive match RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] ,它就没有任何效果(即不切换环境),这会使它更加尴尬。在Powershell中重新加载配置文件更为复杂,因为其中有六个[1]。我用过:

conda activate myenv

在问题[2]中,为何要求Conda使用conda init powershell个人资料。

参考:

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

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

答案 1 :(得分:2)

line = line[:line.index("#")] if "#" in line else line触摸conda init文件。要重新初始化外壳,您可以获取它:

.bashrc

我想这是比 - conda create --name myenv - conda init bash - source ~/.bashrc # <- !!! - conda activate myenv 好还是坏是一个单独的讨论。

答案 2 :(得分:0)

您可能会发现更简洁优雅的另一种可能性:直接获取 conda 与 bash 一起运行所需的代码。如果不是这种情况,这也具有将 conda 添加到 PATH 的效果。

这是通过

- source <anaconda_root>/etc/profile.d/conda.sh
- conda activate myenv

(从 https://stackoverflow.com/a/60523131/11343 窃取)

答案 3 :(得分:0)

不知何故,所有这些答案都让我失望。我最终使用 conda run 而不是激活环境。这使我可以在不激活环境的情况下运行 pytest

- conda run -n <environment-name> python -m pytest <addl-pytest-args>