Add python stage helper

This commit is contained in:
Jim Nicholson 2024-02-13 22:15:52 -08:00
parent d33eceddc2
commit 024a9798b3
2 changed files with 64 additions and 0 deletions

13
README.md Normal file
View File

@ -0,0 +1,13 @@
# pipelineUtils
This project is an attempt to provide a structured set of functions that can be used to create a pipeline library for the Jenkins CI/CD system.
## Overview
Functions provided fall into several categories:
- "stage" helpers that implment a stage in the pipeline with particular behavior.
- "versioning" helpers that implement a particular strategy for versioning a project.
- "utiltity" functions that help with various things, such as printing banners in the job console log, incrementing a semantic version string, and adding git meta information to a pipeline's metadata.
## Example

View File

@ -0,0 +1,51 @@
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()
}
}
}