Search code examples
pythonanacondagitlabcondagitlab-ci-runner

Activating Conda environment in GitLab Config yml file for CI-CD using Shell Executor


I want to activate conda environment in my Gitlab CI-CD process. I registered Gitlab runner (v13.10) with Shell Executor on a local machine (UNIX) different from my work laptop I am trying to activate a conda environment through an environment yml file present in my repo but it fails and says conda command not found!

I edited the .gitlab-ci.yml file something like this:

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

The problem I am facing is it throws an error: CONDA Command NOT FOUND

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

I referred to various questions posted like here and here. Also, I have tried adding the anaconda path to the bash file for the environment path variable. But I am not sure If I am doing it correctly

My questions being:

  1. Since it's running on shell executor and I already have conda running, why is it not able to pick it up. How do I fix this in my GitLab config file
  2. I have a limitation on the usage of docker images for my purpose and want to stick with the Shell executor

Solution

  • An approach that works for me is to use a conda docker image, and execute the commands conda init bash, source ~/.bashrc and conda activate env_name. This would be the content of the .gitlab-ci.yml file:

    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