0
0
Jenkinsdevops~5 mins

Deployment pipelines (dev, staging, prod) in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Deployment pipelines automate the process of moving software through different environments like development, staging, and production. This helps catch errors early and ensures smooth releases.
When you want to test new code in a safe environment before it reaches users
When you need to automatically deploy updates after code changes
When you want to keep development, testing, and production separate
When you want to reduce manual steps and human errors in deployment
When you want to quickly roll back if something goes wrong in production
Config File - Jenkinsfile
Jenkinsfile
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building the application...'
            }
        }
        stage('Deploy to Dev') {
            steps {
                echo 'Deploying to Development environment...'
            }
        }
        stage('Deploy to Staging') {
            steps {
                input message: 'Approve deployment to Staging?', ok: 'Deploy'
                echo 'Deploying to Staging environment...'
            }
        }
        stage('Deploy to Production') {
            steps {
                input message: 'Approve deployment to Production?', ok: 'Deploy'
                echo 'Deploying to Production environment...'
            }
        }
    }
}

This Jenkinsfile defines a pipeline with four stages:

  • Build: Simulates building the app.
  • Deploy to Dev: Automatically deploys to development.
  • Deploy to Staging: Waits for manual approval before deploying to staging.
  • Deploy to Production: Waits for manual approval before deploying to production.

This setup ensures code moves step-by-step through environments with checks.

Commands
This command updates the Jenkins pipeline configuration with the Jenkinsfile defining the deployment stages.
Terminal
jenkins-jobs --conf jenkins.ini update Jenkinsfile
Expected OutputExpected
Job updated successfully
Starts the Jenkins pipeline job named 'my-pipeline' and shows the build status live.
Terminal
jenkins-cli build my-pipeline -s
Expected OutputExpected
Started build #1 [Pipeline] Start of Pipeline [Pipeline] echo Building the application... [Pipeline] echo Deploying to Development environment... [Pipeline] input Proceeding after input [Pipeline] echo Deploying to Staging environment... [Pipeline] input Proceeding after input [Pipeline] echo Deploying to Production environment... [Pipeline] End of Pipeline Finished: SUCCESS
-s - Show live build status
Fetches the console output of build number 1 for the 'my-pipeline' job to verify deployment steps.
Terminal
jenkins-cli console my-pipeline 1
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] echo Building the application... [Pipeline] echo Deploying to Development environment... [Pipeline] input User approved deployment to Staging [Pipeline] echo Deploying to Staging environment... [Pipeline] input User approved deployment to Production [Pipeline] echo Deploying to Production environment... [Pipeline] End of Pipeline Finished: SUCCESS
Key Concept

If you remember nothing else from this pattern, remember: deployment pipelines move code through environments step-by-step with automated and manual checks to ensure safe releases.

Common Mistakes
Skipping manual approval steps in staging or production
This can cause untested or faulty code to reach users, leading to failures.
Always include input steps for manual approval before deploying to sensitive environments.
Not defining separate stages for dev, staging, and production
Without clear stages, it is hard to control and track where the code is deployed.
Define explicit stages in the Jenkinsfile for each environment.
Running deployment commands without checking build success
Deploying broken builds wastes time and risks production stability.
Ensure build stage completes successfully before deploying.
Summary
Create a Jenkinsfile with stages for build, dev, staging, and production deployments.
Use manual input steps to approve deployments to staging and production.
Run the pipeline job and monitor its progress with Jenkins CLI commands.