Jenkins Complete Learning Guide
Jenkins Complete Learning Guide
Install | Configure | Freestyle Jobs | Declarative Pipelines | Docker | Agents | Best Practices
Prepared for: Pavan | Cloud & DevOps Engineer | Accenture Interview Prep
■ Table of Contents
What is Jenkins?
Jenkins is an open-source automation server written in Java. It is the most widely used CI/CD tool in the world with over 1,800
plugins. Jenkins automates the process of building, testing, and deploying software so developers don't have to do it manually
every time they push code. Think of Jenkins as a robot that watches your code repository and automatically runs tasks
whenever code changes.
ANAL Real-world analogy: Jenkins is like a factory assembly line. When a developer pushes code (raw material), Jenkins picks it
OGY up, builds it, tests it, and ships it to production — automatically, every single time.
Jenkins Controller Main server. Manages jobs, UI, scheduling, plugins. Does NOT Factory Manager
(Master) run builds in production.
Jenkins Agent Worker machine that actually executes the build steps. Can be Factory Worker
(Node/Slave) any machine.
Executor A single build slot on an agent. One executor = one job at a Worker's Hands
time. Agents can have many.
Plugin Extends Jenkins with extra features — Git, Docker, Slack, AWS, Tool Add-ons
etc. 1,800+ available.
Jenkinsfile Pipeline defined as code, stored in your Git repo. The blueprint Recipe / Instruction Sheet
for your build.
Workspace Folder on the agent where your code is checked out and build Worker's Desk
happens.
Blue Ocean Modern visual UI for Jenkins pipelines. Shows stages as swim Dashboard Screen
lanes.
docker run -d \
-p 8080:8080 \
-p 50000:50000 \
-v jenkins_home:/var/jenkins_home \
--name jenkins \
jenkins/jenkins:lts
# Go to: [Link]
-d means run in background (detached). -p 8080:8080 maps port 8080 on your machine to Jenkins. -v
PORT
jenkins_home:/var/jenkins_home saves all Jenkins data so it persists even if container restarts.
[Link]
[Link] binary/" | \
# Open: [Link]
Go to [Link]
2 Install Plugins
New Item Create button (top left) Create a new Job or Pipeline
Build Executor Status Right sidebar Shows what's running right now on each agent
Job List Center of dashboard All your jobs and their last build status
Build History Left sidebar on job page List of all past builds with pass/fail
Console Output Inside each build The actual terminal output — use to debug
failures
Credentials Under Manage Jenkins Where you store API keys, passwords, SSH keys
Yellow / Warning UNSTABLE Build ran but test failures were found.
Grey / Not run NOT BUILT Job was never triggered or skipped by condition.
Chapter 4 — Freestyle Jobs — Your First Build
Click OK
If private repo: click 'Add' under Credentials and add your GitHub token
Branch: */main
Check 'GitHub hook trigger for GITScm polling' — auto build on push
Type your commands: echo 'Hello Jenkins!' and pwd and ls -la
pwd
ls -la
KEY Pipeline as Code = your pipeline lives in your repo alongside your application code. When you change the Jenkinsfile, the
IDEA pipeline changes. No clicking in the UI required.
Select 'Pipeline'
Click OK
Script Path: Jenkinsfile (default — it looks for this file in your repo root)
APP_NAME = 'my-app'
VERSION = '1.0.0'
echo 'Building...'
stage('Test') {
steps {
echo 'Testing...'
pipeline {} Wrapper for entire declarative pipeline Always — every Jenkinsfile starts
with this
agent any Run on any available agent/node Default choice — use unless you
need specific agent
agent none No global agent — each stage picks its own When different stages need
different environments
agent { label 'linux' } Run only on agents with this label When you have dedicated build
servers
agent { docker 'python:3.11' } Run inside a Docker container When you want clean isolated
environments
stages {} Container for all your stages Always — wraps your stage()
blocks
sh 'command' Run a shell command on Linux/Mac Most common step — runs any
bash command
script {} Run Groovy code inside declarative pipeline When you need if/else logic or
loops
when {} Conditional — stage only runs if condition is true Deploy only on main branch, etc.
when { branch 'main' } Run stage only when on main branch Production deployments
when { expression {} } Run stage based on any custom condition Complex conditions using
variables
parallel {} Run multiple stages at the same time Speed up pipeline — run tests in
parallel
triggers {} What automatically starts this pipeline GitHub webhook, scheduled cron,
upstream job
success {} Inside post — runs only on success Success Slack message, deploy
notifications
failure {} Inside post — runs only on failure Alert team, rollback, page on-call
input {} Pause pipeline and wait for human approval Manual gate before production
deployment
withCredentials {} Inject secrets safely into steps AWS keys, Docker registry
passwords
junit 'path' Parse and publish JUnit test results After running tests — shows
pass/fail in UI
archiveArtifacts Save files from build as downloadable artifacts Save JAR files, test reports, built
binaries
Chapter 7 — Complete Pipeline Stages — Build, Test, Deploy
agent any
environment {
APP_NAME = 'flask-api'
DOCKER_REPO = 'yourdockerhub/flask-api'
DEPLOY_ENV = 'staging'
options {
triggers {
stages {
stage('Checkout') {
steps {
stage('Install') {
steps {
stage('Lint') {
steps {
sh 'flake8 . --max-line-length=88'
stage('Test') {
parallel {
stage('Unit Tests') {
steps {
sh 'python -m pytest tests/unit -v --junitxml=[Link]'
stage('Integration Tests') {
steps {
post {
always {
stage('Docker Build') {
steps {
script {
stage('Docker Push') {
steps {
withCredentials([usernamePassword(
credentialsId: 'dockerhub-creds',
usernameVariable: 'DOCKER_USER',
passwordVariable: 'DOCKER_PASS'
)]) {
stage('Deploy Staging') {
steps {
stage('Approve Production') {
input {
steps {
stage('Deploy Production') {
steps {
sh './scripts/[Link]'
post {
always {
success {
slackSend(color: 'good',
failure {
slackSend(color: 'danger',
emailext(to: 'team@[Link]',
}
Chapter 8 — Jenkins Agents & Distributed Builds
agent any
agent none
agent {
docker {
image 'python:3.11-slim'
pipeline {
stages {
stage('Build on Linux') {
stage('Test on Windows') {
1 Go to Node Management
Save the node — Jenkins gives you a command to run on the agent machine
On the agent machine, run the provided java -jar [Link] command
docker ps
steps {
script {
withCredentials([usernamePassword(
credentialsId: 'dockerhub-creds',
usernameVariable: 'USER',
passwordVariable: 'PASS'
)]) {
// ID: aws-creds
environment {
AWS_REGION = 'ap-south-1'
ECR_ACCOUNT = '123456789012'
ECR_REPO = 'my-flask-api'
steps {
withCredentials([[
$class: 'AmazonWebServicesCredentialsBinding',
credentialsId: 'aws-creds'
]]) {
script {
// Login to ECR
}
Chapter 10 — Credentials & Secrets Management
Username + Password Docker Hub, GitHub, any login Username with password
SSH Private Key SSH into servers, GitHub SSH SSH Username with private key
AWS Credentials AWS Access Key + Secret Key AWS Credentials (needs plugin)
ID: give it a name like 'github-token' — this is how you reference it in Jenkinsfile
Click OK
3 Use in Jenkinsfile
withCredentials Examples
// Example 1: Username + Password
withCredentials([usernamePassword(
credentialsId: 'github-token',
usernameVariable: 'GIT_USER',
passwordVariable: 'GIT_TOKEN'
)]) {
withCredentials([string(
credentialsId: 'slack-webhook-url',
variable: 'SLACK_URL'
)]) {
withCredentials([sshUserPrivateKey(
credentialsId: 'deploy-server-key',
keyFileVariable: 'SSH_KEY',
usernameVariable: 'SSH_USER'
)]) {
NEVER hardcode passwords or tokens in your Jenkinsfile. Always store them in Jenkins Credentials and use
WARN
withCredentials() to inject them. If you accidentally commit a secret to Git, rotate it immediately.
Chapter 11 — Jenkins Plugins — Must-Know List
Git Plugin Connects Jenkins to GitHub/GitLab/Bitbucket Required for any project with Git
Blue Ocean Modern visual UI for pipelines Makes stages visible and easy to
debug
Docker Pipeline Build/push Docker images in Jenkinsfile using For Docker-based builds
[Link]()
Docker Plugin Use Docker containers as build agents Run each stage in clean container
AWS Credentials Plugin Store and use AWS access keys Required for any AWS deployment
Amazon ECR Plugin Push Docker images to Amazon ECR For AWS container deployments
Slack Notification Plugin Send build result messages to Slack Team notifications on pass/fail
Email Extension Plugin Send rich HTML emails on build events Email alerts with full build logs
GitHub Integration Plugin GitHub webhooks trigger builds automatically Auto-trigger on push
Credentials Plugin Manage and inject secrets safely Always needed — installed by
default
Workspace Cleanup Plugin Adds cleanWs() step to delete workspace Keep agents disk-clean
JUnit Plugin Parse and display test results in UI Show test pass/fail in build page
Timestamper Plugin Add timestamps to console output Know exactly when each step ran
Build Timeout Plugin Kill builds that hang too long Prevent stuck builds blocking agents
SSH Agent Plugin Use SSH keys in pipeline steps Deploy to remote servers via SSH
Chapter 12 — Shared Libraries — Reusable Pipeline Code
my-jenkins-library/
[Link]
[Link]
[Link]
org/company/[Link]
[Link]
vars/ Functions
// vars/[Link]
// ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
// vars/[Link]
pipeline {
agent any
stages {
steps {
dockerBuildPush('myrepo/my-app', env.BUILD_NUMBER)
post {
success { sendSlackNotify('Build passed!') }
ID: slack-webhook
post {
success {
slackSend(
color: 'good',
"Branch: ${GIT_BRANCH}\n" +
"URL: ${BUILD_URL}"
failure {
slackSend(
color: 'danger',
// Email notification
post {
failure {
emailext(
to: 'team@[Link]',
}
Chapter 14 — Common Errors & How to Fix Them
No such DSL method 'docker' Docker Pipeline plugin not installed Install Docker Pipeline plugin in Plugin
Manager
Permission denied ([Link]) Jenkins user not in docker group Run: sudo usermod -aG docker jenkins
&& restart
unable to connect to GitHub: GitHub webhook URL wrong or firewall Check Jenkins URL in Manage Jenkins
Connection refused blocking → Configure System. Use ngrok for
local testing.
Script not permitted to use Groovy sandbox blocking your code Go to: Manage Jenkins → In-process
staticMethod Script Approval → Approve the method
No credentials found for Credential ID doesn't match what's stored Check Manage Jenkins → Credentials
credentialsId: 'xyz' — confirm exact ID spelling
checkout scm: null Pipeline job not linked to Git SCM In job config, set Pipeline from SCM and
add repo URL
ERROR: No such file: Jenkinsfile Script Path is wrong or file not committed Check Script Path field matches actual
file name + path in repo
Workspace is locked Previous build still running or crashed Manage Jenkins → Nodes →
Disconnect & reconnect agent
[Link]: remote file Agent disk full Free disk space on agent. Enable
operation failed cleanWs() in post block.
Host key verification failed (SSH) SSH host not in known_hosts On agent, run: ssh-keyscan host >>
~/.ssh/known_hosts
Build keeps queuing but never No available executors / agents offline Check Manage Jenkins → Nodes —
starts bring offline agents online
Chapter 15 — Practice Labs — Hands-On Exercises
5. Run the pipeline and see all 3 stages pass (green boxes)
Goal: Build a Docker image of your Flask app and push to Docker Hub
Goal: Run unit tests and linting in parallel to speed up the pipeline
2. After it, add a stage with input{} block asking for approval
4. Run the pipeline — it will pause and wait for your click
4. Add post{} block to your pipeline with success and failure blocks
A: Jenkins is an open-source automation server used for CI/CD. It solves the problem of manual, error-prone software builds and
deployments. Without Jenkins, every developer runs builds manually, integration conflicts pile up, and deployments are risky.
Jenkins automates the entire process: detect code push → build → test → deploy. This gives teams faster feedback and reliable,
repeatable deployments.
Q2: What is the difference between a Freestyle Job and a Pipeline Job?
A: A Freestyle Job is configured through the Jenkins UI by filling form fields — no code needed. Easy for beginners but hard to
version control. A Pipeline Job is defined as code in a Jenkinsfile stored in your Git repo. It supports complex logic, parallel stages,
conditions, and is the modern recommended approach. The key advantage of Pipeline: it is versioned, reviewable, and shareable
just like your application code.
A: A Jenkinsfile is a text file written in Groovy DSL that defines your entire CI/CD pipeline as code. It lives in the root of your Git
repository alongside your application code. It contains all pipeline stages, steps, conditions, and post-build actions. When Jenkins
detects a change in your repo, it reads the Jenkinsfile and executes the pipeline automatically.
A: The Jenkins Controller (master) is the main server that manages jobs, the UI, scheduling, and plugins. It does NOT run builds
itself in production. Agents (nodes) are worker machines that execute the actual build steps. When a build is triggered, the
Controller assigns it to a free Agent. The Agent checks out code, runs the stages, and reports results back to the Controller. This
allows Jenkins to scale — many agents running builds in parallel.
A: The post block defines actions to run after all pipeline stages complete, regardless of success or failure. It has conditions:
always{} runs every time, success{} only runs if all stages passed, failure{} only runs if something failed, unstable{} runs if tests
failed but build succeeded. Typical use: always cleanWs() to delete workspace, success send a Slack green message, failure send
a Slack red alert and email the team.
A: Never hardcode secrets in Jenkinsfile. Store them in Jenkins Credentials Manager under Manage Jenkins → Credentials.
Supported types: Username+Password, Secret Text, SSH Keys, AWS Credentials. In the Jenkinsfile use withCredentials() block
which injects the secret as an environment variable. Jenkins automatically masks the secret value in logs — even if you accidentally
echo it, it appears as ****.
A: The when directive makes a stage conditional — it only runs when the condition is true. Common examples: when { branch 'main'
} runs only on main branch, when { expression { return [Link] == 'yes' } } runs based on a custom condition. This is how
you deploy to production only from the main branch while feature branches only run tests.
Q9: What Jenkins plugins do you know and what do they do?
A: Key plugins: Git Plugin connects Jenkins to GitHub/GitLab. Pipeline plugin enables Jenkinsfile support. Blue Ocean gives a
visual pipeline UI. Docker Pipeline lets you build/push images in code. AWS Credentials stores AWS keys. Slack Notification sends
build alerts to Slack. Email Extension sends rich HTML emails. GitHub Integration enables automatic webhook triggers. Workspace
Cleanup adds cleanWs() to delete workspace after builds.
A: Two methods: 1) GitHub Webhook — In GitHub repo Settings, add a Webhook pointing to
[Link] In Jenkins job, check 'GitHub hook trigger for GITScm polling'. When developer pushes
code, GitHub instantly notifies Jenkins. 2) Poll SCM — Jenkins checks the repo every N minutes using a cron expression. Less
efficient than webhooks but works without public Jenkins URL.
Quick Reference — Commands & Cheat Sheet
Start with Lab 1 today — install Jenkins in Docker and run your first pipeline. Each lab builds on the previous one. By
Lab 6 you will have a fully working CI/CD pipeline with Docker, GitHub integration, and Slack notifications. That is
exactly what Accenture wants to see. You've got this, Pavan!