Create Jenkins Agents for running Jobs

My Jenkins Story: Configure AWS EC2 instances as non-containerised ...

Even though you can use the master node (aka where jenkins is installed) as a "worker node" jenkins is designed with master-slave architecture in mind, so up next we are going to configure a linux agent on a GCP VM.

1. Install Jenkins

if you don't have jenkins running right now you can check this tutorial on how to deploy it on a ubuntu VM.

2. Create a single Agent

First you need to create an user on the master jenkins machine, type the following on the console.

sudo adduser jenkins
su - jenkins
ssh-keygen

Generate a key for ssh connection on your master node (where you installed jenkins)

ssh-keygen

create credentials on jenkins master node

click on global domain and then add credentials

 

Grab the private key from jenkins user and copy to the key box

su - jenkins

cat ~/.ssh/id_rsa

after you hit ok you should end up with something like this

Now setup the Agent, we are going to create an agent on an ubuntu machine on GCP.

Create a VM on GCP, use f1-micro (free first 744 hours /month)

 

connect to the VM using SSH and install java JDK

     sudo apt-get update

sudo apt install software-properties-common apt-transport-https -y
sudo add-apt-repository ppa:openjdk-r/ppa -y
sudo apt install openjdk-8-jdk -y
sudo apt install nano

java -version

If everything is ok you will see the Java version on you console.
create user jenkins on agent machine (GCP)

sudo useradd -m -s /bin/bash jenkins
sudo passwd jenkins

now copy the public key from master to the worker nodes

Go to your jenkins master node console Log in as jenkins and copy the public key

su -jenkins

cat ~/.ssh/id_rsa.pub

now go to you agent console log in as jenkins, create .ssh folder and add the key to a file name called authorized_keys

su - jenkins

mkdir .ssh

nano authorized_keys

mkdir /home/jenkins/agentdir

Don't forget to install git on the agent and any other programs for your builds.

now add the node 

Go to Jenkins > Manage Jenkins > Manage Nodes and Clouds

select New Node

 

 now fill the information

 remember to put on Host the public IP of your GCP VM and then hit Save

 Wait for the agent to become online

now lets try to build something with that agent.

3. Running A Job

Create a new job

 

First configure the agent where this project should run by setting the labels that match the agent.


 I'm using a repo on Azure DevOps tha contains a simple python script that print "script works"

the only build step I'm going to configure is running the python script.

note: the agent must have python installed

 

Save and then click on Build Now

You should see that the build ran on the agent Created

that's it, hope this helps you.

SHARE

Daniel Sanin

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment