How to Set Up Jenkins for Automation: A Complete Beginner’s Guide to CI/CD

8.04K 0 0 0 0

📘 Chapter 3: Creating Your First Jenkins Job

🔐 Introduction

With Jenkins installed and configured, it’s time to take your first hands-on step into automation — by creating your very first Jenkins job. A Jenkins job is essentially a task or project that Jenkins will execute for you automatically. Whether it's building code, running tests, or deploying an application, a job is the core unit of execution in Jenkins.

This chapter walks you through the complete process of creating a Jenkins job, step-by-step. You’ll learn how to create freestyle jobs, connect to version control, add build steps, trigger actions, and view output — all with real-world examples and screenshots in mind.


🧠 What is a Jenkins Job?

A Jenkins job (or project) is a runnable task that can be configured to:

  • Pull code from a version control system
  • Compile and build applications
  • Run scripts, test suites, or deployments
  • Notify teams on success or failure
  • Execute at scheduled times or based on triggers

🧱 Types of Jenkins Jobs

Job Type

Description

Freestyle Project

GUI-based configuration for simple builds

Pipeline

Scripted or declarative Groovy pipelines for advanced automation

Multibranch Pipeline

Automatically builds branches and PRs from repositories

Maven Project

Simplifies Java builds using Maven

External Job

Monitor tasks outside Jenkins (used with cron, shell scripts, etc.)

In this chapter, we focus on the Freestyle Project, which is ideal for beginners.


🛠️ Step-by-Step: Create Your First Jenkins Job


Step 1: Log In to Jenkins

  1. Visit your Jenkins instance (e.g., http://localhost:8080)
  2. Use your admin credentials to log in
  3. You’ll land on the Jenkins Dashboard

Step 2: Create a New Freestyle Job

  1. Click on “New Item” in the left-hand menu
  2. Enter a name: HelloWorld-Build
  3. Select “Freestyle project”
  4. Click OK

You’ll be redirected to the job configuration screen.


Step 3: Configure Source Code Management (Git)

If you want to automate a project from GitHub:

  1. Scroll to Source Code Management
  2. Select Git
  3. Enter your Git repository URL
  4. Add credentials (if private repo)
  5. Optionally specify a branch (e.g., main)

Step 4: Add a Build Step

  1. Scroll to Build section
  2. Click “Add build step”Execute shell (Linux/Mac) or Execute Windows batch command
  3. Example command:

bash

 

echo "Hello Jenkins!"

You can also run:

bash

 

python3 myscript.py

npm install && npm run build

mvn clean package


Step 5: Set Triggers (Optional)

To automate job execution:

Trigger Type

Description

Poll SCM

Checks Git repo periodically (cron format)

Build periodically

Runs job at intervals (cron format)

GitHub webhook

Builds when code is pushed (via webhook)

Trigger from another project

Builds this job when another job finishes

Example for Poll SCM (every 15 mins):

bash

 

H/15 * * * *


Step 6: Post-build Actions (Optional)

  • Archive artifacts (e.g., build outputs, logs)
  • Send email notifications
  • Deploy to test/staging environments
  • Trigger downstream jobs

Step 7: Save and Run the Job

  1. Click Save
  2. You’ll return to the project dashboard
  3. Click “Build Now”
  4. Observe the build history and output

Step 8: View Console Output

  1. Under Build History, click on the build number (#1)
  2. Click “Console Output”
  3. You’ll see:

pgsql

 

Started by user admin

Running as SYSTEM

Building in workspace /var/jenkins_home/workspace/HelloWorld-Build

Hello Jenkins!

Finished: SUCCESS

Congratulations, you've created and run your first Jenkins job!


🧩 Understanding Jenkins Job Components

Section

Purpose

General

Set job name, description, and concurrency settings

Source Code Management

Connects to Git, Subversion, or Mercurial repositories

Build Triggers

Defines when to run the job (cron, webhook, etc.)

Build Environment

Pre-build scripts, cleanups, environment variables

Build Steps

Commands/scripts to compile, test, or run code

Post-build Actions

Actions like notifications, archiving, downstream triggers


🧪 Sample Use Cases for Freestyle Jobs

Use Case

How Jenkins Helps

Compile a Java app

Use Maven or Gradle and archive .jar files

Run tests for a Python app

Use pytest or unittest in shell commands

Pull and run a Node.js app

Use npm install && npm test

Deploy static files to FTP

Use shell script and FTP plugin

Backup a database nightly

Schedule backup script via CRON-style triggers


🔄 What Happens Behind the Scenes

When you run a job, Jenkins:

  1. Creates a workspace directory
  2. Pulls source code (if configured)
  3. Executes build/test commands
  4. Collects and stores logs
  5. Cleans up (if configured)

You can see all of this in the console output or the Workspace directory.


📘 Summary

Creating your first Jenkins job is like building your first robot — simple at first, but packed with potential. With Freestyle projects, you can quickly prototype automation tasks, build test scripts, or deploy apps. As you grow, you can evolve these jobs into powerful pipelines.


Jenkins jobs are the stepping stones to full CI/CD pipelines — learn the basics, then scale the automation.

Back

FAQs


1. What is Jenkins and why is it used in automation?

Jenkins is an open-source automation server that helps developers automate building, testing, and deploying code. It enables Continuous Integration and Continuous Delivery (CI/CD), making software delivery faster and more reliable.

2. What are the system requirements to install Jenkins?

To install Jenkins, you need:

  • Java 11 or later (Java 17 recommended)
  • At least 2 GB of RAM and 1 GB of disk space
  • A modern browser for accessing the Jenkins UI

3. What’s the easiest way to install Jenkins for beginners?

The simplest way is to use the Jenkins WAR file:

java -jar jenkins.war

Alternatively, you can use a Docker container for a quick and clean setup:

docker run -p 8080:8080 jenkins/jenkins:lts

4. How do I connect Jenkins to my GitHub repository?

Install the Git and GitHub plugins, then:

  • Create a new job or pipeline
  • Choose “Git” as the source code management
  • Enter the GitHub repo URL and credentials if needed
  • Add a webhook to trigger Jenkins builds on each push

5. What is a Jenkins Pipeline?

A pipeline is a script-based workflow written in Groovy DSL that defines your automation steps (e.g., build, test, deploy). Pipelines can be declarative (simplified) or scripted (flexible).

6. What plugins should I install first in Jenkins?

For basic automation, start with:

  • Git plugin (source control)
  • Pipeline plugin (workflow scripting)
  • Docker plugin (if using containers)
  • Blue Ocean (modern UI for pipelines)
  • Email Extension (build notifications)

7. Can Jenkins be used with Docker and Kubernetes?

Yes! Jenkins integrates with Docker for building images and with Kubernetes for scaling jobs using agents. Tools like Jenkins X also help automate deployments in Kubernetes.

8. How do I secure my Jenkins installation?

  • Use HTTPS for the web UI
  • Create individual user accounts
  • Configure role-based access controls
  • Mask secrets and tokens in logs
  • Regularly update plugins and Jenkins core

9. How do I trigger Jenkins builds automatically?

You can:

  • Use webhooks from GitHub/GitLab
  • Schedule jobs using CRON syntax
  • Trigger builds after another job completes
  • Use a poll SCM trigger to check for changes periodically

10. Is Jenkins free to use for commercial projects?

Yes! Jenkins is 100% free and open-source, licensed under MIT. You can use it in personal, educational, and commercial environments without restriction.