DEV Community

Aisalkyn Aidarova
Aisalkyn Aidarova

Posted on

Lab 1 — Your First Jenkins Job

Goal

By the end of this lab, you will understand:

Jenkins → Job → Build → Console Output → Workspace

You will create a job that Jenkins executes automatically.

Prerequisites

Make sure Jenkins is running. On your Mac, you can check:

brew services list | grep jenkins
Enter fullscreen mode Exit fullscreen mode

If Jenkins is stopped:

brew services start jenkins-lts
Enter fullscreen mode Exit fullscreen mode

Then open:

http://localhost:8080
Enter fullscreen mode Exit fullscreen mode

You should see the Jenkins Dashboard.


Step 1 — Create Your First Job

On the Jenkins Dashboard:

New Item → Enter a name:

my-first-job
Enter fullscreen mode Exit fullscreen mode

Choose:

Freestyle project
Enter fullscreen mode Exit fullscreen mode

Click:

OK
Enter fullscreen mode Exit fullscreen mode

What is a Jenkins Job?

A job is a task that Jenkins knows how to execute.

For example, later your jobs can:

Pull code from GitHub
        ↓
Run tests
        ↓
Run security scan
        ↓
Build Docker image
        ↓
Push image to ECR
        ↓
Deploy to Kubernetes
Enter fullscreen mode Exit fullscreen mode

But today we'll start with a simple command.


Step 2 — Add Your First Build Step

Scroll down to:

Build → Add build step → Execute shell

Paste:

echo "Hello from Jenkins!"
echo "This is my first Jenkins job."
echo "Jenkins is running commands automatically."
Enter fullscreen mode Exit fullscreen mode

Click:

Save

You should now be on the job page.


Step 3 — Run the Job

Click:

Build Now

On the left side you should see:

Build History

#1
Enter fullscreen mode Exit fullscreen mode

Click:

#1
Enter fullscreen mode Exit fullscreen mode

Then click:

Console Output
Enter fullscreen mode Exit fullscreen mode

Expected output:

Started by user
Building in workspace ...

Hello from Jenkins!
This is my first Jenkins job.
Jenkins is running commands automatically.

Finished: SUCCESS
Enter fullscreen mode Exit fullscreen mode

If you see:

Finished: SUCCESS
Enter fullscreen mode Exit fullscreen mode

your first Jenkins job worked.


Step 4 — Understand What Just Happened

You manually clicked:

Build Now
Enter fullscreen mode Exit fullscreen mode

Jenkins then:

1. Created a build
        ↓
2. Created/used a workspace
        ↓
3. Started a shell
        ↓
4. Executed your commands
        ↓
5. Collected the output
        ↓
6. Determined SUCCESS/FAILURE
Enter fullscreen mode Exit fullscreen mode

This is the foundation of CI/CD.


Step 5 — Make the Job More Useful

Go to:

my-first-job → Configure

Replace the shell commands with:

echo "================================"
echo "      JENKINS DEVOPS LAB"
echo "================================"

echo ""

echo "Current user:"
whoami

echo ""

echo "Current directory:"
pwd

echo ""

echo "Files:"
ls -la

echo ""

echo "Git version:"
git --version

echo ""

echo "Java version:"
java -version

echo ""

echo "Current date:"
date

echo ""

echo "================================"
echo "BUILD COMPLETED"
echo "================================"
Enter fullscreen mode Exit fullscreen mode

Save it.

Click:

Build Now

Then open:

Build History → #2 → Console Output

You should see information about the Jenkins environment.


Step 6 — Understand the Jenkins Workspace

One very important DevOps concept is the workspace.

When Jenkins runs a job, it needs a directory in which to work.

Your command:

pwd
Enter fullscreen mode Exit fullscreen mode

may show something similar to:

/Users/Shared/Jenkins/Home/workspace/my-first-job
Enter fullscreen mode Exit fullscreen mode

or another Jenkins workspace location depending on your installation.

Think of it as:

Jenkins
│
└── workspace
      │
      └── my-first-job
            │
            ├── source code
            ├── build files
            ├── test files
            └── generated artifacts
Enter fullscreen mode Exit fullscreen mode

Later, when Jenkins clones a GitHub repository, the repository will normally appear in this workspace.


Step 7 — Learn SUCCESS vs FAILURE

Now let's intentionally break the build.

Go to:

my-first-job → Configure → Build → Execute shell

Use:

echo "Starting application deployment..."

echo "Checking application..."

exit 1

echo "Deployment completed."
Enter fullscreen mode Exit fullscreen mode

Save and click:

Build Now

Open:

#3 → Console Output

You should get:

Finished: FAILURE
Enter fullscreen mode Exit fullscreen mode

Why?

Because:

exit 1
Enter fullscreen mode Exit fullscreen mode

means the command failed.

In Linux:

exit 0  = SUCCESS
exit 1+ = FAILURE
Enter fullscreen mode Exit fullscreen mode

This concept is extremely important for CI/CD.

For example:

Jenkins
   ↓
Run tests
   ↓
Tests return exit 0
   ↓
Continue deployment
Enter fullscreen mode Exit fullscreen mode

