Ravindra BagaleCourses & study guides मराठी

Chapter 21: Jenkins Pipeline and Jenkinsfile

21.9 Freestyle vs Pipeline — same badge, two shapes

Why: the problem

Many students ask: if Freestyle and Pipeline both build the app, why learn both? Because in your first job, you will see both. Old jobs at a bank like HDFC may still be Freestyle. New jobs are Pipelines. You must read both and move one to the other. The best way to learn is to build the same badge app twice and compare. Freestyle is quick to click once. But it has no stages view, no easy parallel work, no code review, and it does not survive a restart in the middle. Pipeline can resume after a controller restart, shows each stage, and lives in Git. In this lesson you build both, put them side by side, and fill a comparison table with real numbers from your screen.

What we will build

You will build badge Node as a Freestyle job and as a Pipeline job. You will time both, restart Jenkins in the middle of a long build, and export the Freestyle config. You will finish with a mapping table you can use at work.

What: every term in plain English

  • Freestyle — Click based job with build steps and post-build actions.
  • Pipeline — Code based job with stages, in a Jenkinsfile.
  • Durability — A Pipeline can continue after a controller restart.
  • Stage view — The boxes view that shows each stage and its time.
  • Job DSL — A plugin that makes jobs from Groovy code. Different from Pipeline.
  • Configuration as Code — JCasC. Jenkins settings from a YAML file.
  • Build step — One action in Freestyle, like Execute shell.
  • Post-build action — Freestyle action after steps, like Archive artifacts.
Create Freestyle Create Pipeline Add a slow step Restart during build Look at both Export config
Same build as Freestyle and as Pipeline

