52 lines
1.3 KiB
Groovy
52 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',
|
|
name: '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.name) {
|
|
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()
|
|
}
|
|
}
|
|
}
|