In my last article we talked about how to install Jenkins on both a mac and on a cloud server. If you haven't already, I recommend following my guide introduction-to-jenkins-how-to-install-it and install Jenkins on a cloud server to follow along with this guide and get the most out of it. In this article, we will be setting up our Jenkins install to create jobs and execute them.
To understand the architecture of Jenkins, you need to understand that it is a distributed system with "Master" and "Slaves" in a fairly simple to understand structure.
We are going to learn these terms and others but we first need to understand the fundamentals associated with this "master" & "slave" relationship. Bear in mind, I didn't come up with these sinister names for things - I'm just parroting them. Perhaps a subject for another chat but it's good to be aware of the namesake if you hear it.
On that note, I'll be changing that nomenclature from here on out to be primary/replica π.
Definitions and Terms
Jobs π§ββοΈ
Jobs are a task(s) that are controlled and monitored by Jenkins (the Primary). The job is something that is executable. The Primary solely schedules it and then dispatches it for execution to a selected replica. The Primary does not execute anything itself.
You are probably wondering what a Primary and Replica do. Never fear!
The role of the Primary Jenkins π§βπ³
- Schedule build jobs
- Dispatch build to the replica for actual job execution
- Monitoring the replicas and recording the build results
Primary: Execution doesn't occur on the Jenkins install (Primary), it actually happens on the virtual machines that the Jenkins Master is instructing it to perform on. After it schedules a job, it monitors the replicas execution and if issues arise it will be notified and you will be able to see it on the dashboard.
The role of the Replicas π¨βπ§π¨βπ§βπ¦π¨βπ©βπ¦βπ¦
- Execute build jobs dispatched by the primary
Replicas: Also referred to as "Nodes". Replicas are computers or virtual machines that are a part of Jenkins to run jobs.
Replica Agents π§βπ§
Jenkins runs a separate program called "replica agents" on replicas. When replicas are registered to the primary, the primary will start distributing the load to replicas.
Executor π
Executor is a separate stream of builds that can be run on a node in parallel to run a job. On a single replica, you can define any number of executors. You will be limited by the node's resources to how many parallel jobs on the same machine - be careful. Example, if you have 2 CPUs, you can only run 2 processes at once.
Pluginsπ
Plugins, like in many other applications, is a piece of code that extends the core functionality of the Jenkins system. Depending on your project, you may need additional plugins installed but we probably won't dive too deep here.
Set up Jenkins
To follow this section and get the most out of it, I highly recommend installing Jenkins per this guide: introduction-to-jenkins-how-to-install-it first.
Creating your first Job in Jenkins
Navigate to the IP of your server and go to port 8080. If you aren't sure how to do this, refer back to this section: here.
Log into your Jenkins install. Here you will see your Jenkins Dashboard.
From the dashboard, select "New Item" from the Menu Bar on the left side.
Create an Item name. For this example we are just going to call it "example", select "Freestyle Project" and click "OK".
If you give your new item a description.
Head to the "Build" section and select "Execute shell" and paste the following into the command text block:
#! /bin/bash # Start Loop for a in 1 2 3 4 5 6 7 8 9 10 do # if a is equal to 5 make the loop go to sleep for 10 seconds if [ $a == 5 ]; then sleep 30 fi #print the value from the loop echo "Iteration # $a" done
This is not important to really know how to write for now but what you need to know about this snippet is that it's running a loop and will put the loop to sleep for 30 seconds after the 5th iteration and output the iteration number in the console.
Click save.
Running your first build
You will be taken to a screen that looks like this:
You can then select "Build now" and you will see a new build is running. Click on it, it should look like this:
Failed Console Output Started by user drew karriker Running as SYSTEM Building in workspace /var/lib/jenkins/workspace/example [example] $ /bin/bash /tmp/jenkins12193338751104274318.sh FATAL: command execution failed java.io.IOException: error=2, No such file or directory at java.base/java.lang.ProcessImpl.forkAndExec(Native Method) at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:340) at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:271) at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1107) Caused: java.io.IOException: Cannot run program "" (in directory "/var/lib/jenkins/workspace/example"): error=2, No such file or directory at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1128) at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1071) at hudson.Proc$LocalProc.<init>(Proc.java:252) at hudson.Proc$LocalProc.<init>(Proc.java:221) at hudson.Launcher$LocalLauncher.launch(Launcher.java:936) at hudson.Launcher$ProcStarter.start(Launcher.java:454) at hudson.tasks.CommandInterpreter.perform(CommandInterpreter.java:109) at hudson.tasks.CommandInterpreter.perform(CommandInterpreter.java:66) at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20) at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:741) at hudson.model.Build$BuildExecution.build(Build.java:206) at hudson.model.Build$BuildExecution.doRun(Build.java:163) at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:504) at hudson.model.Run.execute(Run.java:1880) at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43) at hudson.model.ResourceController.execute(ResourceController.java:97) at hudson.model.Executor.run(Executor.java:428) Build step 'Execute shell' marked build as failure Finished: FAILURE
Uh oh, What did I do wrong?!
This Error was intentional so you could see a failed build. Note this last line: Build step 'Execute shell
'. What this is telling us is that the shell failed to run. If we look at line 1 in the command we used above, there is a space between the !
and the /bin/bash
.
Click the Configure Item
button and then go down to the build block and edit the command. Remove that space I just mentioned. It should look like this:
#! /bin/bash # Start Loop for a in 1 2 3 4 5 6 7 8 9 10 do # if a is equal to 5 make the loop go to sleep for 10 seconds if [ $a == 5 ]; then sleep 30 fi #print the value from the loop echo "Iteration # $a" done
Now save and select Build Now
again. You should see a new job in the job history that looks like this:
Note the new job is no longer red (meaning a failure) and is now blue (successful).
Select that job and open the Console Output
again. You will see a job status bar. You'll see an output like this:
You have now successfully ran a job in Jenkins! π
Next...
We will learn how to integrate Git & Github into Jenkins to automate builds so that you can see the magic of Jenkins in action.
If you found this article helpful, give me a shout on twitter β Iβd love to hear from you. @codingwithdrewk or connect with me on linkedin. As always, if you found any errors, just highlight it and mash that βRβ button on the right side of the screen and Iβll get those fixed right up!
Featured Image credit: Created by CloudBees abConsulting Network
Drew is a seasoned DevOps Engineer with a rich background that spans multiple industries and technologies. With foundational training as a Nuclear Engineer in the US Navy, Drew brings a meticulous approach to operational efficiency and reliability. His expertise lies in cloud migration strategies, CI/CD automation, and Kubernetes orchestration. Known for a keen focus on facts and correctness, Drew is proficient in a range of programming languages including Bash and JavaScript. His diverse experiences, from serving in the military to working in the corporate world, have equipped him with a comprehensive worldview and a knack for creative problem-solving. Drew advocates for streamlined, fact-based approaches in both code and business, making him a reliable authority in the tech industry.