python - 使用 Shell Executor 在 GitLab Config yml 文件中为 CI-CD 激活 Conda 环境

标签 python anaconda gitlab conda gitlab-ci-runner

我想在我的 Gitlab CI-CD 过程中激活 conda 环境。
我在不同于我的工作笔记本电脑的本地机器 (UNIX) 上使用 Shell Executor 注册了 Gitlab runner (v13.10)
我正在尝试通过我的存储库中存在的环境 yml 文件激活 conda 环境,但它失败并说未找到 conda 命令!
我编辑了 .gitlab-ci.yml 文件,如下所示:

stages:
  - build
build stage:
    stage: build
    before_script:
        - which python
        - export PIP_CACHE_DIR="/opt/cache/pip"
        - conda env create -f environment.yml
        - source activate env_work
    script:
        - echo "Building"
        - cd parent_dir
        - python main.py new_studies/first_study
    artifacts:
        paths:
            - out/
    only:
        - master
我面临的问题是它抛出一个错误:未找到 CONDA 命令
Running with gitlab-runner 13.10.0 (5421146)
  on rig ci runner gZzdceA
Preparing the "shell" executor
00:00
Using Shell executor...
Preparing environment
00:00
Running on rig-machine...
Getting source from Git repository
00:04
Fetching changes with git depth set to 50...
Reinitialized existing Git repository in /home/gitlab-runner/builds/gZzdceA/0/company/gg/product/repo/.git/
Checking out 883ga36 as master...
Skipping Git submodules setup
Executing "step_script" stage of the job script
00:00
$ export PIP_CACHE_DIR="/opt/cache/pip"
$ conda env create -f environment.yml
bash: line 120: conda: command not found
Cleaning up file based variables
00:00
ERROR: Job failed: exit status 1

我提到了像here这样的各种问题。和 here .另外,我尝试将 anaconda 路径添加到环境路径变量的 bash 文件中。但我不确定我是否做对了
我的问题是:
  • 既然它在 shell executor 上运行,而我已经在运行 conda,为什么它无法捡起它。如何在我的 GitLab 配置文件中修复此问题
  • 我对 docker 图像的使用有限制,并希望坚持使用 Shell 执行程序
  • 最佳答案

    对我有用的方法是使用 conda docker 镜像,并执行命令 conda init bash , source ~/.bashrcconda activate env_name .这将是 .gitlab-ci.yml 文件的内容:

    image: continuumio/miniconda3
    
    before_script:
        - apt-get update
        # install all required libraries using existing conda environment requirements file:
        - conda env create -f env_name.yml
        - conda init bash
        - source ~/.bashrc
        - conda activate env_name
       
    pages:
      stage: deploy
      script:
        # build and publish automated documentation with Sphinx:
        - PYTHONPATH=. sphinx-build -b html . public
      artifacts:
        paths:
        - public
      only:
      - master
    

    关于python - 使用 Shell Executor 在 GitLab Config yml 文件中为 CI-CD 激活 Conda 环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66811822/

    相关文章:

    python - 如何通过cloudpickle来pickle python-attrs类函数?

    python - 使用 Conda 创建像 Anaconda 这样的自定义安装程序

    python - 导入错误 PyQt5 python

    docker - Gitlab 中的软件包和注册表中缺少容器注册表

    python - 如何为 tkinter 框架添加阴影?

    python - 部署到heroku时TemplateDoesNotExist at/base/index.html

    python - 两个conda环境冲突

    git - 推送到 Git (GitLab) 时出现错误 401

    ssh - 在尝试添加 gitlab ci 以使用 SSH 执行程序构建我的项目时,我收到错误 "asn1: structure error: superfluous leading zeros in length"

    python - 在 ipdb session 中如何使用 list()?