Skip to content

Instantly share code, notes, and snippets.

@PatrickChildersIT
Last active November 12, 2024 04:47
Show Gist options
  • Select an option

  • Save PatrickChildersIT/2b067769ee55988092b00d8c413967db to your computer and use it in GitHub Desktop.

Select an option

Save PatrickChildersIT/2b067769ee55988092b00d8c413967db to your computer and use it in GitHub Desktop.
Jenkinsfile definition for a python service that will automatically generate a requirements.txt file from the pyproject.toml file using poetry, in a container, before building the service's Dockerfile and uploading it.
pipeline {
agent none
stages {
stage("Clone & Prepare")
{
agent {
docker {
image 'python:latest'
registryUrl "https://index.docker.io/v1/"
}
}
stages {
stage("Clone repository") {
steps {
checkout scm
}
}
stage("Install Poetry") {
steps {
sh "pip install poetry"
sh "poetry self add poetry-plugin-export"
sh "poetry install --only main"
}
}
stage("Generate Requirements")
{
steps {
sh "poetry export -f requirements.txt --output requirements.txt --only main"
}
}
}
post
{
cleanup{
sh "pip uninstall poetry --yes"
}
}
}
stage("Build & Push Docker Image")
{
environment {
APPLICATION_NAME = env.GIT_URL.replaceFirst(/^.*\/([^\/]+?).git$/, '$1')
}
agent any
steps {
script {
def app
app = docker.build("${APPLICATION_NAME}")
docker.withRegistry("${env.MYDOCKER_REGISTRY_URL}") {
app.push("0.0.${env.BUILD_ID}")
app.push("latest")
}
}
}
}
}
}
@PatrickChildersIT
Copy link
Author

re-defining the docker url was necessary because without it Jenkins picked up the url from my local repository in the "Build & Push Docker Image" stage and using that for some reason
another important piece of information is that the repository should not have a checked-in requirements.txt file otherwise that second main stage which will re-pull the git repository will overwrite the generated requirements file with the source control requirements file, and if you forgot to re-generate it for source control then the un-updated version will end up in the docker image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment