0

I'm running Conda 4.6.14 on Ubuntu 16.04.

I thought about using virtual environments with Conda in my Jenkins pipelines that needed Ansible, by having all nodes use the same environment, ansible-env to run playbooks. While the process seems pretty straight forward, I'm unable to call conda activate ansible-env, because it results in the following error:

+ conda activate ansible-env

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'. To initialize your shell, run

    $ conda init <SHELL_NAME>

Currently supported shells are:
  - bash
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.

Here is the code I'm using to activate...pretty basic

sh("conda activate ansible-env")
sh("ansible --version")
sh("conda deactivate")

I ran conda init --system on the build node as the Jenkins user and then rebooted. No change.

I've spent a great deal of time trying to google a solution, but everything I've tried hasn't worked.

What I've attempted:

  • Tried running sh("conda init bash") in the pipeline.
  • Executing . /home/jenkins/miniconda3/profile.d/conda.sh from within the pipeline.
  • Made sure the bin directory for miniconda3 is set in $PATH.
  • Ran dpkg-configure dash to make sure /bin/bash is the default shell on the node.
  • Tried sourcing the /home/jenkins/.bashrc file
  • Calling the conda executable via the absolute path.
  • Tried using source ansible-env or activate ansible-env without conda.

I'm pretty sure the issue has to do with the following lines added to ~/.bashrc of the jenkins user after running conda init --system, but still not being accessible from that same user when a job is running. Unfortunately, I've hit a wall and not sure where to go from here.

NOTE: Please don't suggest switching to VirtuanEnv unless there's a specific reason Conda won't work with Jenkins. I'm aware of that route, but went with conda for...reasons.

1

What we do is download and install miniconda in the first step in our (declarative) pipeline and create the environment(s) from a yaml file.

sh '''#!/usr/bin/env bash
wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -nv -O miniconda.sh
bash miniconda.sh -b -p $WORKSPACE/miniconda
conda config --set always_yes yes --set changeps1 no
conda update -q conda

conda env create -f envs/ansible-env.yaml
'''

In each step we start the conda environment like so:

sh '''#!/usr/bin/env bash
source $WORKSPACE/miniconda/etc/profile.d/conda.sh
conda activate miniconda/envs/ansible-env/

# do stuff
'''
  • Thank you for this suggestion, @Siebren! I decided to take an entirely different approach, but I wanted to mention that I completely overlooked using sh() with multiple lines. That would have likely solved my original problem, so I'm going to mark this as the accepted answer for that reason. – Argyle Feb 14 at 15:11
0

As a suggestion... Can you try calling the absolute path of the python executable in the environment? I mean: when you change conda environments, what happens is that the python executable you call changes path; you can find which python executable would be executed by

which python

Inside the environment. Then just forget about calling conda in the Jenkins pipeline. That should work if that’s what you’re changing environments for. Or are you changing the conda environment for some other reason?

EDIT: the same would happen when calling ansible as a library installed in the environment. It will change path to something like:

~/conda/envs/your_env/lib/ansible

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.