Latest Jenkinsfile

This commit is contained in:
Lihan 2024-07-27 01:52:53 -04:00
parent 40819bc102
commit ce92d0bca2

46
Jenkinsfile vendored
View file

@ -10,26 +10,33 @@ pipeline {
stages { stages {
stage('Checkout') { stage('Checkout') {
steps { steps {
git url: 'https://github.com/CChariot/spring-petclinic.git', branch: 'FinalProject_main', credentialsId: 'github-token' script {
echo "Checking out code..."
git url: 'https://github.com/CChariot/spring-petclinic.git', branch: 'FinalProject_main', credentialsId: 'github-token'
}
} }
} }
stage('Build Docker Image') { stage('Build Docker Image') {
steps { steps {
script { script {
echo "Building Docker Image..."
dockerImage = docker.build("spring-petclinic") dockerImage = docker.build("spring-petclinic")
echo "Docker Image built: ${dockerImage.id}"
} }
} }
} }
stage('SonarQube Analysis') { stage('SonarQube Analysis') {
steps { steps {
withSonarQubeEnv('SonarQube') { script {
script { echo "Starting SonarQube analysis..."
withSonarQubeEnv('SonarQube') {
dockerImage.inside("-u root") { dockerImage.inside("-u root") {
sh './mvnw sonar:sonar -Dsonar.projectKey=spring-petclinic' sh './mvnw sonar:sonar -Dsonar.projectKey=spring-petclinic'
} }
} }
echo "SonarQube analysis completed."
} }
} }
} }
@ -37,9 +44,11 @@ pipeline {
stage('Build Application') { stage('Build Application') {
steps { steps {
script { script {
echo "Building application..."
dockerImage.inside("-u root") { dockerImage.inside("-u root") {
sh './mvnw clean package -DskipTests' sh './mvnw clean package -DskipTests'
} }
echo "Application build completed."
} }
} }
} }
@ -47,32 +56,46 @@ pipeline {
stage('Run Application') { stage('Run Application') {
steps { steps {
script { script {
echo "Running application..."
dockerImage.inside("-u root") { dockerImage.inside("-u root") {
sh 'java -jar target/*.jar' sh 'java -jar target/*.jar'
} }
echo "Application is running."
} }
} }
} }
stage('OWASP ZAP') { stage('OWASP ZAP') {
steps { steps {
sh ''' script {
docker run --rm -v $(pwd)/zap-report:/zap/wrk:rw \ echo "Running OWASP ZAP..."
owasp/zap2docker-stable zap-baseline.py -t http://localhost:8080 \ sh '''
-g gen.conf -r zap-report.html docker run --rm -v $(pwd)/zap-report:/zap/wrk:rw \
''' owasp/zap2docker-stable zap-baseline.py -t http://localhost:8080 \
-g gen.conf -r zap-report.html
'''
echo "OWASP ZAP analysis completed."
}
} }
} }
stage('Publish ZAP Report') { stage('Publish ZAP Report') {
steps { steps {
publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: 'zap-report', reportFiles: 'zap-report.html', reportName: 'OWASP ZAP Report']) script {
echo "Publishing OWASP ZAP report..."
publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: 'zap-report', reportFiles: 'zap-report.html', reportName: 'OWASP ZAP Report'])
echo "OWASP ZAP report published."
}
} }
} }
stage('Deploy to Production') { stage('Deploy to Production') {
steps { steps {
sh 'ansible-playbook -i inventory/production deploy.yml' script {
echo "Deploying to production..."
sh 'ansible-playbook -i inventory/production deploy.yml'
echo "Deployment to production completed."
}
} }
} }
} }
@ -82,6 +105,7 @@ pipeline {
script { script {
try { try {
if (dockerImage != null) { if (dockerImage != null) {
echo "Stopping and removing Docker Image..."
dockerImage.stop() dockerImage.stop()
dockerImage.remove() dockerImage.remove()
} }
@ -97,4 +121,4 @@ pipeline {
echo 'Pipeline failed.' echo 'Pipeline failed.'
} }
} }
} }