But:

Jenkins
   ↓
Run tests
   ↓
Tests return exit 1
   ↓
STOP
   ↓
Build FAILED
Enter fullscreen mode Exit fullscreen mode

This prevents bad code from reaching production.


Step 8 — Fix the Build

Go back to Configure and replace the script with:

echo "Starting Jenkins build..."

echo "Checking Git..."
git --version

echo "Checking Java..."
java -version

echo "Running application test..."

echo "Test passed!"

echo "Build completed successfully."

exit 0
Enter fullscreen mode Exit fullscreen mode

Run another build.

Expected result:

Finished: SUCCESS
Enter fullscreen mode Exit fullscreen mode

Step 9 — Create a Small Project

Now let's make the lab closer to real DevOps work.

On your Mac Terminal:

mkdir jenkins-first-project
cd jenkins-first-project
Enter fullscreen mode Exit fullscreen mode

Create a file:

touch app.sh
Enter fullscreen mode Exit fullscreen mode

Open it:

nano app.sh
Enter fullscreen mode Exit fullscreen mode

Paste:

#!/bin/bash

echo "================================"
echo "My Application"
echo "================================"

echo "Application started successfully!"

echo "Environment: Development"

echo "Version: 1.0.0"
Enter fullscreen mode Exit fullscreen mode

Save with:

CTRL + O
ENTER
CTRL + X
Enter fullscreen mode Exit fullscreen mode

Give it permission:

chmod +x app.sh
Enter fullscreen mode Exit fullscreen mode

Run:

./app.sh
Enter fullscreen mode Exit fullscreen mode

Expected:

================================
My Application
================================
Application started successfully!
Environment: Development
Version: 1.0.0
Enter fullscreen mode Exit fullscreen mode

Step 10 — Put It on GitHub

Initialize Git:

git init
git add .
git commit -m "Initial Jenkins lab"
Enter fullscreen mode Exit fullscreen mode

Create a GitHub repository called:

jenkins-first-project
Enter fullscreen mode Exit fullscreen mode

Then connect your local project to it:

git branch -M main
git remote add origin YOUR_GITHUB_REPOSITORY_URL
git push -u origin main
Enter fullscreen mode Exit fullscreen mode

Now you have:

Developer
   ↓
GitHub
   ↓
jenkins-first-project
Enter fullscreen mode Exit fullscreen mode

Step 11 — Connect Jenkins to GitHub

Go back to Jenkins.

Create another job:

New Item
Enter fullscreen mode Exit fullscreen mode

Name:

jenkins-github-job
Enter fullscreen mode Exit fullscreen mode

Choose:

Freestyle project
Enter fullscreen mode Exit fullscreen mode

Click OK.

Under:

Source Code Management

select:

Git
Enter fullscreen mode Exit fullscreen mode

Repository URL:

YOUR_GITHUB_REPOSITORY_URL
Enter fullscreen mode Exit fullscreen mode

For a public repository, credentials aren't necessary.

Set branch to:

*/main
Enter fullscreen mode Exit fullscreen mode

Step 12 — Tell Jenkins What to Run

Go to:

Build → Add build step → Execute shell

Paste:

echo "================================"
echo "JENKINS CI BUILD STARTED"
echo "================================"

echo ""
echo "Workspace:"
pwd

echo ""
echo "Source code:"
ls -la

echo ""
echo "Running application..."
chmod +x app.sh
./app.sh

echo ""
echo "================================"
echo "BUILD SUCCESSFUL"
echo "================================"
Enter fullscreen mode Exit fullscreen mode

Save.

Click:

Build Now
Enter fullscreen mode Exit fullscreen mode

Open:

Build History
→ #1
→ Console Output
Enter fullscreen mode Exit fullscreen mode

You should see Jenkins clone your GitHub repository and execute app.sh.


What You Built

Your architecture is now:

┌──────────────┐
│  Developer   │
│     Mac      │
└──────┬───────┘
       │
       │ git push
       ▼
┌──────────────┐
│    GitHub    │
│ Repository   │
└──────┬───────┘
       │
       │ git clone / checkout
       ▼
┌──────────────┐
│   Jenkins    │
│     Job      │
└──────┬───────┘
       │
       │ Execute shell
       ▼
┌──────────────┐
│    app.sh    │
└──────┬───────┘
       │
       ▼
   SUCCESS
Enter fullscreen mode Exit fullscreen mode

What your students should understand after Lab 1

They should be able to explain these concepts:

Concept Meaning
Jenkins Automation server
Job Task Jenkins executes
Build One execution of a job
Workspace Directory where Jenkins works
Console Output Logs from the build
Execute Shell Run Linux/macOS commands
Exit 0 Success
Exit 1 Failure
Git Source-code version control
GitHub Remote repository
CI Automatically build/test code changes

The most important takeaway is:

Jenkins takes work that a DevOps engineer could perform manually and executes it in a repeatable, automated way.

For your next lab, I recommend moving directly to Pipeline Job + first Jenkinsfile. That is where students start learning Jenkins the way it's commonly used in DevOps: GitHub → Jenkinsfile → stages → build → test → deploy.

Top comments (0)