Core Concepts — The Assembly Line Blueprint
Before the factory owner can set up the assembly line, everyone needs to learn the vocabulary. Jenkins has a small set of core concepts — once you understand these, every Jenkins configuration makes sense.
The Big Picture
Jobs — The Assembly Line Tasks
A Job (or Project) is a single automated task in Jenkins. It's one assembly line that performs a specific sequence of steps. Jobs are configured, run, and monitored individually.
There are two types:
| Type | Purpose | When to use |
|---|---|---|
| Freestyle | Classic UI-configured jobs | Simple, one-off tasks |
| Pipeline | Code-defined in a Jenkinsfile | Complex, version-controlled workflows |
Always prefer Pipeline jobs. The pipeline definition lives in your Git repo (Jenkinsfile), so it's version-controlled and reviewed like any other code.
Pipelines — The Full Assembly Line
A Pipeline is a sequence of stages that run in order. It's defined in a Jenkinsfile and committed to your repository.
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'npm install'
sh 'npm run build'
}
}
stage('Test') {
steps {
sh 'npm test'
}
}
stage('Deploy') {
steps {
sh 'docker build -t campus-library .'
sh 'docker push riya/campus-library:latest'
}
}
}
}
Each part explained
| Part | Meaning | Analogy |
|---|---|---|
pipeline | The entire workflow | The full assembly line |
agent | Where to run (any available machine) | Which workstation gets the task |
stages | The major phases | The assembly line stations |
steps | Individual commands within a stage | Actions at each station |
A Pipeline is declarative — you describe what happens, and Jenkins figures out how to execute it. This is the recommended syntax.
Stages and Steps — Stations and Actions
Stages are the major phases of a pipeline (Build, Test, Deploy). Each stage contains one or more steps — individual commands.
stages {
stage('Build') {
steps {
sh 'echo "Compiling code..."'
sh 'npm run build'
sh 'echo "Build complete!"'
}
}
}
Stages are displayed separately in the Jenkins UI, making it easy to see which phase succeeded or failed.
Agents — The Workstations
An agent is a machine (physical, virtual, or container) that executes the pipeline. Jenkins can use:
| Agent type | Description |
|---|---|
agent any | Any available agent |
agent { label 'linux' } | An agent with the "linux" label |
agent { docker 'node:20' } | A fresh Docker container |
pipeline {
agent {
docker {
image 'node:20-alpine'
}
}
// ... stages run inside a fresh Node.js container
}
Docker agents give you a clean environment every time. No leftover files from previous builds. This is the most reliable approach.
Triggers — The Production Schedule
Triggers define when a pipeline runs automatically:
| Trigger | Purpose |
|---|---|
pollSCM | Check Git periodically for new commits |
cron | Run on a schedule (e.g., nightly) |
upstream | Run when another job completes |
| Webhook | Run instantly when code is pushed (recommended) |
triggers {
pollSCM('H/5 * * * *') // Check every 5 minutes
}
Webhooks are better than polling. Polling checks periodically (wasteful). Webhooks push instantly (efficient). Configure webhooks in your Git provider (GitHub, GitLab).
Shared Libraries — Reusable Factory Parts
If multiple pipelines repeat the same steps, extract them into a Shared Library:
def call(Map config) {
sh "docker build -t ${config.image} ."
sh "docker push ${config.image}"
sh "kubectl set image deployment/${config.name} web=${config.image}"
}
@Library('my-shared-library') _
pipeline {
agent any
stages {
stage('Deploy') {
steps {
deployApp image: 'riya/campus-library:1.0', name: 'campus-library'
}
}
}
}
Shared libraries are the "reusable jigs" of the factory. Write once, use everywhere.