Chapter 21: Jenkins Pipeline and Jenkinsfile
Chala mitrano, aaj aapan Freestyle chya click fields kadhun code madhe — Declarative Jenkinsfile. Chapter 20 madhe Node.js manual job baghitale. Ithe toach badge Node app Pipeline script from SCM ne. Checkout, Build, Test, lab Live. Ghabru naka.
What you will learn in this chapter
- Declarative pipeline shape with agent any
- Pipeline job from SCM reading Jenkinsfile
- Full badge Node Jenkinsfile: Checkout, Build, Test, lab Live
- Credentials by id inside a Jenkinsfile
- timeout, retry, stash, approval, currentBuild, parameters, when, parallel
- A short Scripted pipeline and triggers in the file
- What green stage balls and red Console Output look like
Concepts in this chapter
- 21.1Why move Freestyle into a Jenkinsfile
- 21.2Declarative vs Scripted (pick Declarative)
- 21.3Create a Pipeline job from SCM
- 21.4Same Node.js app — project shape reminder
- 21.5Full Declarative Jenkinsfile for badge Node
- 21.6Optional: bind a credential by id (pattern only)
- 21.7Agent tools: Node must exist
- 21.8Multibranch awareness (not required for the lab)
- 21.9Freestyle vs Pipeline — same badge, two shapes
- 21.10Troubleshooting
- 21.11Declarative blocks you must be able to read
- 21.12Options: timeout, retry, timestamps, one build at a time
- 21.13Stash, approval, and currentBuild
- 21.14Parameters, when, parallel, and matrix
- 21.15Scripted Pipeline and a little Groovy
- 21.16Triggers written in the Jenkinsfile
The chapter recap is at the end of the last concept page.
Practice Tasks
Suppose we are on the DevOps team at Emirates, the airline company, working from Dubai. Fatima Al Mansoori leads the team, and we look after the seat booking service. Costs are counted in AED. In the tasks below we do real DevOps jobs on this project using what this lesson teaches. Emirates is a real company, but this project, the people, servers, IP addresses and numbers are made up for practice.
0 / 12
Easy Declarative Jenkinsfile: build and test
Why: Pipeline as code lives in Git with the app, so every change is reviewed.
What to do: Write a declarative Jenkinsfile: agent any; stage Build runs sh 'mvn -B package'; stage Test runs sh 'mvn test'.
- Start with pipeline { and close it with } at the end.
- Add agent, then stages { } with one stage('Name') { steps { } } per step.
- Put the commands inside steps using sh '...'.
- Count your { and } so every block is closed, then click Check answer.
Easy GitHub Actions: test on every push
Why: Running tests on every push catches bugs before they reach main.
What to do: Write .github/workflows/ci.yml named CI that runs on push. One job test on ubuntu-latest with steps: actions/checkout@v4, actions/setup-node@v4 with node-version 20, run npm ci, run npm test.
- Start from the top-level keys the task names.
- Indent child keys with 2 spaces (no tabs).
- Use "- " for list items and fill each value from the task.
- Check the colons and spacing, then click Check answer.
Medium Run the build inside a Docker agent
Why: A Docker agent gives a clean, versioned toolchain for every build.
What to do: Declarative pipeline with agent docker { image 'maven:3.9-eclipse-temurin-21' } and stage Build running sh 'mvn -B verify'.
- Start with pipeline { and close it with } at the end.
- Add agent, then stages { } with one stage('Name') { steps { } } per step.
- Put the commands inside steps using sh '...'.
- Count your { and } so every block is closed, then click Check answer.
Medium Pipeline environment variables
Why: Values set once in environment are reused by every stage.
What to do: Declarative pipeline, agent any, environment with IMAGE = 'emirates/seat-booking' and TAG = '1.1.4', and stage Image running sh 'docker build -t $IMAGE:$TAG .'.
- Start with pipeline { and close it with } at the end.
- Add agent, then stages { } with one stage('Name') { steps { } } per step.
- Put the commands inside steps using sh '...'.
- Count your { and } so every block is closed, then click Check answer.
Medium Compose: env file and restart policy
Why: Secrets live in an env file that is not committed, and services come back after a reboot.
What to do: Write compose.yaml: service worker with image emirates/seat-booking-worker:1.1.4, env_file .env, and restart: unless-stopped.
- Start from the top-level keys the task names.
- Indent child keys with 2 spaces (no tabs).
- Use "- " for list items and fill each value from the task.
- Check the colons and spacing, then click Check answer.
Medium Pass environment variables
Why: The same image runs in dev and prod; only the settings change.
What to do: Run emirates/seat-booking:1.1.4 detached, named seat-booking, with APP_ENV=production and PORT=9090, publishing 9090:9090.
- Read the task and pick the tool it needs (git, docker, kubectl, terraform, ansible...).
- Write the tool name, then the sub-command (the action word).
- Add the flags the task needs, each followed by its value.
- Add names, files or paths at the end, then click Check answer.
Medium Always archive, alert on failure
Why: post blocks run after the stages, whatever the result.
What to do: Declarative pipeline, agent any, one stage Build with sh 'make'. Add post: always → archiveArtifacts artifacts: 'build/**'; failure → echo 'Build failed'.
- Start with pipeline { and close it with } at the end.
- Add agent, then stages { } with one stage('Name') { steps { } } per step.
- Put the commands inside steps using sh '...'.
- Count your { and } so every block is closed, then click Check answer.
Medium Deploy only from main
Why: Feature branches are built and tested but never deployed.
What to do: Declarative pipeline, agent any, stage Deploy with when { branch 'main' } and steps sh './deploy.sh me-central-1'.
- Start with pipeline { and close it with } at the end.
- Add agent, then stages { } with one stage('Name') { steps { } } per step.
- Put the commands inside steps using sh '...'.
- Count your { and } so every block is closed, then click Check answer.
Hard Parallel test stages
Why: Running unit and lint at the same time halves feedback time.
What to do: Declarative pipeline, agent any, stage Checks containing parallel with stage Unit (sh 'npm test') and stage Lint (sh 'npm run lint').
- Start with pipeline { and close it with } at the end.
- Add agent, then stages { } with one stage('Name') { steps { } } per step.
- Put the commands inside steps using sh '...'.
- Count your { and } so every block is closed, then click Check answer.
Hard Use Jenkins credentials safely
Why: withCredentials masks the password and removes it after the block.
What to do: Inside stage Push steps, use withCredentials with usernamePassword(credentialsId: 'reg-cred', usernameVariable: 'U', passwordVariable: 'P') and run sh 'echo $P | docker login -u $U --password-stdin registry.emirates.example.com'. Wrap it in a full declarative pipeline with agent any.
- Start with pipeline { and close it with } at the end.
- Add agent, then stages { } with one stage('Name') { steps { } } per step.
- Put the commands inside steps using sh '...'.
- Count your { and } so every block is closed, then click Check answer.
Hard Manual approval before production
Why: A human sign-off stops an accidental production release.
What to do: Declarative pipeline, agent any, stage Prod with input message 'Deploy to production?' and steps sh './deploy.sh prod'.
- Start with pipeline { and close it with } at the end.
- Add agent, then stages { } with one stage('Name') { steps { } } per step.
- Put the commands inside steps using sh '...'.
- Count your { and } so every block is closed, then click Check answer.
Hard Matrix build on three Python versions
Why: A matrix tests every supported version in parallel.
What to do: Write a workflow (on: push) with job test on ubuntu-latest using strategy.matrix.python with "3.10", "3.11", "3.12". Steps: actions/checkout@v4, actions/setup-python@v5 with python-version ${{ matrix.python }}, run pytest.
- Start from the top-level keys the task names.
- Indent child keys with 2 spaces (no tabs).
- Use "- " for list items and fill each value from the task.
- Check the colons and spacing, then click Check answer.
Ravi Sir's Cards: 35 revision, interview and comparison cards for this chapter · मराठी cards