Prerequisite
- Java install
- Jenkins server
- Service Account with the GKE permissions
- Any application
Introduction
In this blog, we will write a declarative Jenkins pipeline to deploy applications on GKE and configure the multibranch pipeline. I am using the spring boot pet clinic for demonstration. This project consists of a spring-boot application and a docker file.
Declarative Pipeline
pipeline{
agent any
environment {
PROJECT_ID = 'your project id'
CLUSTER_NAME = 'your cluster name'
LOCATION = 'zone'
CREDENTIALS_ID = 'your project name '
}
stages{
stage('checkout git'){
steps{
git branch: 'main',
url:'https://github.com/shivampateriya1/spring-petclinic.git'
}
}
stage('Build App'){
steps{
sh './mvnw clean package'
}
post{
success{
junit 'target/surefire-reports/*.xml'
archiveArtifacts "target/*.jar"
}
}
}
stage('Docker build'){
steps{
script{
sh 'docker build -t shivampateriyaknoldus/petclinic:0.1 . '
}
}
}
stage('Docker login and push'){
steps{
withCredentials([string(credentialsId: 'dockersecret', variable: 'TOKEN')]) {
sh 'docker login -u shivampateriyaknoldus -p $TOKEN'
sh 'docker push shivampateriyaknoldus/petclinic:0.1'
}
}
}
stage('Deploy to kubernetes'){
steps{
step([$class: 'KubernetesEngineBuilder', projectId: env.PROJECT_ID, clusterName: env.CLUSTER_NAME, location: env.LOCATION, manifestPattern: 'petclinic.yaml', credentialsId: env.CREDENTIALS_ID, verifyDeployments: true])
echo "Deployment Finished ..."
}
}
}
}
Stages
- Checkout git
- This stage will clone the repo for further use.
- Build App
- Build maven project
- Docker build
- Build docker image
- Docker login
- Login into docker and push the docker image for deployment
- Deploy to Kubernetes
- Deploy application on GKE cluster

For practice purposes, you can use https://github.com/spring-projects/spring-petclinic.git



Download Google Kubernetes Engine






Jenkins Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: petclinic
name: petclinic
spec:
selector:
matchLabels:
app: petclinic
template:
metadata:
labels:
app: petclinic
spec:
containers:
- name: petclinic
image: shivampateriyaknoldus/petclinic:0.1
imagePullPolicy: Always
ports:
- containerPort: 8080
Expose using Loadbalancer
apiVersion: v1
kind: Service
metadata:
namespace: petclinic
name: petclinic
spec:
type: LoadBalancer
selector:
app: petclinic
ports:
- port: 8081
targetPort: 8080
Build Pipeline









Conclusion
In this blog, we are able to build a multibranch pipeline and build it using maven, docker and push it on docker hub and deploy it on the GKE cluster using Jenkins.
For CI/CD blog https://blog.knoldus.com/jenkins-continuous-integration-ci-continuous-delivery-and-deployment-cd/