Updated new Jenkinsfile
This commit is contained in:
116
Jenkinsfile
vendored
116
Jenkinsfile
vendored
@@ -1,130 +1,20 @@
|
|||||||
pipeline {
|
pipeline {
|
||||||
agent { label 'jenkins-agent' }
|
agent { label 'jenkins-agent' }
|
||||||
|
|
||||||
|
|
||||||
environment {
|
|
||||||
COMPOSE_PROJECT_NAME = "${env.JOB_NAME}-${env.BUILD_NUMBER}"
|
|
||||||
DOCKER_COMPOSE_FILE = 'docker-compose.yml'
|
|
||||||
}
|
|
||||||
|
|
||||||
stages {
|
stages {
|
||||||
stage('Cleanup') {
|
|
||||||
steps {
|
|
||||||
// Clean workspace before build
|
|
||||||
cleanWs()
|
|
||||||
// Stop any existing containers
|
|
||||||
sh 'docker-compose down --remove-orphans || true'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
stage('Build') {
|
stage('Build') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
sh 'docker-compose build'
|
||||||
try {
|
|
||||||
sh 'docker-compose build --no-cache'
|
|
||||||
} catch (err) {
|
|
||||||
error "Failed to build Docker images: ${err}"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Test') {
|
stage('Test') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
sh 'docker-compose run --rm app python -m pytest'
|
||||||
try {
|
|
||||||
// Run tests and capture the output
|
|
||||||
sh '''
|
|
||||||
docker-compose run --rm app python -m pytest \
|
|
||||||
--junitxml=test-results/junit.xml \
|
|
||||||
--cov=app \
|
|
||||||
--cov-report=xml:coverage-reports/coverage.xml
|
|
||||||
'''
|
|
||||||
} catch (err) {
|
|
||||||
error "Tests failed: ${err}"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
post {
|
|
||||||
always {
|
|
||||||
// Publish test results even if tests fail
|
|
||||||
junit allowEmptyResults: true, testResults: 'test-results/*.xml'
|
|
||||||
// Publish coverage reports
|
|
||||||
publishCoverage adapters: [coberturaAdapter('coverage-reports/coverage.xml')]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Security Scan') {
|
|
||||||
steps {
|
|
||||||
script {
|
|
||||||
try {
|
|
||||||
// Run security scanning on Docker images
|
|
||||||
sh 'docker scan app:latest || true'
|
|
||||||
} catch (err) {
|
|
||||||
// Don't fail the build but warn about security issues
|
|
||||||
warning "Security scan found issues: ${err}"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
stage('Deploy') {
|
stage('Deploy') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
sh 'docker-compose up -d'
|
||||||
try {
|
|
||||||
// Take backup of current deployment state
|
|
||||||
sh 'docker-compose ps > deployment-state.txt || true'
|
|
||||||
|
|
||||||
// Deploy with health check
|
|
||||||
sh '''
|
|
||||||
docker-compose up -d
|
|
||||||
# Wait for containers to be healthy
|
|
||||||
./scripts/wait-for-healthy.sh 300
|
|
||||||
'''
|
|
||||||
} catch (err) {
|
|
||||||
// If deployment fails, attempt rollback
|
|
||||||
error "Deployment failed: ${err}"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
post {
|
|
||||||
always {
|
|
||||||
script {
|
|
||||||
// Cleanup resources regardless of build result
|
|
||||||
sh '''
|
|
||||||
# Save logs before cleanup
|
|
||||||
mkdir -p logs
|
|
||||||
docker-compose logs > logs/docker-compose.log || true
|
|
||||||
|
|
||||||
# Cleanup
|
|
||||||
docker-compose down --remove-orphans || true
|
|
||||||
docker system prune -f || true
|
|
||||||
'''
|
|
||||||
|
|
||||||
// Archive logs and deployment state
|
|
||||||
archiveArtifacts artifacts: 'logs/*, deployment-state.txt', allowEmptyArchive: true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
success {
|
|
||||||
echo 'Pipeline completed successfully!'
|
|
||||||
// Add notifications for success (email, Slack, etc.)
|
|
||||||
}
|
|
||||||
failure {
|
|
||||||
echo 'Pipeline failed!'
|
|
||||||
// Add notifications for failure (email, Slack, etc.)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
options {
|
|
||||||
// Set timeout for entire pipeline
|
|
||||||
timeout(time: 1, unit: 'HOURS')
|
|
||||||
// Keep build logs and artifacts for 10 builds
|
|
||||||
buildDiscarder(logRotator(numToKeepStr: '10'))
|
|
||||||
// Don't run concurrent builds
|
|
||||||
disableConcurrentBuilds()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user