'How to activate a conda environment on Jenkins

I've seen this question, but I am running on RHEL 7.3, not Windows.

With this simple Jenkins shell script:

#!/bin/bash
echo $PATH
echo $HOME
source /app/local/anaconda3/bin/activate py35_myenv

I am getting this failure:

$ /bin/bash /tmp/jenkins5945453203311093000.sh
/sbin:/usr/sbin:/bin:/usr/bin:/opt/teradata/client/15.10/bin:/app/local/anaconda3/bin:/opt/teradata/client/15.10/bin:/app/local/anaconda3/bin
/home/jenkins
/tmp/jenkins5945453203311093000.sh: line 4: /app/local/anaconda3/bin/activate: Permission denied

I tried chmoding the /app/local/anaconda3/bin/activate file to 664 (was originally 660), but that did not help. Also tried chmod -R o+rx /app/local/anaconda3/envs/py35_myenv (executables under there were originally 770, now 775). That also did not help. The reason I tried that is because I am getting "Permission denied" complaints and the jenkins user on this machine does not belong to the group that these Anaconda files are in.

I tried adding #!/bin/bash to the beginning of my Jenkins script, per the suggestion here, but that did not help.

Regular, non-Jenkins users, can activate this conda environment just fine. I am trying to have Jenkins run automated scripts out of this environment but I cannot activate it within a Jenkins job.

EDIT: If I do not provide the full path to activate, I get "activate: No such file or directory" complaints.

EDIT: The reason activate is given a chmod of 664 (non-executable) is stated on this answer. activate must be non-executable and run via source so it can make changes to the environment.

Jenkins version: 2.129-1.1



Solution 1:[1]

Add the following lines to the Executed shell in Jenkins because bash shell does not support conda init.

export PATH=/path/to/anaconda3/bin:$PATH # modify this path 
eval "$(conda shell.bash hook)"
conda activate your_env # change your_env based on your env name

Solution 2:[2]

Hide the unnecessary output if not needed:

steps {
            sh """
                #!/usr/bin/env bash
                set +x
                source /opt/conda/etc/profile.d/conda.sh
                conda activate env-p38
                echo $PATH
                set -x
                """
  }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tiến Hào Phùng
Solution 2 Vadiraj k.s