This commit is contained in:
Ramón Vásquez 2025-10-29 16:00:07 -03:00
parent aafb9b5b0c
commit 9dd7d2ca73

73
Jenkinsfile vendored
View file

@ -1,53 +1,52 @@
pipeline { pipeline {
agent any agent any
stages { stages {
stage('Checkout') { stage('Checkout') {
steps { steps {
checkout scm checkout scm
} }
} }
stage('Build Image') { stage('Build Image') {
steps { steps {
echo 'Building image...' echo 'Building image...'
// --pull ensures base layer is fresh; tag includes BUILD_NUMBER to avoid cache clashes // --pull ensures base layer is fresh; tag includes BUILD_NUMBER to avoid cache clashes
sh 'docker build --pull -t my-python-app:${BUILD_NUMBER} .' sh 'docker build --pull -t my-python-app:${BUILD_NUMBER} .'
} }
} }
stage('Run Container') { stage('Run Container') {
steps { steps {
echo 'Running container...' echo 'Running container...'
// --rm cleans up; this will print "Hello, World!" to the console // --rm cleans up; this will print "Hello, World!" to the console
sh 'docker run --rm --name my-python-app-${BUILD_NUMBER} my-python-app:${BUILD_NUMBER}' sh 'docker run --rm --name my-python-app-${BUILD_NUMBER} my-python-app:${BUILD_NUMBER}'
} }
} }
stage('(Optional) Verify Output') { stage('(Optional) Verify Output') {
steps { steps {
script { script {
// Re-run capturing output to assert expected behavior (example) // Re-run capturing output to assert expected behavior (example)
def out = sh( def out = sh(
script: 'docker run --rm my-python-app:${BUILD_NUMBER}', script: 'docker run --rm my-python-app:${BUILD_NUMBER}',
returnStdout: true returnStdout: true
).trim() ).trim()
if (out != 'Hello, World!') { if (out != 'Hello, World!') {
error "Unexpected output: '${out}'" error "Unexpected output: '${out}'"
} }
}
}
} }
}
} }
}
post { post {
always { always {
// Best-effort cleanup of any leftover container/image for this build tag // Best-effort cleanup of any leftover container/image for this build tag
sh ''' sh '''
docker rm -f my-python-app-${BUILD_NUMBER} 2>/dev/null || true docker rm -f my-python-app-${BUILD_NUMBER} 2>/dev/null || true
docker image rm my-python-app:${BUILD_NUMBER} 2>/dev/null || true docker image rm my-python-app:${BUILD_NUMBER} 2>/dev/null || true
''' '''
}
} }
}
} }
``