This Jenkinsfile is designed to build, push, and deploy a Spring Boot application using Docker. Before using this Jenkinsfile in your project, please follow the instructions below to customize it based on your project and DockerHub configuration.
-
Clone Repository:
- Ensure that your Jenkins pipeline has the necessary permissions to access your source code repository.
- Update the
scm
section in the Jenkinsfile with the correct repository URL if needed.
stage('Clone repository') { checkout scm }
-
Build Image:
- Set the correct values for the
application
anddockerhubaccountid
variables. - Update the Docker image version or tag based on your versioning strategy.
stage('Build image') { def application = "your-spring-boot-app" def dockerhubaccountid = "your-dockerhub-account" app = docker.build("${dockerhubaccountid}/${application}:${BUILD_NUMBER}") }
- Set the correct values for the
-
Push Image:
- Set the correct DockerHub credentials ID in the
credentialsId
field. - Update the DockerHub registry URL if needed.
stage('Push image') { withDockerRegistry([ credentialsId: "your-dockerhub-credentials-id", url: "your-dockerhub-url" ]) { app.push() app.push("latest") } }
- Set the correct DockerHub credentials ID in the
-
Deploy:
- Update the Docker run command with the correct port mapping, volume mapping, and image version.
stage('Deploy') { sh ("docker run -d -p 82:8080 -v /var/log/:/var/log/ ${dockerhubaccountid}/${application}:${BUILD_NUMBER}") }
-
Remove Old Images:
- Update the Docker image reference to remove old images.
stage('Remove old images') { sh("docker rmi ${dockerhubaccountid}/${application}:latest -f") }
-
Credentials in Jenkins:
- Ensure that DockerHub credentials are configured in Jenkins and the correct credentials ID is used in the Jenkinsfile.
-
Pipeline Execution:
- Run the Jenkins pipeline and monitor the build process.
Note: Ensure that Docker is properly configured on the Jenkins server and that the Jenkins user has the necessary permissions to interact with Docker.
For additional information on Jenkins pipelines, Docker, and Spring Boot, refer to the documentation for each respective tool.
Replace placeholders like `"your-spring-boot-app"`, `"your-dockerhub-account"`, `"your-dockerhub-credentials-id"`, and `"your-dockerhub-url"` with your actual values. Additionally, review and customize other sections based on your project's requirements.