pipelineUtils/vars/pythonVenvStage.groovy

68 lines
2.2 KiB
Groovy

void call(Map config = [:], Closure body) {
defaultConfig = [
pythonPath: 'python3',
recreateVenv: true,
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)
ansiColor('css') {
if (fileExists(runConfig.venvDir) && runConfig.recreateVenv) {
echo 'Info: Removing old venv from workspace.'
dir(runConfig.venvDir) {
deleteDir()
}
}
if (!fileExists(runConfig.venvDir)) {
echo "Info: Creating new venv in ${runConfig.venvDir}."
sh "${runConfig.pythonPath} -m venv ${runConfig.venvDir}"
}
withEnv(venvEnv) {
List setupCmds = []
if (runConfig.updatePip) {
echo 'Info: Will update pip if an update is available.'
updateCmd = "${venvPython.join('/')} -m pip install --upgrade pip"
setupCmds.add(updateCmd)
}
if (runConfig.installPipTools) {
echo 'Info: Will install pip-tools.'
installCmd = "${venvPython.join('/')} -m pip install pip-tools"
setupCmds.add(installCmd)
}
if (fileExists(runConfig.requirementsFile)) {
echo "Info: Will resolve requirements from ${runConfig.requirementsFile}."
reqCmd = "pip-compile ${runConfig.requirementsFile}"
setupCmds.add(reqCmd)
}
if (!setupCmds.isEmpty()) {
echo 'Info: Beginning venv setup.'
sh setupCmds.join('\n')
}
echo 'Executing closure...'
body()
}
}
}
}