From 024a9798b3720f348fe5bbe970c235d24472fbcd Mon Sep 17 00:00:00 2001 From: Jim Nicholson Date: Tue, 13 Feb 2024 22:15:52 -0800 Subject: [PATCH] Add python stage helper --- README.md | 13 ++++++++++ vars/pythonVenvStage.groovy | 51 +++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 README.md create mode 100644 vars/pythonVenvStage.groovy diff --git a/README.md b/README.md new file mode 100644 index 0000000..82842fc --- /dev/null +++ b/README.md @@ -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 diff --git a/vars/pythonVenvStage.groovy b/vars/pythonVenvStage.groovy new file mode 100644 index 0000000..18e5710 --- /dev/null +++ b/vars/pythonVenvStage.groovy @@ -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() + } + } +}