tony_tiger
tony_tiger

Reputation: 799

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

Upvotes: 2

Views: 8209

Answers (3)

Vadiraj k.s
Vadiraj k.s

Reputation: 59

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
                """
  }

Upvotes: 0

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

Upvotes: 3

Oresztesz
Oresztesz

Reputation: 2430

chmod 664 does not give you execute permission. You can explicitly specify in a chmod command which privileges do you want to grant or revoke. It's a bit more readable. Try changing your script to the following:

#!/bin/bash
echo $PATH
echo $HOME
/app/local/anaconda3/bin/activate py35_myenv
chmod ug+x /app/local/anaconda3/bin/activate

You can use https://chmodcommand.com to verify it the command does what you're expecting.

Also, it can be an issue if jenkins user does not have access to one of the script's parent folder. Try to set the same permissions to parent folders as well. The following commands are from your code snippet in the comments:

sudo find . -perm 770 -exec chmod o+rx {}; \
sudo find . -perm 750 -exec chmod o+rx {}; \
sudo find . -perm 660 -exec chmod o+r {}; \
sudo find . -perm 640 -exec chmod o+r {}

Upvotes: 1

Related Questions