Table of Contents
- What is a Jenkins Agent?
- Prerequisites
- Stage 1 — Create the agents on the controller
- Stage 2 — Start each agent
- Stage 3 — A simple Java program and tests
- Stage 4 — A freestyle job restricted to the agents
- Stage 5 — Parallel demo with two jobs
- Stage 6 — Agents with a Jenkinsfile (pipeline)
- Verify
- Key points
This note shows how to set up two Jenkins agents (also called “nodes”) that connect to a Jenkins controller, and how to make those agents build and test a simple Java program. Distributing work across agents lets multiple jobs run in parallel and keeps heavy compilation off the controller.
What is a Jenkins Agent?
Jenkins uses a controller-worker (master-slave) model:
- The controller (the main Jenkins server) is responsible for scheduling jobs, managing configuration, and collecting results. It does the orchestration, not the heavy lifting.
- Agents (also called nodes or slaves) are separate machines or processes that actually run the build steps. They connect back to the controller (typically over TCP port
50000) and execute whatever work the controller assigns to them.
An agent can be:
- Another physical machine or VM,
- A container,
- Or a separate process on the same host.
For this demo we run two agents locally as separate processes, but the setup is identical for remote machines.
Prerequisites
- A running Jenkins controller (see 03-jenkings-installation). The controller listens on port
8080for its web UI and on port50000for incoming agent connections. - OpenJDK installed on each agent host (agents run Java, so each one needs a JDK). The
javacommand must be on thePATHof each agent. - Optionally, install the Git client on each agent if a job has to clone a repository there.
- A credential (user/password) that agents will use to authenticate when connecting — this is usually an ordinary Jenkins user, or even the
adminuser with a dedicated access token.
Stage 1 — Create the agents on the controller
In the Manage Jenkins menu, navigate to Manage Nodes and Clouds → New Node.
Create Agent 1 (agent-linux-1)
- Click New Node → enter the name
agent-linux-1→ select Permanent agent → Create. - Fill in the fields:
- Number of executors:
1(how many concurrent builds this agent can run). - Remote root directory: the folder where Jenkins will put the agent’s workspace, e.g.
/home/jenkins/agent1. - Labels:
linuxandjava(labels let a job request a specific agent type). - Usage: Use this node as much as possible.
- Launch method: Launch agent by connecting it to the controller (the agent connects out to the controller on port
50000, which avoids firewall issues).
- Number of executors:
Create Agent 2 (agent-linux-2)
Repeat the same steps with the name agent-linux-2:
- Number of executors:
1 - Remote root directory:
/home/jenkins/agent2 - Labels:
linuxandjava
Using distinct labels lets a job say “I only run on
javalabelled agents” or split work acrossagent-linux-1andagent-linux-2. For this demo we give both the same labels so either can take the work.
Stage 2 — Start each agent
Each agent needs a small agent.jar (the agent binary) plus a secret connection command. The controller generates the exact start command for you:
- On the controller, open the agent page: Manage Jenkins → Nodes → [agent name].
- Scroll to the agent section — Jenkins shows a
java -jar agent.jar ...command that includes a one-time secret. Copy it.
Then, on each agent host, download the agent jar and run that command:
mkdir -p /home/jenkins/agent1
cd /home/jenkins/agent1
curl -sO http://<controller-ip>:8080/jnlpJars/agent.jar
java -jar agent.jar \
-url http://<controller-ip>:8080 \
-secret <AGENT1_SECRET> \
-name agent-linux-1 \
-workDir /home/jenkins/agent1Repeat for the second agent:
mkdir -p /home/jenkins/agent2
cd /home/jenkins/agent2
curl -sO http://<controller-ip>:8080/jnlpJars/agent.jar
java -jar agent.jar \
-url http://<controller-ip>:8080 \
-secret <AGENT2_SECRET> \
-name agent-linux-2 \
-workDir /home/jenkins/agent2Back in Manage Jenkins → Nodes, both agents should change status to a green (online) state.
For production, prefer running each agent as a systemd service so it survives reboots and keeps itself connected.
Stage 3 — A simple Java program and tests
Create a small Java project that we will build and test on the agents. Keep it in a folder (e.g. RandomNumberGenerator):
RandomNumberGenerator.java
import java.util.Random;
public class RandomNumberGenerator {
public static void main(String[] args) {
System.out.println("Generating 100 random numbers:");
int[] numbers = generateRandomNumbers(100);
for (int i = 0; i < numbers.length; i++) {
System.out.println("Number " + (i + 1) + ": " + numbers[i]);
}
}
public static int[] generateRandomNumbers(int count) {
Random random = new Random();
int[] numbers = new int[count];
for (int i = 0; i < count; i++) {
numbers[i] = random.nextInt();
}
return numbers;
}
}RandomNumberGeneratorTest.java
public class RandomNumberGeneratorTest {
public static void main(String[] args) {
testRandomNumberArrayLength();
testReturnsDistinctInstances();
System.out.println("All tests passed.");
}
public static void testRandomNumberArrayLength() {
int count = 100;
int[] numbers = RandomNumberGenerator.generateRandomNumbers(count);
assert numbers.length == count
: "Test failed: Array length is incorrect.";
System.out.println("PASS: array length is correct.");
}
public static void testReturnsDistinctInstances() {
int[] a = RandomNumberGenerator.generateRandomNumbers(5);
int[] b = RandomNumberGenerator.generateRandomNumbers(5);
assert a != b : "Test failed: each call must return a new array.";
System.out.println("PASS: each call returns a new array.");
}
}Stage 4 — A freestyle job restricted to the agents
Now create a Freestyle project that runs on the agents:
-
Create Job →
agent-java-demo→ Freestyle project. -
In General, tick Restrict where this project can be run and set Label Expression to
java. This is the key step: it tells Jenkins the job may only execute on an agent that has thejavalabel — i.e.agent-linux-1oragent-linux-2. -
Add the project files (the two
.javafiles above) into the job’s workspace, or better, connect it to a Git repository (Source Code Management → Git + repository URL) so the agents clone the code themselves. -
Build Steps → Add build step → Execute shell:
echo "Building on agent: $(hostname)" javac RandomNumberGenerator.java RandomNumberGeneratorTest.java -
Add a second build step to run the tests with assertions enabled (the
-eaflag makesassertstatements active; without it they are silently skipped):echo "Running on agent: $(hostname)" java -ea RandomNumberGeneratorTest -
Save, then Build Now. Open the Console Output and note that it reports the build ran on an agent (its hostname appears in the output).
Run the build a few times. Because both agents share the java label and have one executor each, Jenkins will schedule the builds across agent-linux-1 and agent-linux-2 — you can see the hostname change between runs.
Stage 5 — Parallel demo with two jobs
To prove the value of two agents, create a second identical job so you can run both at the same time:
- On the
agent-java-demoproject, choose New Item → copy fromagent-java-demoand name itagent-java-demo-2. - Keep the same
javalabel restriction. - Trigger both jobs at the same moment. Because each job is pinned to executor-count-1 agents, and both agents are available, Jenkins runs them on separate agents in parallel rather than queuing one behind the other.
Check the builds:
- Each build lands on a different agent (
agent-linux-1vsagent-linux-2). - Both complete concurrently instead of one waiting for the other.
Stage 6 — Agents with a Jenkinsfile (pipeline)
You can do the same in a declarative pipeline, where the agent block requests the java label:
pipeline {
agent { label 'java' }
stages {
stage('Build') {
steps {
sh '''
echo "Building on agent: $(hostname)"
javac RandomNumberGenerator.java RandomNumberGeneratorTest.java
'''
}
}
stage('Test') {
steps {
sh '''
echo "Running on agent: $(hostname)"
java -ea RandomNumberGeneratorTest
'''
}
}
}
}The agent { label 'java' } line is what routes the pipeline to either agent-linux-1 or agent-linux-2. If you want the two stages to run on different agents, add a second labelled group:
pipeline {
agent none
stages {
stage('Build') {
agent { label 'java' }
steps { sh 'javac RandomNumberGenerator.java' }
}
stage('Test') {
agent { label 'java' }
steps { sh 'java -ea RandomNumberGeneratorTest' }
}
}
}Verify
- Both agents show online and idle in Manage Jenkins → Nodes.
- A job restricted to the
javalabel runs on an agent, printing its hostname in the console. - Two concurrent jobs spread across
agent-linux-1andagent-linux-2run in parallel. - The tests pass (green build) and fail (red build) when an
assertfails — confirming work is really happening on the agents.
Key points
- Agents run the actual builds; the controller only schedules and monitors them.
- Labels are how the controller decides which agent can run a job — a job requests a label, agents declare labels.
- Each agent needs a JDK (and a
javaonPATH) to run Java builds. - Agents authenticate to the controller over port 50000 by default; the controller generates each agent’s secret connection command.
- Use
java -ea(or equivalent) so test assertions actually execute on the agent.