Skip to main content

JENKINS ON EC2: SETTING MASTER SLAVE NODE

In this tutorial we will setup a Jenkins master slave setup. Here we have a master and slave ec2 instances running in aws. Assume we have two ec2 ubuntu instances running.One will be master and other will be our slave.



First on master we will install Jenkins.
INSTALLING JENKINS
Add the repository key to the system
#wget -q -O - https://pkg.jenkins.io/debian/jenkins-ci.org.key | sudo apt-key add -
Next, we'll append the Debian package repository address to the server's sources.list:
#echo deb https://pkg.jenkins.io/debian-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list
Now update
#apt-get update
Now install jenkins
#apt-get install jenkins
Then we will start and enable Jenkins service
#systemctl start jenkins
#systemctl enable Jenkins

Now install github and maven integration plugins.
Manage Jenkins > Manage plugins > select github integration









Now on Configure  global security , uder agents give random for jnlp port.This will enable launching agent on java start.










 After that go to Manage nodes > New Node
Here give name as slave1 and click on Permenenet Agent
After that give description  and root directory on slave where Jenkins should have workspace.Here iam creating /opt/Jenkins as root directory.
Give no of executers as 1.Under Labels give a name which we use while building. Here iam giving it as slave1 itself. Let the Usage be use this node as much as possible and launch method be launch on java web start.
Under Agent it be keep this agent online as much as possible.
Now click on Save.



Now login to slave machine.
Create directory for Jenkins where the master will utilise workspace
#mkdir /opt/Jenkins
Now install java and maven.
#add-apt-repository ppa:webupd8team/java
#apt-get update -y
#apt-get install oracle-java8-installer

Now we will install maven.By default maven 3.3.9 is available on ubuntu 16.
#apt-get install maven
Now go to dashboard, go to Manage nodes and click on slave








Right click on agent.jar and copy link address.Now go to slave server
#cd /opt/Jenkins
#wget java -jar agent.jar -jnlpUrl http://54.187.96.79:8080/computer/slave%201/slave-agent.jnlp -secret d559feadf1d597bfe0fa876d69597151f0aa728b4bcb5387db7d054ea545498c -workDir "/opt/jenkins"

Now copy the java command from dash board and execute it here.
#java -jar agent.jar -jnlpUrl http://54.187.96.79:8080/computer/slave%201/slave-agent.jnlp -secret d559feadf1d597bfe0fa876d69597151f0aa728b4bcb5387db7d054ea545498c -workDir "/opt/jenkins"

Now go to dashboard and go to nodes.We can see our slave is up.

To run build on slave, we will create a new job.
















Give project name and select free style project and click ok.
There will be different sections.Under General give project name,description.
Usually Jenkins will create log files on each builds.It uses memory and can result in running out of space.So under Discard old builds we will specify number of days we want to keep the build or number of builds we want to keep.Usually we set the days to our release cycle.
Under Project is parameterized we can set parameters that we want to pass.
Under Restrict where this project can run, give the label we gave on our slave .Here I gave slave1.

Then Under Source Code management. Select the SCM we uses. Since we are using Git select git.Give the url of repository.There will be a branch specifier.Since we are using master branch.Keep as it is.
Under Build Triggers we will select how to trigger the build.
Build periodically will trigger build at periodic intravels based on intravel we gave.
Build after other projects are built: Here we can give project name after which our build is to be executed.
Poll SCM will trigger a build only when source repository is changed.we need to only set the intravel it will check repository.Here we will set Poll SCM as * * * * * so that it will check in every minute.

Under Build section select Invoke top-level Maven targets.Now give clean Pacakage over there.This will execute mvn clean package command.