Skip to main content
Jenkins Pipeline
CHAPTER 12

Deploying Applications with Jenkins

Updated: May 15, 2026
25 min read

# 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. 1. SCP (Secure Copy Protocol): Jenkins takes the zipped code and securely transfers it to the production server.
  1. 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.

How do we update a PHP application without the website going down for 30 seconds while files copy? The Symlink Strategy:
  1. 1. Jenkins SSH's into the server and creates a new folder named with the timestamp: /var/www/releases/2026-10-21-1200
  1. 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.
  1. 3. Once the files are fully copied, Jenkins runs a single, instantaneous Linux command to update a "shortcut" (Symlink). The shortcut /var/www/html suddenly points to the new timestamp folder.
  1. 4. The deployment is instant. Zero downtime.

6. Mini Project: Deploy a PHP App Automatically

Let's add a deployment stage to our Jenkinsfile utilizing basic SSH commands.

Step-by-Step Pipeline Concept:

groovy
123456789101112131415161718192021222324252627282930313233343536
pipeline {
    agent any
    environment {
        PROD_SERVER = 'user@192.168.1.50'
        WEB_DIR = '/var/www/html/myapp'
    }
    stages {
        stage('Build & Test') {
            steps {
                echo 'Code compiled and tested successfully...'
                sh 'tar -czf app.tar.gz ./src'
            }
        }
        stage('Deploy to Production') {
            steps {
                // We use an SSH Agent to securely provide the private key for login
                sshagent(['production-ssh-key-id']) {
                    
                    echo 'Transferring files to production...'
                    sh "scp -o StrictHostKeyChecking=no app.tar.gz ${PROD_SERVER}:/tmp/"
                    
                    echo 'Running deployment script on production server...'
                    sh """
                    ssh -o StrictHostKeyChecking=no ${PROD_SERVER} '
                        cd /tmp
                        tar -xzf app.tar.gz
                        rsync -a ./src/ ${WEB_DIR}/
                        rm app.tar.gz
                        sudo systemctl reload nginx
                    '
                    """
                }
            }
        }
    }
}

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=no to bypass SSH prompts. In a true production environment, you should pre-configure the knownhosts file 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/authorizedkeys file on the production server.

11. Exercises

  1. 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.
  1. 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., running php 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 root sudo privileges?

14. Summary

In Chapter 12, we completed the final mile of the CI/CD journey: pushing code to production. We explored how Jenkins utilizes secure protocols like SSH and SCP to interact with remote servers. We identified the catastrophic risks of slow file transfers on live sites and embraced the architectural pattern of Zero-Downtime deployments via symlink swapping. Finally, we acknowledged that deployments will inevitably fail, cementing the necessity of a rapid, automated rollback strategy.

15. Next Chapter Recommendation

Deploying to traditional Linux servers is common, but modern enterprises are moving to the cloud and container orchestration. How does Jenkins deploy to, and run inside, massive clusters? Proceed to Chapter 13: Jenkins and Kubernetes.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·