80.3 Project 69: Declarative Jenkinsfile pipeline
Friends, code reviews need the pipeline in git. Today we write a Declarative Jenkinsfile with stages, agent labels, post actions, and a Multibranch tip.
मित्रांनो, pipeline git मध्ये हवी. आज Declarative Jenkinsfile, stages, agent labels, post actions, आणि Multibranch tip.
दोस्तों, pipeline git में चाहिए. आज Declarative Jenkinsfile, stages, agent labels, post actions, और Multibranch tip.
What you will build in this project
- Write a Declarative Jenkinsfile.
- Add Checkout Test Package stages.
- Use agent labels and post always.
- Create a Pipeline job that points at the file.
1. Start from a real company story
At Wipro in Hyderabad, freestyle clicks differed per team and a release used the wrong Maven goals.
Rough cost: about ₹15 lakh in rollback and customer credits.
Ananya moved every service to a Jenkinsfile in the repo.
Today we write that Declarative file.
Ravindra Bagale's Tip
Students put credentials as plain environment strings in the Jenkinsfile. Use credentials() helpers. Keep that habit.
Students credentials plain env मध्ये ठेवतात. credentials() helpers वापरा. सवय ठेवा.
Students credentials plain env में रखते हैं. credentials() helpers इस्तेमाल करें. आदत रखिए.
2. Start and prepare
Step 1: Why Jenkinsfile
Why: UI clicks are not reviewable. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: echo why. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 1: a1
Type this:
echo 'Jenkinsfile in git = reviewable pipeline as code'
You will see:
Jenkinsfile in git = reviewable pipeline as code
What it means: Why.
Example 2: a2
Type this:
mkdir -p /tmp/jen69/repo && ls /tmp/jen69
You will see:
repo
What it means: Folder.
Example 3: a3
Type this:
echo 'Today: Declarative stages, label, post, Pipeline job'
You will see:
Today: Declarative stages, label, post, Pipeline job
What it means: Scope.
Step 2: Skeleton file
Why: pipeline block. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: printf Jenkinsfile. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 4: b1
Type this:
printf '%s\n' 'pipeline {' ' agent { label \'maven\' }' ' options { timestamps() }' ' stages {' ' stage(\'Checkout\') {' ' steps { checkout scm }' ' }' ' }' '}' > /tmp/jen69/repo/Jenkinsfile && head -8 /tmp/jen69/repo/Jenkinsfile
You will see:
agent { label 'maven' }
What it means: Skeleton.
Example 5: b2
Type this:
wc -l /tmp/jen69/repo/Jenkinsfile
You will see:
9 /tmp/jen69/repo/Jenkinsfile
What it means: Lines.
Example 6: b3
Type this:
echo 'Declarative starts with pipeline { ... } — not node {'
You will see:
Declarative starts with pipeline { ... } — not node {
What it means: Syntax tip.
Step 3: Test stage
Why: mvn test. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: add stage. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 7: c1
Type this:
printf '%s\n' ' stage(\'Test\') {' ' steps {' ' sh \'mvn -B test\'' ' }' ' }' >> /tmp/jen69/repo/Jenkinsfile && tail -6 /tmp/jen69/repo/Jenkinsfile
You will see:
sh 'mvn -B test'
What it means: Test stage.
Example 8: c2
Type this:
echo 'Use sh on Linux agents; bat on Windows agents'
You will see:
Use sh on Linux agents; bat on Windows agents
What it means: Shell tip.
Example 9: c3
Type this:
echo 'Fail the stage if tests fail — default Maven behaviour'
You will see:
Fail the stage if tests fail — default Maven behaviour
What it means: Fail.
3. Build the main pieces
Step 4: Package stage
Why: mvn package. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: add stage. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 10: d1
Type this:
printf '%s\n' ' stage(\'Package\') {' ' steps {' ' sh \'mvn -B -DskipTests package\'' ' }' ' }' >> /tmp/jen69/repo/Jenkinsfile && grep Package /tmp/jen69/repo/Jenkinsfile
You will see:
stage('Package') {
What it means: Package.
Example 11: d2
Type this:
echo 'skipTests here only because Test stage already ran'
You will see:
skipTests here only because Test stage already ran
What it means: Why skip.
Example 12: d3
Type this:
echo 'Never skip tests globally without a Test stage above'
You will see:
Never skip tests globally without a Test stage above
What it means: Habit.
Step 5: Post actions
Why: always and success. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: close stages and post. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 13: e1
Type this:
printf '%s\n' ' }' ' post {' ' always { junit \'target/surefire-reports/*.xml\' }' ' success { archiveArtifacts artifacts: \'target/*.jar\', fingerprint: true }' ' cleanup { cleanWs() }' ' }' '}' >> /tmp/jen69/repo/Jenkinsfile && tail -8 /tmp/jen69/repo/Jenkinsfile
You will see:
success { archiveArtifacts artifacts: 'target/*.jar', fingerprint: true }
What it means: Post.
Example 14: e2
Type this:
echo 'always runs on success and failure'
You will see:
always runs on success and failure
What it means: Always.
Example 15: e3
Type this:
echo 'cleanup keeps agents from filling disks'
You will see:
cleanup keeps agents from filling disks
What it means: Cleanup.
Step 6: Pipeline job
Why: Point at SCM. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: job tip. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 16: f1
Type this:
printf '%s\n' 'New Item -> Pipeline' 'Name: cart-pipeline' 'Definition: Pipeline script from SCM' 'Script Path: Jenkinsfile' > /tmp/jen69/JOB.txt && cat /tmp/jen69/JOB.txt
You will see:
Script Path: Jenkinsfile
What it means: Job.
Example 17: f2
Type this:
echo 'SCM URL = your cart repo; credentials from Jenkins store'
You will see:
SCM URL = your cart repo; credentials from Jenkins store
What it means: SCM.
Example 18: f3
Type this:
echo 'Build Now should paint stage boxes green'
You will see:
Build Now should paint stage boxes green
What it means: Expect.
4. Automate and verify
Step 7: Blue Ocean tip
Why: Stage view. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: read the UI. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 19: g1
Type this:
echo 'Open the run -> Pipeline Steps to see each sh'
You will see:
Open the run -> Pipeline Steps to see each sh
What it means: UI.
Example 20: g2
Type this:
echo 'Stage View plugin shows a grid of past runs'
You will see:
Stage View plugin shows a grid of past runs
What it means: Grid.
Example 21: g3
Type this:
printf '%s\n' 'Acceptance: Checkout Test Package all green' > /tmp/jen69/ACCEPT.txt && cat /tmp/jen69/ACCEPT.txt
You will see:
Acceptance: Checkout Test Package all green
What it means: Gate.
Step 8: Multibranch tip
Why: One job many branches. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: notes. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 22: h1
Type this:
printf '%s\n' 'Multibranch Pipeline scans branches for Jenkinsfile' 'PR builds appear as separate jobs' > /tmp/jen69/MULTIBRANCH.txt && cat /tmp/jen69/MULTIBRANCH.txt
You will see:
Multibranch Pipeline scans branches for Jenkinsfile
What it means: MB.
Example 23: h2
Type this:
echo 'Discover branches strategy: exclude branches without Jenkinsfile'
You will see:
Discover branches strategy: exclude branches without Jenkinsfile
What it means: Discover.
Example 24: h3
Type this:
echo 'Webhook project 70 will trigger scans automatically'
You will see:
Webhook project 70 will trigger scans automatically
What it means: Bridge.
Step 9: Credentials tip
Why: No plain secrets. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: withCredentials. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 25: i1
Type this:
echo 'withCredentials([string(credentialsId: \'sonar-token\', variable: \'SONAR_TOKEN\')]) { sh \'echo token length ${#SONAR_TOKEN}\' }'
You will see:
withCredentials([string(credentialsId: 'sonar-token', variable: 'SONAR_TOKEN')]) { sh 'echo token length ${#SONAR_TOKEN}' }
What it means: Helper.
Example 26: i2
Type this:
echo 'Mask passwords in console — Jenkins does this for credentials bindings'
You will see:
Mask passwords in console — Jenkins does this for credentials bindings
What it means: Mask.
Example 27: i3
Type this:
echo 'Never printf a secret in a shared library either'
You will see:
Never printf a secret in a shared library either
What it means: Habit.
5. Harden and accept
Step 10: Story Wipro
Why: Hyderabad freestyle drift. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: story. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 28: j1
Type this:
printf '%s\n' 'At Wipro in Hyderabad, freestyle clicks differed per team and a release used wrong Maven goals.' 'Ananya moved every service to a Jenkinsfile in the repo.' > /tmp/jen69/STORY.txt && wc -l /tmp/jen69/STORY.txt
You will see:
2 /tmp/jen69/STORY.txt
What it means: Story.
Example 29: j2
Type this:
echo 'Rough cost: about ₹15 lakh in rollback and customer credits'
You will see:
Rough cost: about ₹15 lakh in rollback and customer credits
What it means: Cost.
Example 30: j3
Type this:
echo 'Ananya: PASS'
You will see:
Ananya: PASS
What it means: Sign-off.
Step 11: Proof
Why: Jenkinsfile exists. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: grep stages. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 31: k1
Type this:
grep stage /tmp/jen69/repo/Jenkinsfile
You will see:
stage('Checkout') {\n stage('Test') {\n stage('Package') {
What it means: Stages.
Example 32: k2
Type this:
test -f /tmp/jen69/ACCEPT.txt && echo 'accept ok'
You will see:
accept ok
What it means: accept.
Example 33: k3
Type this:
echo 'Next: SCM webhook auto-build project 70'
You will see:
Next: SCM webhook auto-build project 70
What it means: Bridge.
Step 12: Acceptance
Why: Pipeline green from Jenkinsfile. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.
What: final. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.
Example 34: z1
Type this:
echo 'Declarative file in git'
You will see:
Declarative file in git
What it means: File.
Example 35: z2
Type this:
echo 'Three stages green'
You will see:
Three stages green
What it means: Stages.
Example 36: z3
Type this:
echo 'JAR archived on success'
You will see:
JAR archived on success
What it means: Artifact.
Check yourself
Q1. Who fixed Wipro freestyle drift?
Ananya
Q2. File name?
Jenkinsfile
Q3. Syntax?
Declarative
Q4. Agent key?
label
Q5. Post block?
always
Q6. Secrets in env plain?
no
Friends, you ran a Declarative Jenkinsfile pipeline. Did you understand? Let us try it once more. Add a stage named Quality that echoes a Sonar tip and keep the pipeline green.
मित्रांनो, तुम्ही Declarative Jenkinsfile pipeline चालवली. Quality नावाची stage जोडा जी Sonar tip echo करेल आणि pipeline green ठेवा.
दोस्तों, आपने Declarative Jenkinsfile pipeline चलाई. Quality नाम की stage जोड़ें जो Sonar tip echo करे और pipeline green रखें.
Lab: try at home
- Write Jenkinsfile.
- Create Pipeline job.
- Run and archive.
- Note Multibranch next step.
Recap
- Pipeline as code.
- Stages are readable.
- Labels pick agents.
- Post always cleans.
- No plain secrets.