user6754289
user6754289

Reputation: 437

Activate conda environment from docker image in Jenkinsfile

I'm trying to activate a conda environment in my Jenkinsfile, which was created from a Dockerbuild, but I can't figure out how to activate the environment in the shell script in my Jenkinsfile.

But this line . /opt/conda/envs/myapp-env/bin/activate fails on activating in my Jenkinsfile

Dockerfile

FROM continuumio/miniconda3:latest

WORKDIR /tmp/app

COPY environment.yml environment.yml

#missing dependencies

RUN conda config --add channels conda-forge \ 
  && conda env create -n myapp-env -f environment.yml \
  && rm -rf /opt/conda/pkgs/*

ENV PATH /opt/conda/envs/myapp-env/bin:$PATH

RUN echo $PATH
RUN conda env list

Jenkinsfile:

try {
        stage('Activate environment & Unit Test') {
            buildImage.inside {

                sh '''
                echo $PATH
                echo $HOME
                . /opt/conda/envs/myapp-env/bin/activate && python -m pytest tests --cov ./server --cov-report term-missing --cov-report xml --junitxml=build/results.xml
                '''
            }
        }

Result of echo $PATH and $HOME:

Running shell script
+ echo /opt/conda/envs/dfog-app/bin:/opt/conda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
/opt/conda/envs/myapp-env/bin:/opt/conda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
+ echo /

Upvotes: 3

Views: 3429

Answers (2)

OrangeDog
OrangeDog

Reputation: 38797

The error message you got was probably

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.

Because the continuumio/miniconda3:latest image sets everything up for the root user only, which is not what Jenkins runs as.

To be able to activate environments you need to first source /opt/conda/etc/profile.d/conda.sh.

However, you will probably then encounter all sorts of other permission problems, because conda does not work as any other user, and Jenkins does not work as root.

Upvotes: 3

Nehal J Wani
Nehal J Wani

Reputation: 16639

In your Dockerfile, there is no need to do:

ENV PATH /opt/conda/envs/myapp-env/bin:$PATH

Inside your Jenkinsfile, do:

source /opt/conda/etc/profile.d/conda.sh
conda activate myapp-env
python -m pytest tests --cov ./server --cov-report term-missing --cov-report xml --junitxml=build/results.xml

Upvotes: 1

Related Questions