📖 Scenario: You are working on a Jenkins pipeline to automate testing for a software project. The project requires running integration tests in separate stages to ensure the system components work together correctly.
🎯 Goal: Build a Jenkins pipeline script with multiple integration test stages. Each stage will run a specific integration test command. You will create the pipeline structure, add configuration variables, implement the test stages, and finally print the test results.
📋 What You'll Learn
Create a Jenkins pipeline with a pipeline block
Define an environment variable TEST_COMMAND with the value echo Running integration test
Add two stages named Integration Test 1 and Integration Test 2
Each stage should run the TEST_COMMAND using a sh step
Print a message All integration tests completed at the end
💡 Why This Matters
🌍 Real World
Jenkins pipelines automate software testing to catch bugs early and ensure code quality before deployment.
💼 Career
Understanding Jenkins pipelines and test stages is essential for DevOps engineers and automation specialists to maintain reliable CI/CD workflows.
Progress0 / 4 steps
1
Create the Jenkins pipeline skeleton
Write a Jenkins pipeline script starting with a pipeline block and a stages section. Do not add any stages yet.
Jenkins
Hint
Start with pipeline { and inside it add stages {}.
2
Add environment variable for test command
Inside the pipeline block, add an environment section. Define a variable called TEST_COMMAND with the value echo Running integration test.
Inside the stages section, add two stages named Integration Test 1 and Integration Test 2. Each stage should run the shell command stored in TEST_COMMAND using sh.
Jenkins
Hint
Use stage('Name') { steps { sh TEST_COMMAND } } for each test stage.
4
Print completion message after tests
Add a final stage named Summary after the test stages. In this stage, use a sh step to print All integration tests completed.
Jenkins
Hint
Add a new stage with stage('Summary') { steps { sh 'echo All integration tests completed' } }.
Practice
(1/5)
1. What is the main purpose of an integration test stage in a Jenkins pipeline?
easy
A. To check if different parts of the software work together correctly
B. To deploy the software to production
C. To compile the source code
D. To clean up temporary files after deployment
Solution
Step 1: Understand the role of integration tests
Integration tests verify that different modules or components of software interact correctly.
Step 2: Identify the stage purpose in Jenkins
In Jenkins pipelines, integration test stages run after building the code but before deployment to ensure combined parts work well.
Final Answer:
To check if different parts of the software work together correctly -> Option A
Quick Check:
Integration test purpose = verify combined parts work [OK]
Hint: Integration tests check combined parts, not deployment or build [OK]
Common Mistakes:
Confusing integration tests with deployment steps
Thinking integration tests compile code
Assuming integration tests clean files
2. Which of the following is the correct syntax to define an integration test stage in a Jenkins declarative pipeline?
easy
A. stage: 'Integration Test' steps: sh 'run-tests.sh'
B. integrationTest { steps { sh 'run-tests.sh' } }
C. stage('Integration Test') { steps { sh 'run-tests.sh' } }
D. testStage('Integration Test') { shell 'run-tests.sh' }
5. You want to add an integration test stage that runs only if the build stage succeeds and skips deployment if tests fail. Which Jenkins pipeline snippet correctly implements this logic?
medium
A. stage('Integration Test') {
steps {
sh 'run-tests.sh'
}
}
stage('Deploy') {
steps {
sh 'deploy.sh'
}
}
B. stage('Integration Test') {
steps {
sh 'run-tests.sh'
}
post {
failure { error 'Tests failed, stop pipeline' }
}
}
stage('Deploy') {
steps {
sh 'deploy.sh'
}
}
C. stage('Integration Test') {
steps {
sh 'run-tests.sh || true'
}
}
stage('Deploy') {
steps {
sh 'deploy.sh'
}
}
D. stage('Integration Test') {
steps {
sh 'run-tests.sh'
}
}
stage('Deploy') {
when {
expression { currentBuild.result == null || currentBuild.result == 'SUCCESS' }
}
steps {
sh 'deploy.sh'
}
}
Solution
Step 1: Ensure integration tests run after build and affect deployment
The integration test stage runs normally after build; deployment should only happen if tests pass.
Step 2: Identify the correct sequential stage implementation
In Jenkins pipelines, stages run sequentially by default. A failure in 'Integration Test' (non-zero exit from sh) causes the pipeline to fail and skip the 'Deploy' stage. stage('Integration Test') { steps { sh 'run-tests.sh' } } stage('Deploy') { steps { sh 'deploy.sh' } } correctly implements this logic.
Final Answer:
stage('Integration Test') {
steps {
sh 'run-tests.sh'
}
}
stage('Deploy') {
steps {
sh 'deploy.sh'
}
} -> Option A