pipelineUtils/vars/pythonVenvStage.groovy

64 lines
1.9 KiB
Groovy

i
void call(Map config = [:], Closure body) {
defaultConfig = [
pythonPath: 'python3',
recreateVenv: false,
venvDir: '.venv',
updatePip: true,
installPipTools: true,
requirementsFile: 'requirements.in',
stepName: 'python'
]
runConfig = defaultConfig + config
venvDir = [
env.WORKSPACE,
runConfig.venvDir
]
venvBinDir = venvDir + 'bin'
venvPython = venvBinDir + 'python'
venvEnv = [
"VIRTUAL_ENV=${venvDir.join('/')}",
"PATH+CUSTOM=${venvBinDir.join('/')}"
]
stage(runConfig.stepName) {
printBanner(runConfig.stepName)
if (fileExists(runConfig.venvDir) && runConfig.recreateVenv) {
echo 'Removing old venv...'
dir(runConfig.venvDir) {
deleteDir()
}
}
if (!fileExists(runConfig.venvDir)) {
createVenv = "${runConfig.pythonPath} -m venv ${runConfig.venvDir}"
echo "python create venv cmd is: ${createVenv}"
sh createVenv
}
withEnv(venvEnv) {
if (runConfig.updatePip) {
updateCmd = "${venvPython.join('/')} -m pip install --upgrade pip"
echo "Updating pip with (${updateCmd})"
echo "Environment is ${venvEnv}"
sh updateCmd
}
if (runConfig.installPipTools) {
installCmd = "${venvPython.join('/')} -m pip install pip-tools"
echo "Installing pip-tools with (${installCmd})"
sh installCmd
}
if (fileExists(runConfig.requirementsFile)) {
reqCmd = "pip-compile ${runConfig.requirementsFile}"
echo "Collecting requirements with (${reqCmd})"
sh reqCmd
}
echo 'Executing closure...'
body()
}
}
}