Python Forum

Full Version: JenkinsFile to create a Python venv
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I am working to create a new pipeline to run test cases from my Python repository in Jenkins. have created a 'Jenkinsfile' defining the different stages that I would need, however I'm unable to create a virtual environment for Python to run.
Can someone help what i am missing?

#!/usr/bin/env groovy

properties([
	pipelineTriggers([
		pollSCM('*/1 * * * *')
	]),
	disableConcurrentBuilds()
])

node {
	env.GIT_PROJECT_NAME = "*******"					
	//PYTHON_VERSION = "3.12"  // Specify the Python version
	VENV_DIR = "venv"       // Directory for the virtual environment
	ALLURE_RESULTS_DIR = "allure-results" // Directory for Allure results	 	
	
	// Credentials for TFSFSService SSH user
	def GIT_CREDENTIALS_ID = '*******'

	def branchName = "${env.BRANCH_NAME}"
	
	try {
		stage ('Checkout') {
	        git(
				poll: true,
				url: "[email protected]:v3/d***d/***/${env.GIT_PROJECT_NAME}",
				credentialsId: GIT_CREDENTIALS_ID,
				branch: branchName
	        )			
		}
		stage('Setup Python Environment') {																
			// Create a virtual environment
			echo 'python --version'
			
			bat 'python3 -m venv ${VENV_DIR}' 
			bat '${VENV_DIR}\\bin\\activate'
		}
	}
	catch (e) {		
		throw e	        
	}
}
Getting the below error currently:

Error:
[Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Setup Python Environment) [Pipeline] echo python --version [Pipeline] bat C:\Users\Jenkins\AppData\Local\Jenkins\.jenkins\workspace\st_thymeit-auto-base_development>python -m venv ${VENV_DIR} Actual environment location may have moved due to redirects, links or junctions. Requested location: "C:\Users\Jenkins\AppData\Local\Jenkins\.jenkins\workspace\st_######\${VENV_DIR}\Scripts\python.exe" Actual location: "D:\jenkins\workspace\st_####\${VENV_DIR}\Scripts\python.exe" [Pipeline] bat C:\Users\Jenkins\AppData\Local\Jenkins\.jenkins\workspace\st_thymeit-auto-base_development>${VENV_DIR}\bin\.activate The system cannot find the path specified. [Pipeline] } [Pipeline] // stage [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline ERROR: script returned exit code 1 Finished: FAILURE
There are packages available here. I haven't used any of them, so you will need to examine for usability.
(Sep-21-2024, 06:53 AM)Larz60+ Wrote: [ -> ]There are packages available here. I haven't used any of them, so you will need to examine for usability.

Thank you Larzo, but I'm looking for groovy script that can help me create a virtual environment in Jenkins. currently, I believe it's a path issue that Im unable to rectify.