Jenkins Pipeline for K8s Cluster
With Jenkins Pipeline for Kubernetes Cluster, you can create a continuous integration environment for your project
Jenkins Pipeline on Kubernetes Cluster
(Total Setup Time: 10 mins)
In this guide, I will create my own declarative Jenkins pipeline. With this, I can build, package and run my Spring Boot Hello-World application.
Configuring Jenkins and Gitea
(1 min)
First, by following my previous post, I will install Git.
# Get the Jenkins pod
kubectl get pods
# Get a shell in container
kubectl exec --stdin --tty jenkins-7c5ffc6f55-c8fmj -- /bin/bash
# Search for Git
which git
# Install Git if not present
apt-get install git
Second, navigate to Jenkins > Global Tool Configuration. Check if Git setup correctly.
Third, navigate to Gitea > Site Administration Tool Configuration. Create a new organization.
Testing Jenkins Pipeline
(9 mins)
With the Gitea webhooks in place, Jenkins master will trigger a build on each Git commit. You may configure Jenkins to send email alert when the build fails.
First, this is my Jenkinsfile:
pipeline {
agent any
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Build') {
steps {
sh './mvnw clean verify'
}
}
}
}
Second, when Apache Maven builds, it downloads all dependency jars.
Last, this is my final build success result:
That’s all to it! My Jenkins Pipeline for Kubernetes Cluster works in sync with each Git commits. You may follow my next guide to improve on the maven build time.
Troubleshooting
There’s no such executable git in PATH
You can clear this error by installing Git in the Jenkins pod.
For a permanent solution, re-build the jenkins docker image from the previous post. This is the modified Dockerfile:
FROM balenalib/raspberrypi4-64-debian-openjdk:11-bullseye
ENV JENKINS_HOME /var/jenkins_home
ENV JENKINS_SLAVE_AGENT_PORT 50000
RUN apt-get update \
&& apt-get install -y --no-install-recommends curl git
RUN curl -fL -o /opt/jenkins.war http://updates.jenkins-ci.org/download/war/2.235.3/jenkins.war
VOLUME ${JENKINS_HOME}
WORKDIR ${JENKINS_HOME}
EXPOSE 8080 ${JENKINS_SLAVE_AGENT_PORT}
CMD ["/bin/bash","-c","java -jar /opt/jenkins.war"]
Build the image and tag it:
docker build -t seehiong/jenkins:2.0 .
[Gitea] Notifying branch build status: PENDING Build started
This error happens when Jenkins cannot notify Gitea. You can fix this error by creating a new organization at Gitea.
Jenkins pipeline notifies Gitea on each build status:
./mvnw: Permission denied
My initial build failed because of the file permission issue.
+ ./mvnw clean verify
/var/jenkins_home/workspace/seehiong_hello-world_master@tmp/durable-c269e2ef/script.sh: 1: ./mvnw: Permission denied
You can fix this issue by changing mode for mvnw and push the changes to gitea:
chmod +x mvnw
git update-index --chmod=+x mvnw