2025-10-29 13:11:30 -03:00
|
|
|
pipeline {
|
2025-10-29 16:00:07 -03:00
|
|
|
agent any
|
2025-10-29 15:59:47 -03:00
|
|
|
|
2025-10-29 16:00:07 -03:00
|
|
|
stages {
|
|
|
|
|
stage('Checkout') {
|
|
|
|
|
steps {
|
|
|
|
|
checkout scm
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-29 15:59:47 -03:00
|
|
|
|
2025-10-29 16:00:07 -03:00
|
|
|
stage('Build Image') {
|
|
|
|
|
steps {
|
|
|
|
|
echo 'Building image...'
|
|
|
|
|
// --pull ensures base layer is fresh; tag includes BUILD_NUMBER to avoid cache clashes
|
|
|
|
|
sh 'docker build --pull -t my-python-app:${BUILD_NUMBER} .'
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-29 15:59:47 -03:00
|
|
|
|
2025-10-29 16:00:07 -03:00
|
|
|
stage('Run Container') {
|
|
|
|
|
steps {
|
|
|
|
|
echo 'Running container...'
|
|
|
|
|
// --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}'
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-29 15:59:47 -03:00
|
|
|
|
2025-10-29 16:00:07 -03:00
|
|
|
stage('(Optional) Verify Output') {
|
|
|
|
|
steps {
|
|
|
|
|
script {
|
|
|
|
|
// Re-run capturing output to assert expected behavior (example)
|
|
|
|
|
def out = sh(
|
2025-10-29 15:59:47 -03:00
|
|
|
script: 'docker run --rm my-python-app:${BUILD_NUMBER}',
|
|
|
|
|
returnStdout: true
|
|
|
|
|
).trim()
|
2025-10-29 16:00:07 -03:00
|
|
|
if (out != 'Hello, World!') {
|
|
|
|
|
error "Unexpected output: '${out}'"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-29 13:11:30 -03:00
|
|
|
}
|
2025-10-29 15:59:47 -03:00
|
|
|
}
|
|
|
|
|
|
2025-10-29 16:00:07 -03:00
|
|
|
post {
|
|
|
|
|
always {
|
|
|
|
|
// Best-effort cleanup of any leftover container/image for this build tag
|
|
|
|
|
sh '''
|
2025-10-29 15:59:47 -03:00
|
|
|
docker rm -f my-python-app-${BUILD_NUMBER} 2>/dev/null || true
|
|
|
|
|
docker image rm my-python-app:${BUILD_NUMBER} 2>/dev/null || true
|
|
|
|
|
'''
|
2025-10-29 16:00:07 -03:00
|
|
|
}
|
2025-10-29 13:11:30 -03:00
|
|
|
}
|
|
|
|
|
}
|