0

I'm using declarative pipeline for jenkins and my goal is to have a separated virtual environment for each project in jenkins/workspace. I've created a new pipeline with the following code:

pipeline {
    agent any
    stages {
        stage('Pull all changes') {
            steps {
                git branch: "master", url: "git@fancylink.git"
            }
        }
        stage('Create and Activate venv') {
            steps {
                sh 'python3.11 -m venv venv'
                sh 'source venv/bin/activate'
            }
        }
    }
    post {
        always {
            sh 'deactivate' 
        }
    }
}

However I'm getting this error: enter image description here

When I try to do the same steps via SSH everything is working fine and venv is activated. Also I don't know if it's important but I have 2 version of the python on the server (python3.9, python3.11)

Arrstad
  • 100
  • 6
  • Does this answer your question? [source command not found in sh shell](https://stackoverflow.com/questions/13702425/source-command-not-found-in-sh-shell) – Azeem Mar 15 '23 at 08:22
  • Relevant: https://stackoverflow.com/questions/12455932/what-shell-does-jenkins-use – Azeem Mar 15 '23 at 08:22
  • See this for running bash commands: https://stackoverflow.com/questions/44330148/run-bash-command-on-jenkins-pipeline – Azeem Mar 15 '23 at 08:23

1 Answers1

1

Availability of source depends on the actual shell jenkins uses. As mentioned in the comments, you probably should use . instead of source.

However; activation and deactivation of venv is related to the scope of sh.

If you have multiple steps and multiple sh's you need to activate the venv for each of those where you need to access python libs/tools installed into venv. This is because activation is applied the current shell and if you activate it on one sh, once that stops, venv settings are automatically deactivated because next sh will spawn a new shell.

rasjani
  • 7,372
  • 4
  • 22
  • 35