CHAPTER 08
CI/CD Workflows with Jenkins
Updated: May 15, 2026
25 min read
# CHAPTER 8
CI/CD Workflows with Jenkins
1. Introduction
A pipeline is simply a script that runs commands. The true value of Jenkins lies in the *logic* of the workflow you design. A poorly designed workflow will slow developers down; a well-designed CI/CD workflow acts as an automated quality control inspector, catching bugs before they reach customers. In this chapter, we will design a professional CI/CD workflow, explicitly defining the stages of Build, Test, and Deploy, and understanding how Jenkins acts as a strict gateway for code quality.2. Learning Objectives
By the end of this chapter, you will be able to:- Map out a standard Continuous Integration workflow.
- Understand the sequence of Build, Test, and Deploy stages.
- Explain "Fail Fast" methodology in pipeline design.
- Differentiate between staging and production deployment workflows.
- Architect a basic CI/CD pipeline for a web application (e.g., PHP/Node.js).
3. Beginner-Friendly Explanation
Imagine a car manufacturing plant's quality control line.- The Build Stage (Assembly): The robot puts the engine in the car. If the engine doesn't fit, the robot hits the red button, the line stops, and the car is thrown out.
- The Test Stage (The Dyno): The car moves to the next station. The robot turns the key. If the engine doesn't start, the robot hits the red button. The line stops.
- The Deploy Stage (The Showroom): The car passed assembly and testing. A human inspector looks at it, nods, and the robot drives the car into the customer showroom.
A CI/CD workflow is a strict sequence. The code must survive every stage. If it fails a stage, Jenkins hits the red button and rejects the code.
4. The Anatomy of a Professional Workflow
A standard CI/CD pipeline contains the following sequence:- 1. Checkout: Download the latest code from Git. (Jenkins does this implicitly when using Pipeline from SCM).
-
2.
Build/Install Dependencies: For PHP, this means running
composer install. For Node.js,npm install. This gathers all required third-party libraries.
- 3. Static Analysis (Linting): Checking the code for syntax errors without running it. (e.g., "Did the developer forget a semicolon?")
- 4. Unit Testing: Running automated scripts to verify individual functions work correctly.
- 5. Archive/Package: Zipping up the approved code or building a Docker image.
- 6. Deploy to Staging: Pushing the code to a private server so QA teams can test it.
- 7. Deploy to Production (Manual Approval): Waiting for a manager to click "Approve" before pushing to the live servers.
5. The "Fail Fast" Methodology
Pipelines should be designed to Fail Fast. If a developer writes broken code, they should know immediately. You should always put the fastest tests (like Syntax Linting, which takes 2 seconds) *before* the slow tests (like End-to-End browser testing, which might take 10 minutes). If the syntax is broken, Jenkins immediately fails the build at minute 1, saving 9 minutes of wasted server compute time.6. Mini Project: Build a CI/CD Workflow for a PHP App
Let's translate the standard workflow into a DeclarativeJenkinsfile for a PHP application.
Step-by-Step Pipeline Concept:
groovy
7. Real-World Scenarios
A startup had a pipeline where the "Deploy to Staging" step happened *before* the "Unit Testing" step. The developers thought this was fine because "we test it on the staging server anyway." One day, a developer pushed code with a catastrophic syntax error. Jenkins blindly deployed it to the staging server, crashing the staging server entirely. The QA team couldn't work for hours while IT rebuilt the server. The DevSecOps engineer rewrote the pipeline logic: Code is strictly tested in isolated Jenkins workspaces *first*, and only deployed if it mathematically proves it functions correctly.8. Best Practices
- Isolate Environments: The environment where Jenkins builds and tests the code (the Jenkins Workspace) must be completely separate from the Staging and Production web servers. Jenkins should act as a bridge, testing in isolation, and then pushing only the clean, verified artifact to the final destination.
9. Security Recommendations
-
Clean the Workspace: During the
Install Dependenciesstage, malicious code can sometimes be downloaded if a dependency is compromised (Supply Chain Attack). Ensure your Jenkins pipeline always starts with a completely clean workspace to prevent compromised files from lingering between builds.
10. Troubleshooting Tips
-
Permissions Errors: If your
sh 'composer install'step fails with a "Permission Denied" error, it is because the Linux user running Jenkins (usually thejenkinsuser) does not have read/write access to the directories. Ensure the Jenkins user has ownership of the workspace.
11. Exercises
- 1. Explain the "Fail Fast" pipeline methodology. Give an example of two stages and explain which should run first.
- 2. Why is it dangerous to deploy code to a server before running Unit Tests?
12. FAQs
Q: How do I add a manual approval step before deploying to Production? A: Jenkins provides aninput step. You can add a stage that says input message: 'Deploy to Production?'. The pipeline will pause indefinitely, wait for an authorized human to log into the Jenkins UI and click "Proceed," and then continue to the deployment stage.
13. Interview Questions
- Q: Outline a standard CI/CD workflow for a web application. Justify the sequence of the stages in terms of execution time and risk mitigation.
- Q: A developer complains that their builds take 20 minutes to fail because Jenkins is running heavy integration tests before simple syntax linting. How do you architect the pipeline to improve developer feedback loops?