0
0
Jenkinsdevops~30 mins

Kubernetes agents in Jenkins - Mini Project: Build & Apply

Choose your learning style9 modes available
Kubernetes Agents with Jenkins
📖 Scenario: You are setting up a Jenkins pipeline that runs jobs on Kubernetes agents. This allows Jenkins to create fresh, isolated environments for each job automatically.
🎯 Goal: Build a Jenkins pipeline script that defines a Kubernetes agent with a specific container image, runs a simple shell command inside the container, and prints the output.
📋 What You'll Learn
Create a Jenkins pipeline with a Kubernetes agent block
Specify the container image alpine:3.14 for the agent
Run a shell command echo Hello from Kubernetes agent inside the container
Print the command output in the Jenkins console
💡 Why This Matters
🌍 Real World
Many companies use Jenkins with Kubernetes agents to run CI/CD jobs in isolated, scalable environments.
💼 Career
Understanding Kubernetes agents in Jenkins is valuable for DevOps engineers managing cloud-native build pipelines.
Progress0 / 4 steps
1
Define a basic Jenkins pipeline
Create a Jenkins pipeline script with a pipeline block and an empty agent section.
Jenkins
Need a hint?

Start with the pipeline block and add an empty agent block inside it.

2
Configure the Kubernetes agent with container image
Modify the agent block to use kubernetes with a containerTemplate specifying the container image alpine:3.14 and container name alpine.
Jenkins
Need a hint?

Use containerTemplate inside kubernetes agent to specify the container image and name.

3
Add a stage to run a shell command inside the container
Add a stage named Run Command with a steps block that uses container('alpine') to run sh 'echo Hello from Kubernetes agent'.
Jenkins
Need a hint?

Inside stages, add a stage with steps that run the shell command inside the alpine container.

4
Print the output of the shell command
Add a script block inside container('alpine') to capture the output of sh(script: 'echo Hello from Kubernetes agent', returnStdout: true) into a variable output and then print it using echo output.
Jenkins
Need a hint?

Use sh with returnStdout: true inside a script block to capture and print the command output.