使用Shell Executor在GitLab配置yml文件中激活CICD的Conda环境

2024-04-24 04:36:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在Gitlab CI-CD进程中激活conda环境。 我在本地计算机(UNIX)上向Shell Executor注册了Gitlab runner(v13.10),与我的工作笔记本电脑不同 我试图通过repo中存在的环境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

我提到了各种问题,如herehere。此外,我还尝试将anaconda路径添加到bash文件中,作为环境路径变量。但我不确定我是否做对了

我的问题是:

  1. 既然它是在shell executor上运行的,而且我已经让conda运行了,为什么它不能接收它呢。如何在GitLab配置文件中修复此问题
  2. 我对docker图像的使用有一个限制,我想继续使用Shell executor

Tags: 文件gitbuildenvenvironment环境ymlgitlab
1条回答
网友
1楼 · 发布于 2024-04-24 04:36:25

一种对我有效的方法是使用conda docker映像,并执行命令conda init bashsource ~/.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

相关问题 更多 >