Deploying a Spring Boot application in a CI/CD pipeline on Red Hat OpenShift using Jenkins and Docker involves a few key steps: building a Docker image, creating a Jenkins pipeline, pushing the image to an image registry, and deploying it to OpenShift. Here’s a step-by-step guide to get you started:
Ensure your Spring Boot application has a Dockerfile
in the root directory. A sample Dockerfile
might look like this:
# Use a base image
FROM openjdk:17-jdk-alpine
# Set the JAR file location and copy it to the container
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
# Run the JAR file
ENTRYPOINT ["java", "-jar", "/app.jar"]
-
Install Plugins: Ensure Jenkins has the necessary plugins installed, including Pipeline, Docker, Kubernetes CLI, and OpenShift Pipeline plugins.
-
Configure Jenkins Credentials:
- Add DockerHub or any image registry credentials to Jenkins to push the Docker image.
- Add OpenShift token and server URL to Jenkins for OpenShift deployment.
-
Create a Jenkinsfile: This file defines the CI/CD pipeline, including building the Docker image, pushing it to a registry, and deploying it to OpenShift.
Here’s a sample Jenkinsfile
:
pipeline {
agent any
environment {
DOCKER_IMAGE = "your-docker-registry/your-image-name"
OPENSHIFT_PROJECT = "your-openshift-namespace"
OPENSHIFT_APP = "spring-boot-app"
OPENSHIFT_SERVER = "https://api.openshift-cluster.com:6443"
OPENSHIFT_TOKEN = credentials('openshift-token')
}
stages {
stage('Build') {
steps {
echo "Building Spring Boot application"
sh "mvn clean package -DskipTests"
}
}
stage('Build Docker Image') {
steps {
script {
docker.build(DOCKER_IMAGE)
}
}
}
stage('Push Docker Image') {
steps {
script {
docker.withRegistry('https://index.docker.io/v1/', 'docker-credentials-id') {
docker.image(DOCKER_IMAGE).push("${env.BUILD_NUMBER}")
}
}
}
}
stage('Deploy to OpenShift') {
steps {
script {
withCredentials([string(credentialsId: 'openshift-token', variable: 'TOKEN')]) {
sh "oc login ${OPENSHIFT_SERVER} --token=${TOKEN} --insecure-skip-tls-verify"
sh "oc project ${OPENSHIFT_PROJECT}"
sh "oc set image dc/${OPENSHIFT_APP} ${OPENSHIFT_APP}=${DOCKER_IMAGE}:${env.BUILD_NUMBER}"
sh "oc rollout status dc/${OPENSHIFT_APP}"
}
}
}
}
}
post {
success {
echo 'Deployment Successful!'
}
failure {
echo 'Deployment Failed!'
}
}
}
- In Jenkins, create a new Pipeline job and link it to your repository containing the
Jenkinsfile
. - Run the pipeline to start the CI/CD process. Jenkins will:
- Build the Spring Boot application.
- Build and push the Docker image to the specified registry.
- Deploy the application to OpenShift.
Once the pipeline completes, go to the OpenShift Console:
- In your project, verify the deployment status under Applications > Deployments.
- You can expose your service by using
oc expose
or by creating a route in the OpenShift Console to make your application accessible.
- OpenShift Credentials: You need a valid OpenShift token for Jenkins to authenticate and deploy to OpenShift. You can retrieve it by running
oc whoami -t
in the OpenShift CLI. - Environment-Specific Configurations: Adjust image names, tags, and environment configurations as per your OpenShift project settings.
- Rolling Updates: OpenShift deployment configuration (
dc
) will ensure that updates roll out with zero downtime, provided the readiness and liveness probes are correctly configured.
Would you like further clarification on any step?