Chapter 27: Jenkins Agents
Chala mitrano, aaj aapan agents: labels, inbound/SSH, Docker agent, Kubernetes pods that go away. Docker-in-Docker risky. Ghabru naka.
What you will learn in this chapter
- Labels and agent { label }
- Inbound vs SSH agents
- Maven Docker agent example
- Why Docker-in-Docker is risky
- Kubernetes pods that destroy after the build
Concepts in this chapter
- 27.1Labels
- 27.2Inbound or SSH
- 27.3One Maven Docker agent
- 27.4Docker-in-Docker is risky
- 27.5Kubernetes pods that go away
- 27.6Which agent for the badge
The chapter recap is at the end of the last concept page.
Practice Tasks
Suppose we are on the DevOps team at Bajaj Auto, the two-wheeler manufacturing company, working from Pune. Sachin Jadhav leads the team, and we look after the dealer stock app. Costs are counted in INR. In the tasks below we do real DevOps jobs on this project using what this lesson teaches. Bajaj Auto 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 Which port does the user hit?
Why: Mixing host and container ports is the most common docker mistake.
What to do: A container was started with -p 8443:8081. Which port do you open in the browser on the host?
- Write down the facts or numbers the task gives you.
- Work it out step by step.
- Type only the final answer (one word or number) in the box.
- Click Check answer.
Easy Compose: build from source
Why: Developers build the image locally with the same file the team uses.
What to do: Write compose.yaml with one service api that builds from the current folder (.) and publishes 8081:8081.
- 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 = 'bajajauto/dealer-stock' and TAG = '1.1.1', 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 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 ap-south-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.
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 bajajauto/dealer-stock-worker:1.1.1, 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.
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.bajajauto.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 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 Compose: wait for a healthy database
Why: The app crashes if it starts before the database is ready.
What to do: Write compose.yaml: db (mysql:8.0) with a healthcheck whose test is ["CMD", "mysqladmin", "ping", "-h", "localhost"] and interval 10s; app (image bajajauto/dealer-stock:1.1.1) that depends_on db with condition service_healthy.
- 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: 15 revision, interview and comparison cards for this chapter · मराठी cards