Jenkins multi node Python environment isolation (Windows)

Time:2021-10-20

Jenkins multi node Python environment isolation (Windows)

  1. In the Jenkins construction process, multiple Jenkins nodes need to be used for concurrent construction
  2. Due to condition constraints, there is only one windows host, so multiple Jenkins nodes are deployed on this host
  3. Pip is used in shell commands to install dependencies. When multiple builds are concurrent, the dependencies may be changed
  4. Therefore, environmental isolation is required
  5. Only for special scenarios, GUI programs are built, and windows environment must be used. In other cases, docker is better, which is not discussed here

Reference documents:

https://docs.python.org/zh-cn…

Venv virtual environment using Python

pipeline {
    agent {
        label "Windows"
    }
    stages {
        stage('Build') {
            steps {
                //Create a venv directory under the current directory to place the virtual environment)
                sh 'python -m venv ./venv'

                //Python applications need to be executed in a virtual environment, that is, add ". / venv / scripts /"
                //In Linux is ". / venv / bin"
                sh './venv/Scripts/pip install pytest'
            }
        }
        stage('Test') {
            steps {
                sh './venv/Scripts/pytest'
            }
        }
    }
}
  • When Python has set the environment variable, you can use it directly
  • “Python – M venv. / venv” create
  • When there are different versions of python, the absolute path of the corresponding version is used
  • “C: \ users \ administrator \ appdata \ local \ programs \ Python \ Python 39 \ Python – M venv. / venv” create
  • Executing the command will overwrite the old environment (the dependency of PIP installation has not been deleted, so you need to test whether it has an impact)