Jenkins Maven Agent
By creating Jenkins Maven Agent for Kubernetes Cluster, you can improve build time of your maven builds
Jenkins Maven Agent on Kubernetes
(Total Setup Time: 15 mins)
Following up on the previous post, I will create a Jenkins Maven Agent for Kubernetes. By configuring a local maven m2 repository, you can save previous time on your builds.
Configuring Jenkins
(1 min)
First, navigate to Jenkins > Configure Clouds, and click on the Add Pod Template. I named the pod template as maven, with its usage set as:
Only build jobs with label expressions matching this node
Second, add a container with the following settings:
Name: jnlp
Dcoker image: docjoube/jenkins-agent:1.0
working directory: /home/jenkins/agent
Third, add a volume, with the following persistent volume claim:
Claim Name: maven-agent-pvc
Mount path: /home/jenkins/.m2
Fourth, insert the maven volume claims to the maven-pv.yaml:
apiVersion: v1
kind: PersistentVolume
metadata:
name: maven-agent-pv
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 2Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/hdd/master1k8s/app/maven/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: maven-agent-pvc
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 2Gi
Lastly, apply the maven-agent persistent volume and claims:
# Create the persistent volume and claim
kubectl apply -f maven-pv.yaml
# Create the data folder and change ownership
sudo mkdir -p /mnt/hdd/master1k8s/app/maven/data
sudo chown -R 1000:1000 /mnt/hdd/master1k8s/app/maven/data
Testing Jenkins Maven Agent
(14 mins)
First, modify Jenkinsfile to use maven agent and commit the change. You may refer to kubernetes plugin for more examples.
pipeline {
agent none
stages {
stage('Build') {
agent {
label 'maven'
}
steps {
checkout scm
sh './mvnw -DskipTests clean package'
}
}
}
}
Second, Jenkins automatically triggers a build.
You may notice that Jenkins master provisions a new maven-agent for this build.
Last, this is the build console output:
For subsequent builds, maven-agent uses the local maven m2 repository and hence significantly reducing the build time.
Troubleshooting
persistentvolumeclaim “maven-agent-pvc” not found
When the maven persistent volume is not created before any maven build, this error will occur. You can clear this error by creating the claim first.
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning FailedScheduling <unknown> default-scheduler persistentvolumeclaim "maven-agent-pvc" not found
Warning FailedScheduling <unknown> default-scheduler persistentvolumeclaim "maven-agent-pvc" not found
No such file or directory
Because Jenkins user needs access rights to the maven persistent volume mount path, remember to change the folder ownership.
sudo chown -R 1000:1000 /mnt/hdd/master1k8s/app/maven/data
Waiting for next available executor
You can fix this error by following closely to this console output and removing the default values (/bin/sh -c and cat) from Jenkins > Configure Clouds.