How: step by step

  1. Create Freestyle

    New Item > badge-free > Freestyle > Git URL > Execute shell: npm ci && npm run build && npm test > Archive: dist/**
    

    What it does: The click version.

    Output

    Saved
    
  2. Create Pipeline

    New Item > badge-pipe > Pipeline from SCM
    

    What it does: The code version.

    Output

    Saved
    
  3. Add a slow step

    sleep 60 in a test stage
    

    What it does: Gives time to restart Jenkins.

    Output

    (file saved)
    
  4. Restart during build

    docker restart jenkins
    

    What it does: Tests durability.

    Output

    jenkins
    
  5. Look at both

    Open both build pages
    

    What it does: Freestyle is failed, Pipeline resumed.

    Output

    Resuming build at ... after Jenkins restart
    
  6. Export config

    docker exec jenkins cat /var/jenkins_home/jobs/badge-free/config.xml | wc -l
    

    What it does: Freestyle config size.

    Output

    58
    

Example 1: Freestyle steps as shown on screen

Stack. Freestyle

Goal. Freestyle steps as shown on screen.

Freestyle Execute shell

set -e
node --version
npm ci
npm run build
npm test
sleep 60

What each line does

  • set -e — Stop at the first failed command, like a stage would.
  • One big box — All commands in one step, so no stage view.
  • sleep 60 — Our slow step for the restart test.

Green output

Started by user admin
+ node --version
v20.15.1
+ npm ci
+ npm run build
+ npm test
Tests: 2 passed, 2 total
+ sleep 60
(Jenkins restarted)
Build was aborted
Finished: ABORTED

Expected result. Freestyle lost the build at restart.

Example 2: The Pipeline survives a restart

Stack. Pipeline durability

Goal. The Pipeline survives a restart.

Jenkinsfile

pipeline {
  agent any
  options { durabilityHint('MAX_SURVIVABILITY') }
  stages {
    stage('Build') { steps { sh 'npm ci && npm run build' } }
    stage('Test')  { steps { sh 'npm test' } }
    stage('Slow')  { steps { sleep time: 60, unit: 'SECONDS' } }
    stage('After') { steps { echo 'I ran after the restart' } }
  }
}

What each line does

  • durabilityHint('MAX_SURVIVABILITY') — Saves state often so the build can resume.
  • sleep step — A Jenkins step, so it can resume after restart.
  • After stage — Proves the build kept going.

Green output

[Pipeline] stage (Slow)
Sleeping for 1 min 0 sec
Resuming build at Sun Oct 25 11:20:15 IST 2026 after Jenkins restart
Waiting for reconnection of built-in before proceeding with build
[Pipeline] stage (After)
I ran after the restart
Finished: SUCCESS

Expected result. Pipeline kept its place and finished.

Example 3: The mapping table you use at work

Stack. Jenkins

Goal. The mapping table you use at work.

mapping.txt

Freestyle field                Jenkinsfile
Source Code Management       = checkout scm / git url
Build Triggers: Poll SCM     = triggers { pollSCM('H/5 * * * *') }
Build Environment: timestamps= options { timestamps() }
Build Environment: secrets   = withCredentials([...])
Execute shell                = sh '...'
Archive the artifacts        = archiveArtifacts 'dist/**'
Publish JUnit results        = junit 'junit.xml'
E-mail notification          = post { failure { mail ... } }
Restrict where this runs     = agent { label 'node20' }
Discard old builds           = options { buildDiscarder(...) }

What each line does

  • Left side — Exact Freestyle labels you see on the Configure page.
  • Right side — The Jenkinsfile line that does the same.
  • Use it — Go top to bottom on any Freestyle job.

Green output

badge-free  : 1 job, 58 lines XML, no stages, aborted on restart
badge-pipe  : 1 Jenkinsfile, 9 lines, 4 stages, resumed after restart

Expected result. Facts from your own screen beat theory in interviews.

Example 4: Run both jobs from the command line and compare

Stack. Jenkins REST API

Goal. Run both jobs from the command line and compare.

terminal

JENKINS=http://localhost:8080
AUTH=admin:API_TOKEN
curl -s -X POST -u $AUTH $JENKINS/job/badge-free/build
curl -s -X POST -u $AUTH $JENKINS/job/badge-pipe/build
sleep 60
for j in badge-free badge-pipe; do
  curl -s -u $AUTH "$JENKINS/job/$j/lastBuild/api/json?tree=result,duration" ; echo " $j"
done

What each line does

  • /job/NAME/build — Starts a build through the REST API. Use an API token, not your password.
  • sleep 60 — Give both time to finish.
  • api/json?tree= — Asks only for the result and duration fields.
  • Same API — Works the same for both job types, so scripts do not care.

Green output

{"_class":"hudson.model.FreeStyleBuild","duration":21544,"result":"SUCCESS"} badge-free
{"_class":"org.jenkinsci.plugins.workflow.job.WorkflowRun","duration":23102,"result":"SUCCESS"} badge-pipe

Expected result. Speed is almost the same. The win of Pipeline is history, stages and resume, not raw speed.

Verify: did it work?

  1. Time each stage

    badge-pipe > Pipeline Overview
    

    What it does: Stage times appear only for Pipeline.

    Output

    Build 9s, Test 4s, Slow 60s
    
  2. Compare console sizes

    wc -l on both consoleText downloads
    

    What it does: Both logs are similar size.

    Output

    41 and 47
    

When it is red 1

Build was aborted after restart (Freestyle)

Cause. Freestyle cannot resume.

Fix, step by step

  1. Move long jobs to Pipeline.

When it is red 2

Waiting for reconnection of agent before proceeding

Cause. The agent is still offline after restart.

Fix, step by step

  1. Bring the agent back.
  2. The build continues by itself.

When it is red 3

Unknown parameter(s) found for class type 'WorkflowScript': durabilityHint

Cause. Old Jenkins version.

Fix, step by step

  1. Upgrade Jenkins and Pipeline plugins.

Cleanup

  1. Delete both jobs

    Delete badge-free and badge-pipe
    

    What it does: Removes the practice jobs.

    Output

    Deleted
    
  2. Keep the mapping

    Save mapping.txt in your notes
    

    What it does: You will use it at work.

    Output

    (saved)
    

Ravindra Bagale's Tip

Same app don veles banva, farak swatah bagha. Restart nantar Pipeline chalu rahto. Lakshat theva!

Practice tasks

Easy Build both

Why: See the difference.

What to do: Make badge-free and badge-pipe.

How:
  1. Same commands.

Check: Both green.

Medium Restart test

Why: Durability matters.

What to do: Restart during sleep.

How:
  1. docker restart.

Check: Only Pipeline finishes.

Hard Convert a real job

Why: Work skill.

What to do: Use the mapping on a job.

How:
  1. Line by line.

Check: Pipeline green.

Easy Say Freestyle vs Pipeline in two lines

Why: If you can say it simply, you understand it. Juniors and interviewers want the short version first.

What to do: Write two short lines about Freestyle vs Pipeline for a fresher who knows only Linux basics.

How:
  1. Write what problem it solves.
  2. Write the one command or menu you use for it.
  3. Read it aloud to a friend.

Check: A fresher understands it without asking a question.

Easy Start a fresh lab Jenkins

Why: Practice needs a throwaway Jenkins. If you break it, you delete it and start again.

What to do: Run Jenkins LTS in Docker with a named volume.

How:
  1. docker volume create jenkins_home
  2. docker run -d --name jenkins -p 8080:8080 -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts-jdk17
  3. docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword

Check: The Jenkins unlock page opens on localhost:8080.

Medium Break it on purpose

Why: You must know the red message by face. During an outage there is no time to read slowly.

What to do: Cause one error from the "When it is red" part of this lesson and fix it.

How:
  1. Follow the error section.
  2. Copy the exact message into your notes.
  3. Write the fix in one line.

Check: You fixed it using only the steps, without searching the web.

Medium Write a one-page runbook

Why: At 2 a.m. nobody remembers where a file lives. A short page saves the on-call person.

What to do: Write a runbook page for this lesson.

How:
  1. Title, when to use, exact command, how to check, how to undo.

Check: A teammate can follow it alone.

Medium Repeat it on a cloud VM

Why: A laptop is not a server. Real Jenkins runs on EC2 or a company VM.

What to do: Do the main steps of this lesson again on a small Ubuntu 22.04 EC2 instance.

How:
  1. Launch t3.small with 20 GB disk.
  2. Open port 22 only to your IP.
  3. Repeat the steps and compare the output.

Check: Same green output on the VM as on your laptop.

Hard Explain it like an interview

Why: Interviewers want real stories, not definitions.

What to do: Record a 60 second answer about this lesson.

How:
  1. Say the problem, the command you ran, and one error you hit.

Check: Your answer has a real command and a real error message.

Hard Draw the flow top to bottom

Why: A drawing shows who talks to whom and in what order.

What to do: Draw this lesson as boxes from top to bottom.

How:
  1. Start at the developer push, go through the controller and agent, end at the result.

Check: Each box matches a real thing you saw on screen.

Homework: try at home

Try at home

At home, build both jobs, restart Jenkins during the slow step, and write your own comparison with real numbers.

Recap

  • Both job types build apps.
  • Pipeline has stages, code review and durability.
  • Freestyle fields map one to one to Jenkinsfile lines.
  • Use real numbers to compare.
  • New jobs should be Pipelines.

FAQ

Should I delete all Freestyle jobs now?

Move them one by one, after the Pipeline is green for some days.

Is Job DSL the same as Pipeline?

No. Job DSL creates jobs; Pipeline runs builds.

Can Freestyle run in parallel?

Not inside one job easily. Pipeline has parallel.

Does Pipeline need more plugins?

Yes, the Pipeline plugin set.

What about JCasC?

It sets Jenkins itself as code; together with Jenkinsfiles everything is code.

How do I find all Freestyle jobs left?

Open the Jenkins home page and filter by job type, or run a Script Console query that prints jobs of type FreeStyleProject.

Can a Freestyle job call a Pipeline job?

Yes, with Trigger builds on other projects. It is a common bridge during migration.

Interview questions

  1. Freestyle vs Pipeline?

    Freestyle is click config with no stages or resume. Pipeline is code in Git with stages, parallel, and durability.

  2. How do you migrate a Freestyle job?

    Map each field to a Jenkinsfile line, run both, then delete the Freestyle job.

  3. What is pipeline durability?

    The ability to resume a running build after a controller restart.

  4. Job DSL vs Pipeline?

    Job DSL generates job configs; Pipeline describes how one job builds.

  5. Why do companies still have Freestyle jobs?

    Legacy. Teams migrate slowly.

Quick quiz

Q1. Which job type can resume after restart?

Pipeline.

Q2. What replaces Publish JUnit?

junit.

Q3. Which shows stage times?

Pipeline.

Q4. What replaces Restrict where this runs?

agent label.

Q5. What does set -e do in Freestyle?

Stops at the first failure.

Samajla ka?

Ravi Sir's Cards for this lesson

मराठी cards