Deploying Applications with Jenkins
# CHAPTER 12
Deploying Applications with Jenkins
1. Introduction
We have built our code, tested it rigorously, and packaged it. Now comes the moment of truth: the "Continuous Deployment" phase. This is where Jenkins takes the finished artifact and pushes it to the live production server, making it available to the world. Deployments are inherently risky; if done poorly, the website goes down. In this chapter, we will explore the mechanisms Jenkins uses to deploy applications, focusing on traditional SSH/SCP workflows, the philosophy of Zero-Downtime deployments, and the critical necessity of Rollback strategies.2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the risks associated with live production deployments.
- Configure Jenkins to securely transfer files to a remote server using SCP/SSH.
- Execute remote shell commands on a production server via Jenkins.
- Understand the concept of "Zero-Downtime" (Blue/Green) deployments.
- Conceptualize a basic automated rollback strategy.
3. Beginner-Friendly Explanation
Imagine replacing the engine of a car while it is driving down the highway.- The Bad Deployment (Downtime): You stop the car in the middle of the road (Turn off the web server), rip the old engine out (Delete old code), put the new engine in (Copy new code), and start the car again. For 5 minutes, traffic is blocked, and customers are angry.
- The Good Deployment (Zero-Downtime): You build a second, identical car with the new engine next to it. While both cars are driving, you instantly teleport the driver from the old car to the new car. The driver never even notices the switch.
Jenkins automates the process of building the second car and flipping the switch instantly.
4. Traditional Deployments (SSH/SCP)
If you are not using Docker or Kubernetes, deploying a web application (like PHP or Node.js) usually involves copying files to a remote Linux server.- 1. SCP (Secure Copy Protocol): Jenkins takes the zipped code and securely transfers it to the production server.
-
2.
SSH (Secure Shell): Jenkins logs into the production server remotely and runs commands (e.g.,
unzip app.zip,systemctl restart nginx).
To do this securely, Jenkins uses plugins like "Publish Over SSH" or the sshagent block, which securely injects private SSH keys without exposing them.
5. Zero-Downtime Deployments (Symlink Swap)
How do we update a PHP application without the website going down for 30 seconds while files copy? The Symlink Strategy:-
1.
Jenkins SSH's into the server and creates a new folder named with the timestamp:
/var/www/releases/2026-10-21-1200
- 2. Jenkins copies all the new code into this new folder. The live website is still running from the old folder, so users are unaffected.
-
3.
Once the files are fully copied, Jenkins runs a single, instantaneous Linux command to update a "shortcut" (Symlink). The shortcut
/var/www/htmlsuddenly points to the new timestamp folder.
- 4. The deployment is instant. Zero downtime.
6. Mini Project: Deploy a PHP App Automatically
Let's add a deployment stage to ourJenkinsfile utilizing basic SSH commands.
Step-by-Step Pipeline Concept:
7. Real-World Scenarios
A media company was deploying a massive update to their CMS right before a major breaking news event. They used a simple FTP copy script in Jenkins. Because there were 10,000 files, the FTP transfer took 15 minutes. For those 15 minutes, the website was in a "partially overwritten" state, causing fatal PHP errors for millions of visitors. The engineering team redesigned the Jenkins pipeline to use the "Symlink Swap" strategy (Blue/Green deployment). The next deployment transferred all files silently in the background, and the actual cutover took 0.01 seconds, resulting in a flawless, zero-downtime release.8. Best Practices
- Rollbacks: If your new code crashes the database immediately after deployment, how fast can you undo it? Your pipeline architecture must include a fast rollback mechanism. Using the Symlink strategy, a rollback is as simple as running a script to point the symlink back to the previous timestamp folder.
9. Security Recommendations
-
Strict Host Key Checking: In the mini-project above, we used
-o StrictHostKeyChecking=noto bypass SSH prompts. In a true production environment, you should pre-configure theknownhostsfile on the Jenkins server to prevent Man-in-the-Middle (MITM) attacks during deployment.
10. Troubleshooting Tips
-
SSH Key Permissions: The most common deployment failure is "Permission denied (publickey)". This means the private SSH key stored in Jenkins does not match the public key stored in the
~/.ssh/authorizedkeysfile on the production server.
11. Exercises
-
1.
Explain why deploying files directly into the live web directory (
/var/www/html) using a slow file transfer protocol is a bad architectural decision.
- 2. Describe the concept of a "Rollback" and why it is a critical requirement for a CI/CD deployment pipeline.
12. FAQs
Q: Does Jenkins handle database migrations during deployment? A: Jenkins can trigger database migrations (e.g., runningphp artisan migrate via SSH), but database schema changes are highly complex. If a deployment fails and you must roll back the code, rolling back a database change is often very difficult without data loss.
13. Interview Questions
- Q: Describe the mechanics of a Zero-Downtime deployment using the Symlink swapping strategy on a Linux web server. How does this strategy facilitate instantaneous rollbacks?
-
Q: Your pipeline needs to execute a restart command (
systemctl restart nginx) on a remote production server via SSH. How do you configure the remote server to allow the Jenkins user to execute this specific command without granting it full rootsudoprivileges?