diff --git a/Dockerfile b/Dockerfile index 9b27efd64..2dbfce412 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,7 +12,7 @@ COPY mvnw pom.xml ./ COPY src ./src # Package the application -RUN ./mvnw clean package +RUN ./mvnw clean package -DskipTests # Copy the JAR file to the app directory COPY target/*.jar app.jar diff --git a/Jenkinsfile b/Jenkinsfile index f339986f3..8ac23c6f0 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -2,48 +2,50 @@ pipeline { agent any environment { - DOCKER_CREDENTIALS_ID = 'your-docker-credentials-id' // Replace with your actual Docker credentials ID + SONARQUBE_URL = 'http://sonarqube:9000' + SONARQUBE_CREDENTIALS_ID = 'admin' + GITHUB_TOKEN = credentials('github-token') } stages { stage('Checkout') { - steps { - checkout scm - } - } - stage('Build') { steps { script { - sh './mvnw clean package' + echo "Checking out code..." + git url: 'https://github.com/CChariot/spring-petclinic.git', branch: 'FinalProject_main', credentialsId: 'github-token' } } } + stage('Build Docker Image') { steps { script { - // Build the Docker image - sh 'docker build -t petclinic:latest .' + echo "Building Docker Image..." + def dockerImage = docker.build("spring-petclinic") + echo "Docker Image built: ${dockerImage.id}" + // Store the Docker image ID in the environment if needed across stages + env.DOCKER_IMAGE_ID = dockerImage.id } } } - stage('Push Docker Image') { - steps { - script { - // Push the Docker image to a registry - docker.withRegistry('https://index.docker.io/v1/', "${DOCKER_CREDENTIALS_ID}") { - sh 'docker tag petclinic:latest your-docker-username/petclinic:latest' - sh 'docker push your-docker-username/petclinic:latest' - } + + } + + post { + always { + script { + // Use the saved Docker image ID from the environment if needed + if (env.DOCKER_IMAGE_ID) { + echo "Stopping and removing Docker Image with ID: ${env.DOCKER_IMAGE_ID}" + docker.rmi(env.DOCKER_IMAGE_ID) } } } - stage('Deploy') { - steps { - script { - // Deploy the application using Docker Compose - sh 'docker-compose up -d' - } - } + success { + echo 'Pipeline completed successfully!' + } + failure { + echo 'Pipeline failed.' } } } diff --git a/docker-compose_spring-petclinic.yml b/docker-compose_spring-petclinic.yml index 9b855b942..98c79abe3 100644 --- a/docker-compose_spring-petclinic.yml +++ b/docker-compose_spring-petclinic.yml @@ -36,7 +36,10 @@ services: - prometheus jenkins: - image: jenkins/jenkins:lts + build: + context: . + dockerfile: Dockerfile.jenkins + image: my-jenkins:latest ports: - "8081:8080" - "50000:50000" @@ -48,6 +51,7 @@ services: volumes: - jenkins_data:/var/jenkins_home - ./jenkins.yaml:/var/jenkins_home/casc_configs/jenkins.yaml + - /var/run/docker.sock:/var/run/docker.sock networks: - custom-network