📖 Scenario: You work as a DevOps engineer for a small software team. They want to build a simple Jenkins pipeline that combines both Continuous Integration (CI) and Continuous Deployment (CD) steps. This hybrid approach will first build and test the code, then deploy it automatically if tests pass.This project will guide you to create a Jenkinsfile that defines this hybrid CI/CD pipeline in four clear steps.
🎯 Goal: Build a Jenkins pipeline script (Jenkinsfile) that:Defines the source code repositorySets a branch name variable for deploymentRuns build and test stagesDeploys the application if tests succeed
📋 What You'll Learn
Create a pipeline block with agent any
Define a variable branchName with value main
Add Build and Test stages with simple shell commands
Add a Deploy stage that runs only if tests pass
Print a message after deployment
💡 Why This Matters
🌍 Real World
Hybrid CI/CD pipelines are common in real projects to automate building, testing, and deploying software efficiently and safely.
💼 Career
Understanding Jenkins pipelines and hybrid CI/CD approaches is essential for DevOps roles to automate software delivery and improve team productivity.
Progress0 / 4 steps
1
Setup Jenkins pipeline skeleton
Create a Jenkins pipeline script starting with pipeline block and agent any to run on any available agent.
Jenkins
Hint
Start with pipeline { and inside it add agent any to tell Jenkins to run on any agent.
2
Add branch variable for deployment
Inside the pipeline block, add an environment section and define a variable called branchName with the value main.
Jenkins
Hint
Use the environment block to set variables accessible in all stages.
3
Add Build and Test stages
Add a stages block inside the pipeline. Inside it, create two stages: Build and Test. Each stage should run a simple shell command: echo "Building..." for Build and echo "Testing..." for Test.
Jenkins
Hint
Use stage('Name') blocks inside stages. Use steps with sh to run shell commands.
4
Add Deploy stage and print message
Add a third stage called Deploy inside stages. It should run the shell command echo "Deploying branch $branchName". This stage should run only if the previous stages succeed. Finally, add a post block to print Pipeline completed successfully! after all stages.
Jenkins
Hint
Use when with an expression to run Deploy only if previous stages succeeded. Use post { success { ... } } to print a message after the pipeline finishes successfully.
Practice
(1/5)
1. What is the main purpose of using a hybrid CI/CD approach in Jenkins pipelines?
easy
A. To combine automatic and manual steps for safer deployments
B. To run only manual deployment steps without automation
C. To automate all steps without any manual intervention
D. To disable pipeline triggers and run jobs manually
Solution
Step 1: Understand hybrid CI/CD concept
Hybrid CI/CD mixes automatic and manual steps to balance speed and control.
Step 2: Identify Jenkins pipeline usage
Jenkins pipelines use the input step to pause for manual approval, enabling safer deployments.
Final Answer:
To combine automatic and manual steps for safer deployments -> Option A
Quick Check:
Hybrid CI/CD = mix auto + manual [OK]
Hint: Hybrid means mixing auto and manual steps [OK]
Common Mistakes:
Thinking hybrid means fully manual
Assuming hybrid means fully automated
Confusing hybrid with disabling triggers
2. Which Jenkins pipeline syntax correctly adds a manual approval step?
easy
A. manualApproval('Approve deployment?')
B. input message: 'Approve deployment?'
C. pause('Approve deployment?')
D. waitForInput('Approve deployment?')
Solution
Step 1: Recall Jenkins input step syntax
The input step pauses the pipeline and asks for manual approval with a message.
Step 2: Match syntax to options
Only input message: 'Approve deployment?' is valid Jenkins pipeline syntax for manual input.
Final Answer:
input message: 'Approve deployment?' -> Option B
Quick Check:
Jenkins manual step = input message [OK]
Hint: Use 'input message:' for manual approval in Jenkins [OK]
Common Mistakes:
Using non-existent commands like manualApproval
Confusing input with pause or waitForInput
Missing the 'message:' keyword
3. Given this Jenkins pipeline snippet, what happens when it runs?
B. Build and deploy run automatically without pause
C. Deploy runs first, then build runs after approval
D. Build runs, then pipeline waits for manual approval before deploying
Solution
Step 1: Analyze pipeline stages order
Stages run in order: 'Build' first, then 'Deploy'.
Step 2: Understand input step effect
The input step pauses the pipeline and waits for manual approval before continuing to echo 'Deploying...'.
Final Answer:
Build runs, then pipeline waits for manual approval before deploying -> Option D
Quick Check:
input pauses pipeline for approval [OK]
Hint: Input pauses pipeline until manual approval [OK]
Common Mistakes:
Assuming deploy runs automatically without pause
Thinking input step causes failure
Confusing stage order
4. You added this input step in your Jenkins pipeline but it never pauses for approval:
input 'Approve deployment?'
What is the likely problem?
medium
A. The input step requires a named parameter like message: to work
B. The input step must be inside a script block
C. The input step only works in declarative pipelines
D. The input step needs a timeout parameter
Solution
Step 1: Check input step syntax
The input step requires named parameters like message: 'text' for proper execution.
Step 2: Identify missing named parameter
Using just a string without message: causes Jenkins to ignore the input step and not pause.
Final Answer:
The input step requires a named parameter like message: to work -> Option A
Quick Check:
Input needs message: parameter [OK]
Hint: Always use 'message:' with input step [OK]
Common Mistakes:
Omitting message: parameter
Thinking input must be in script block
Assuming timeout is mandatory
5. You want a Jenkins pipeline that builds automatically, then waits for manual approval, and finally deploys only if approved within 10 minutes. Which snippet correctly implements this hybrid CI/CD approach?