To write a Jenkins pipeline to deploy a Spring Boot service on Docker, you can follow these steps:
- Set up Jenkins: Install and configure Jenkins on your server. Make sure you have the necessary plugins installed, such as the Docker Pipeline plugin. how to write jenkins pipline to deploy spring boot service on dockerĀ
- Create a Jenkinsfile: In your project’s repository, create a file named
Jenkinsfile
(without any file extension) at the root level. This file will contain the pipeline script. - Define the pipeline script: Open the
Jenkinsfile
and define the pipeline script using the Jenkins declarative syntax or scripted syntax. Here’s an example using declarative syntax.
pipeline{
agent any
environment {
SERVICE = ‘asset-personnel-service’
}
stages{
stage(“Clone”){
steps{
echo “Cloning $SERVICE …”
git branch: ‘${BRANCH_NAME}’, credentialsId: ‘1’, url: “https://git-codecommit.us-east-2.amazonaws.com/v1/repos/$SERVICE”
}
}
stage(“Build”){
steps{
dir(‘asset-personnel-service/’) {
echo “Building $SERVICE …”
sh ‘chmod 777 gradlew’
sh ‘./gradlew build’
sh ‘./gradlew clean’
sh ‘./gradlew assemble’
}
}
}
stage(“Docker image”){
steps{
dir(‘asset-personnel-service/’) {
sh ‘sudo docker rm $(sudo docker stop $(sudo docker ps -a | grep “asset-personnel-service” | cut -d ” ” -f 1))’
sh ‘sudo docker build -t asset-personnel-service .’
sh ‘sudo docker run -it -dp 9196 asset-personnel-service’
sh ‘sudo docker ps -a | grep asset-personnel-service’
}
}
}
stage(“Backup”){
steps{
dir(‘asset-personnel-service/build/libs’) {
sh ‘sudo cp asset-personnel-service.jar /data/servers/ams_be_jars/’
}
}
}
post {
always {
cleanWs()
dir(“${env.WORKSPACE}@tmp”) {
deleteDir()
}
}
}
}
- Customize the script: Replace
'your-image-name:tag'
with the desired name and tag for your Docker image. Modify the<your-docker-registry-url>
and<your-docker-registry-credentials-id>
placeholders with your Docker registry details if you want to push the image to a registry. Adjust the Docker deployment command (docker-compose up -d
) based on your specific requirements. - Commit and push: Save the
Jenkinsfile
and commit it to your project’s repository. - Configure Jenkins job: In the Jenkins web interface, create a new pipeline job. In the job configuration, specify the repository and branch where your
Jenkinsfile
is located. - Save and run: Save the job configuration and manually trigger a build. Jenkins will execute the pipeline according to the defined stages and steps. It will build your Spring Boot service, create a Docker image, and deploy it using Docker. how to write jenkins pipline to deploy spring boot
Note: Make sure you have Docker installed and accessible from your Jenkins environment for this pipeline to work properly.