To set up an agent on a VM, we first need to have the VM on same network. For this, I created a VM using Oracle Virtualbox and installed Ubuntu on it.
- Download and install virtualbox > https://www.virtualbox.org/wiki/Downloads
- Create a new VM and install ubuntu. Link
- Open Oracle virtualbox manager.
- Select the VM and open settings.
- Go to network.
- Replace NAT adapder in Adapter 1 with Bridge Adapter.
And we are good to launch the VM.
sudo apt update
sudo apt-get install openssh-server
- Go to Manage Jenkins. Go to Manage Nodes and Clouds.
- Go to New Node. Enter Node name. Mark as Permanent agent and click Ok.
-
Upadate No. of executors to 2,3 whatever. You can keep it 1 as well. This controls how many executors are allowed on a node to run in parallel.
-
Update remote root directory
/home/<user>
In my case, it is /user/kamal
-
Update labels. Set it to ubuntuNode1
-
Select Usage as : "Only Build jobs with label expressions matching this node". This restricts the executions meant for this node to run on this node only.
-
Select Launch method as "Launch agents via SSH". In case you do not see this opetion in dorpdown, you need to install SSH plugins on Jenkins.
- Get the ip of ubuntu. Use this command on ubuntu terminal(Virtual machine/node).
ifconfig
The host should be able to ping VM for communication. We have changed adapted to Bridge to make this possible. To check this, you can run the command from host:
ping 192.168.29.119
where 192.168.29.119 is VM ip address in our case.
-
Enter VM's IP in "Host ip". In our case it is 192.168.29.119
-
Add credentials of VM in creadential manager(if not added already). Select that from dropdown for authentication.
-
Enter Host Key verification strategy as "Non verifying verification strategy"
-
Select Availability as "Bring this agent online when in demand and take offline when idle.". You can choose as per your requirement. This option enables node to be active only when needed.
-
Save.
-
Now run the pipeline using pipelin script. And it should work.
pipeline {
agent {
label 'ubuntuNode1'
}
stages {
stage('pre -build') {
steps {
sh 'echo Pre-build'
}
}
stage('build') {
steps {
sh 'echo Build in progress.'
}
}
stage('Unit tests') {
steps {
sh 'echo Running unit tests'
}
}
stage('deploy') {
steps {
sh 'echo Deploying build'
}
}
stage('Regression tests') {
steps {
parallel(
chrome : {
sh 'echo Running E2E tests on chrome'
},
firefox : {
sh 'echo Running E2E tests on chrome'
},
safari : {
sh 'echo Running E2E tests on chrome'
}
)
}
}
stage('Release to prod') {
steps {
sh 'echo Releasing to prod'
}
}
}
post {
always {
echo 'Cleanup after everything!'
}
}
}
- Now if we use JENKINSFILE5 from github repo to build the pipeline, it may fail.
Selected Git installation does not exist. Using Default
The recommended git tool is: NONE
For this, you need to Configure the Node again. And towards the Bottom, you'll find Node Properties. Check Tool Locations. Select Git default and set path as "/usr/bin/git"
Now it should work from JENKINSFILE as well.