CHAPTER 04
Creating Your First Jenkins Job
Updated: May 15, 2026
25 min read
# CHAPTER 4
Creating Your First Jenkins Job
1. Introduction
A Jenkins server that just sits there is useless. To make Jenkins perform automation, we must create a Job (also known as a Project). A Job is a specific set of instructions that tells Jenkins what to pull, what to execute, and what to do with the result. Historically, the most common type of job was the "Freestyle Project." While the industry has largely shifted to modern "Pipelines" (which we will cover next), understanding Freestyle jobs is a critical stepping stone to grasping how Jenkins executes commands and reads terminal output.2. Learning Objectives
By the end of this chapter, you will be able to:- Define what a Jenkins "Job" is.
- Create a classic Freestyle Project.
- Configure basic "Build Steps" to execute shell commands.
- Understand the vital importance of the "Console Output."
- Run a job manually and interpret the success/failure result.
3. Beginner-Friendly Explanation
Imagine writing a daily chore list for a robotic butler.- The Job: You create a card titled "Morning Routine."
- The Triggers: You write on the card: "Start this routine every day at 7:00 AM."
- The Build Steps: You list the exact commands:
- 1. Make the coffee.
- 2. Toast the bread.
- 3. Fetch the newspaper.
- The Console Output: The robot has a built-in printer. As it does the chores, it prints out a receipt: "Coffee made successfully... Bread burned (Error)... Newspaper fetched."
4. The Anatomy of a Freestyle Job
When you create a Freestyle Job in the UI, you configure several sections:- 1. General: The name and description of the project.
- 2. Source Code Management (SCM): Telling Jenkins where your code lives (e.g., a GitHub URL). Jenkins will download this code before doing anything else.
- 3. Build Triggers: Telling Jenkins *when* to run. (e.g., "Run every midnight" or "Run whenever code is pushed to GitHub").
- 4. Build Environment: Preparing the server (e.g., injecting secret passwords so the script can use them).
- 5. Build Steps: The actual meat of the job. This is where you tell Jenkins to run Windows Batch commands or Linux Shell scripts.
- 6. Post-build Actions: What to do when finished (e.g., "Send an email to the team if the build failed").
5. Interpreting the Console Output
When Jenkins runs a job, it opens a terminal on the server and types your commands exactly as you wrote them. The Console Output is the raw, unedited log of that terminal session.-
If a command exits with a status code of
0(Success), Jenkins marks the build Blue (Success).
- If any command exits with a non-zero status code (e.g., a script crashes or a test fails), Jenkins immediately stops the job and marks it Red (Failure).
6. Mini Project: Create an Automated Build Job
Let's create a simple job that simulates building an application.Step-by-Step Walkthrough:
- 1. On the Jenkins dashboard, click New Item.
-
2.
Enter the item name:
My-First-Automation.
- 3. Select Freestyle project and click OK.
- 4. Scroll down to the Build Steps section.
- 5. Click Add build step -> select Execute shell (or *Execute Windows batch command* if Jenkins is on Windows natively).
- 6. Enter the following simple script:
bash
echo "Starting the automated build process..."
echo "Compiling code..."
sleep 2
echo "Code compiled successfully!"
# Let's check the current working directory
pwd
ls -la
`
-
7.
Click Save.
-
8.
On the project page, click Build Now on the left menu.
-
9.
A progress bar will appear under "Build History." Click on the build number (e.g.,
#1), then click Console Output.
-
10.
Read the logs! You will see your exact
echo statements printed to the screen, ending with Finished: SUCCESS.
7. Real-World Scenarios
A legacy PHP application had a very complex deployment process involving zipping files, transferring them via FTP, and restarting a remote Apache server. The senior developer was the only one who knew the exact order of the Linux commands. When that developer went on vacation, no one could deploy updates. The team took those 15 terminal commands and pasted them directly into a Jenkins Freestyle Job "Execute Shell" block. Now, anyone on the team could deploy the application simply by logging into Jenkins and clicking "Build Now."
8. Best Practices
-
Keep Build Steps Small: Do not write a 500-line Bash script directly inside the Jenkins UI text box. If the script is complex, save it as a file (e.g.,
deploy.sh) inside your Git repository. Then, your Jenkins Build Step should simply be one line: ./deploy.sh. This keeps your logic in version control, not hidden in the Jenkins database.
9. Security Recommendations
-
Beware of "Execute Shell": Anything written in the "Execute Shell" box runs with the permissions of the
jenkins Linux user. If a malicious user has access to edit jobs in the Jenkins UI, they can write a shell command like cat /etc/passwd to steal system information. Restrict who can edit jobs!
10. Troubleshooting Tips
-
Workspace Issues: Every job gets its own folder on the server (the Workspace). If a job acts weird, it might be using leftover files from a previous run. You can configure the job to "Delete workspace before build starts" in the Build Environment settings to ensure a fresh slate every time.
11. Exercises
-
1.
What dictates whether Jenkins marks a Freestyle job as a Success (Blue) or a Failure (Red)?
-
2.
Why is the "Console Output" the most important diagnostic tool for a DevOps engineer?
12. FAQs
Q: Can a job run another job?
A: Yes! In the Post-build Actions, you can select "Build other projects." This allows you to chain jobs together (e.g., Job A compiles the code, and if successful, triggers Job B to deploy it).
13. Interview Questions
-
Q: Explain the concept of the Jenkins "Workspace." How does Jenkins isolate the files of different Freestyle projects?
-
Q: You have a Freestyle job that runs a shell script. The script successfully completes its task, but the final command in the script is
exit 1`. What will be the final status of the Jenkins build, and why?