π§ GOAL OF THIS LAB
After this lab, you will clearly understand:
- What a Jenkins pipeline is
- How Jenkins executes scripts
- What agent means
- How stages and steps work
- How Jenkins handles success vs failure
- How logs are produced
- How Jenkins decides a build is green or red
This is foundation knowledge.
STEP 1 β CREATE A BASIC PIPELINE JOB
- Open Jenkins UI
- Click New Item
- Name:
basic-pipeline
- Select Pipeline
- Click OK
STEP 2 β WRITE THE SIMPLEST POSSIBLE PIPELINE
In Pipeline β Definition β Pipeline script, paste:
pipeline {
agent any
stages {
stage('Hello') {
steps {
echo 'Hello from Jenkins'
}
}
}
}
Click Save β Build Now
STEP 3 β UNDERSTAND WHAT JUST HAPPENED (IMPORTANT)
What Jenkins did internally
- Jenkins received a build request
- Jenkins allocated an agent
- Jenkins created a workspace
- Jenkins executed the steps
- Jenkins printed logs
- Jenkins marked build as SUCCESS
Line-by-line explanation
pipeline {
β‘οΈ Tells Jenkins: this job is a pipeline
agent any
β‘οΈ Run on any available Jenkins worker
β‘οΈ Jenkins needs a machine to run commands
stages {
β‘οΈ Pipeline is divided into visible phases
stage('Hello') {
β‘οΈ One logical step in the pipeline
steps {
β‘οΈ Actual commands executed
echo 'Hello from Jenkins'
β‘οΈ Prints to Jenkins console log
STEP 4 β ADD REAL SCRIPT EXECUTION
Now update the pipeline:
pipeline {
agent any
stages {
stage('Run Shell Commands') {
steps {
sh '''
echo "Jenkins is running a shell script"
date
whoami
pwd
'''
}
}
}
}
Build again.
STEP 5 β HOW JENKINS RUNS SCRIPTS (KEY CONCEPT)
Jenkins does NOT execute code magically.
It:
- Runs commands on the agent
- Uses the OS shell
-
Depends on:
- OS
- Installed tools
- User permissions
What you just saw
-
whoamiβ Jenkins user -
pwdβ workspace directory
π This explains 80% of Jenkins issues in real life
STEP 6 β UNDERSTAND WORKSPACE
Add this step:
sh 'ls -la'
Explain:
- Jenkins creates a workspace per job
- Files live only during the build (unless archived)
Workspace path example:
/var/jenkins_home/workspace/basic-pipeline
STEP 7 β SUCCESS VS FAILURE (CRITICAL)
Modify pipeline:
pipeline {
agent any
stages {
stage('Success') {
steps {
echo 'This stage will pass'
}
}
stage('Failure') {
steps {
sh 'exit 1'
}
}
stage('Never Runs') {
steps {
echo 'You will not see this'
}
}
}
}
Build.
What happened and WHY
-
exit 1= non-zero exit code -
Jenkins immediately:
- stops pipeline
- marks build as FAILED
- skips next stages
π This is how Jenkins protects production
STEP 8 β READ LOGS LIKE A DEVOPS ENGINEER
Click:
- Failed build
- Console Output
Learn to:
- Read from bottom up
- Identify failing command
- Understand exit codes
Jenkins debugging = log reading skill
STEP 9 β POST ACTIONS (WHAT HAPPENS AFTER BUILD)
Update pipeline:
pipeline {
agent any
stages {
stage('Run') {
steps {
sh 'exit 1'
}
}
}
post {
success {
echo 'Build succeeded'
}
failure {
echo 'Build failed'
}
always {
echo 'This always runs'
}
}
}
Build.
Why post section matters
- Cleanup
- Notifications
- Rollbacks
- Reports
STEP 10 β ENVIRONMENT VARIABLES (VERY BASIC)
pipeline {
agent any
environment {
MY_ENV = "hello"
}
stages {
stage('Env Demo') {
steps {
sh 'echo $MY_ENV'
}
}
}
}
Explain:
- Available in all stages
- Used for env-based logic
STEP 11 β HOW JENKINS REALLY WORKS (MENTAL MODEL)
Job created
β
Build triggered
β
Agent assigned
β
Workspace created
β
Stages executed in order
β
Shell commands run
β
Logs collected
β
Build result decided
π€ INTERVIEW FOUNDATION ANSWER
βA Jenkins pipeline runs on an agent, creates a workspace, executes shell commands step by step, and determines success or failure based on exit codes. Stages give visibility, and logs are used for debugging.β
π§ FINAL RULES TO REMEMBER
- Jenkins runs scripts, not magic
- Exit code controls pipeline flow
- Logs are your truth
- Workspace is temporary
- Pipelines are code, not UI
β You now understand Jenkins fundamentals
Next logical steps (when YOU say):
- Pipeline from GitHub
- Credentials usage
- Docker build
- Kubernetes deploy
π NEXT STEP: Jenkinsfile from GitHub (Pipeline as Code)
π― What you will learn in THIS step
You will understand:
- How Jenkins pulls code from GitHub
- How Jenkinsfile controls the job
- Why Pipeline-as-Code is mandatory in real teams
- How CI actually starts from a git push
This is the bridge from βlearning Jenkinsβ β βusing Jenkins professionally.β
π§ Mental shift (important)
Before
- Pipeline written in Jenkins UI
- Jenkins owns the logic
Now
- Pipeline lives in GitHub
- Git owns the logic
- Jenkins only executes
This is how real companies work.
STEP 1 β Create a GitHub Repository
Create a new repo:
jenkins-basic-pipeline
STEP 2 β Create Repo Structure
Inside the repo:
jenkins-basic-pipeline/
βββ Jenkinsfile
βββ hello.sh
STEP 3 β Create a Simple Script (hello.sh)
#!/bin/bash
echo "Hello from GitHub"
date
whoami
pwd
Make it executable:
chmod +x hello.sh
Commit and push.
STEP 4 β Create Jenkinsfile (VERY SIMPLE)
Create Jenkinsfile:
pipeline {
agent any
stages {
stage('Checkout') {
steps {
echo 'Code pulled from GitHub'
checkout scm
}
}
stage('Run Script') {
steps {
sh './hello.sh'
}
}
}
}
Commit and push.
STEP 5 β Create Jenkins Pipeline Job (SCM-based)
- Jenkins β New Item
- Name:
github-pipeline
- Select Pipeline
- Click OK
Configure:
- Definition: Pipeline script from SCM
- SCM: Git
- Repository URL: your GitHub repo
-
Branch:
main -
Script Path:
Jenkinsfile
Save.
STEP 6 β Run the Pipeline
Click Build Now
What Jenkins does internally
- Jenkins connects to GitHub
- Pulls repo
- Finds Jenkinsfile
- Executes stages
- Runs shell script
- Prints logs
- Marks build success
This is real CI.
STEP 7 β PROVE CI AUTOMATION (CRITICAL)
Edit hello.sh:
echo "New change from developer"
Commit and push.
Result:
- GitHub webhook triggers Jenkins
- Jenkins runs pipeline automatically
- No manual click
π This is Continuous Integration
STEP 8 β BREAK IT ON PURPOSE (LEARN FAILURE)
Edit hello.sh:
exit 1
Commit and push.
Observe:
- Pipeline fails
- Jenkins stops execution
- Console shows error
- Build marked β FAILED
Explain this clearly:
Jenkins protects the system by blocking bad changes.
STEP 9 β Fix It (RECOVERY)
Remove exit 1, commit, push.
Pipeline goes green again.
π This is the real DevOps feedback loop.
STEP 10 β What You Can Now Explain Confidently
You can now say:
βI use Jenkinsfile stored in GitHub. Jenkins pulls pipeline definitions from SCM, runs scripts on agents, and automatically triggers builds using webhooks whenever code changes.β
That is DevOps-level Jenkins knowledge.
Top comments (0)