Master-Slave in Jenkins: What It Is and How It Works
master-slave refers to a setup where the master server controls the build process and delegates tasks to one or more slave agents that run jobs. This helps distribute workload and speeds up build execution by running jobs on different machines.How It Works
Think of Jenkins master as the boss who plans and manages work. The slaves are the workers who actually do the tasks. The master decides which job to run and sends it to a slave machine that has the right environment or capacity.
This setup helps when you have many projects or heavy builds. Instead of one machine doing everything slowly, multiple slaves can run jobs in parallel. The master keeps track of all jobs and results, while slaves focus on running the builds.
Slaves connect to the master and wait for instructions. When a job is assigned, the slave runs it and sends back the output. This way, Jenkins can scale and handle more work efficiently.
Example
This example shows how to define a Jenkins slave node using a simple Groovy script in the Jenkins Script Console. It creates a new slave that connects via SSH.
import hudson.slaves.* import hudson.model.* import jenkins.model.* // Define slave details String slaveName = "build-slave-1" String remoteFS = "/home/jenkins" String host = "192.168.1.100" String credentialsId = "ssh-credentials-id" // Create SSH launcher ComputerLauncher launcher = new hudson.plugins.sshslaves.SSHLauncher(host, 22, credentialsId) // Create slave node DumbSlave slave = new DumbSlave(slaveName, "Build slave node", remoteFS, "1", Node.Mode.NORMAL, "linux", launcher, RetentionStrategy.NOOP, new LinkedList()) // Add slave to Jenkins Jenkins.instance.addNode(slave) println("Slave node '${slaveName}' added successfully.")
When to Use
Use master-slave in Jenkins when you want to speed up builds by running them on multiple machines. It is helpful if your projects need different environments, like Windows and Linux, or if builds require heavy resources.
For example, a team building software for multiple platforms can assign slaves with specific OS to run tests. Also, large teams with many jobs benefit from distributing work to avoid overloading a single server.
This setup improves reliability and scalability, making Jenkins suitable for bigger projects and continuous integration pipelines.
Key Points
- The master manages jobs and delegates work.
- Slaves run jobs on different machines.
- This setup allows parallel execution and better resource use.
- Slaves can have different environments for specific build needs.
- It helps scale Jenkins for large teams and projects.