pipelineUtils/vars/pythonVenvStage.groovy
2024-02-13 22:26:06 -08:00

53 lines
1.3 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=${venvBinDir.join('/')}"
]
stage(runConfig.stepName) {
printBanner(runConfig.stepName)
if (fileExists(runConfig.venvDir) && runConfig.recreateVenv) {
dir(runConfig.venvDir) {
deleteDir()
}
}
if (!fileExists(runConfig.venvDir)) {
sh "${runConfig.pythonPath} -m venv ${runConfig.venvDir}"
}
withEnv(venvEnv) {
if (runConfig.updatePip) {
sh "${venvPython} -m pip install --upgrade pip"
}
if (runConfig.installPipTools) {
sh "${venvPython} -m pip install pip-tools"
}
if (fileExists(runConfig.requirementsFile)) {
sh "pip-compile ${runConfig.requirementsFile}"
}
body()
}
}
}