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: "[email protected]"
            }
        }
        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)

3

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.