Bird
Raised Fist0
Jenkinsdevops~5 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of having separate dev, staging, and prod stages in a Jenkins deployment pipeline?
easy
A. To deploy only to production without checks
B. To run the same tests multiple times unnecessarily
C. To test and verify code in different environments before final release
D. To slow down the deployment process

Solution

  1. Step 1: Understand pipeline stages

    Each stage (dev, staging, prod) represents a different environment for testing and validation.
  2. Step 2: Purpose of separate environments

    Separate environments help catch issues early and ensure stability before production deployment.
  3. Final Answer:

    To test and verify code in different environments before final release -> Option C
  4. Quick Check:

    Separate stages = safer deployment [OK]
Hint: Remember: dev, staging, prod = test, verify, release [OK]
Common Mistakes:
  • Thinking all stages do the same thing
  • Assuming staging is optional
  • Believing deployment skips testing
2. Which of the following is the correct syntax to define a stage named "Staging" in a Jenkins declarative pipeline?
easy
A. stage 'Staging' steps { echo 'Deploying to staging' }
B. stage: Staging { steps: { echo 'Deploying to staging' } }
C. stage { name: 'Staging' steps { echo 'Deploying to staging' } }
D. stage('Staging') { steps { echo 'Deploying to staging' } }

Solution

  1. Step 1: Recall Jenkins declarative syntax

    Stages are defined with stage('Name') { steps { ... } } blocks.
  2. Step 2: Check each option

    stage('Staging') { steps { echo 'Deploying to staging' } } matches correct syntax; others have invalid syntax or missing braces.
  3. Final Answer:

    stage('Staging') { steps { echo 'Deploying to staging' } } -> Option D
  4. Quick Check:

    Correct stage syntax = stage('Staging') { steps { echo 'Deploying to staging' } } [OK]
Hint: Use parentheses and braces exactly as in official docs [OK]
Common Mistakes:
  • Missing braces or parentheses
  • Using colons instead of braces
  • Incorrect keyword order
3. Given this Jenkins pipeline snippet, what will be the output if the staging stage fails?
pipeline {
  agent any
  stages {
    stage('Dev') {
      steps { echo 'Deploying to dev' }
    }
    stage('Staging') {
      steps { error 'Staging deployment failed' }
    }
    stage('Prod') {
      steps { echo 'Deploying to prod' }
    }
  }
}
medium
A. Deploying to dev\nError: Staging deployment failed\nProd stage skipped
B. Deploying to dev\nError: Staging deployment failed\nProd stage runs
C. Only Prod stage runs
D. Pipeline completes successfully without errors

Solution

  1. Step 1: Understand pipeline flow

    Pipeline runs stages in order: Dev, Staging, Prod.
  2. Step 2: Effect of error in Staging stage

    Error in Staging stops pipeline; Prod stage does not run.
  3. Final Answer:

    Deploying to dev\nError: Staging deployment failed\nProd stage skipped -> Option A
  4. Quick Check:

    Error in stage stops later stages [OK]
Hint: Error in a stage stops pipeline unless handled [OK]
Common Mistakes:
  • Assuming Prod runs after Staging error
  • Ignoring error messages
  • Thinking pipeline completes despite errors
4. You have this Jenkins pipeline snippet:
pipeline {
  agent any
  stages {
    stage('Dev') {
      steps { echo 'Deploying to dev' }
    }
    stage('Staging') {
      steps { sh 'deploy.sh' }
    }
    stage('Prod') {
      steps { echo 'Deploying to prod' }
    }
  }
}

The deploy.sh script is missing execute permission. What will happen when the pipeline runs the Staging stage?
medium
A. The Staging stage will succeed without running the script
B. The Staging stage will fail with a permission denied error
C. The pipeline will skip the Staging stage and continue to Prod
D. The pipeline will automatically fix permissions and run the script

Solution

  1. Step 1: Understand shell script execution in Jenkins

    Jenkins runs shell commands as is; missing execute permission causes failure.
  2. Step 2: Effect of missing execute permission

    Shell returns 'permission denied' error; stage fails and pipeline stops.
  3. Final Answer:

    The Staging stage will fail with a permission denied error -> Option B
  4. Quick Check:

    Missing execute permission = stage failure [OK]
Hint: Scripts must be executable before Jenkins runs them [OK]
Common Mistakes:
  • Assuming Jenkins fixes permissions automatically
  • Thinking stage will skip on error
  • Believing script runs without execute permission
5. You want to create a Jenkins pipeline that deploys to dev automatically, but requires manual approval before deploying to staging and prod. Which pipeline snippet correctly implements this?
hard
A. pipeline { agent any stages { stage('Dev') { steps { echo 'Deploying to dev' } } stage('Staging') { steps { input 'Approve Staging?' echo 'Deploying to staging' } } stage('Prod') { steps { input 'Approve Prod?' echo 'Deploying to prod' } } } }
B. pipeline { agent any stages { stage('Dev') { steps { echo 'Deploying to dev' } } stage('Staging') { steps { echo 'Deploying to staging' } } stage('Prod') { steps { echo 'Deploying to prod' } } } }
C. pipeline { agent any stages { stage('Dev') { steps { input 'Approve Dev?' echo 'Deploying to dev' } } stage('Staging') { steps { echo 'Deploying to staging' } } stage('Prod') { steps { echo 'Deploying to prod' } } } }
D. pipeline { agent any stages { stage('Dev') { steps { echo 'Deploying to dev' } } stage('Staging') { steps { input 'Approve Prod?' echo 'Deploying to staging' } } stage('Prod') { steps { input 'Approve Staging?' echo 'Deploying to prod' } } } }

Solution

  1. Step 1: Identify manual approval usage

    Jenkins input step pauses pipeline for manual approval.
  2. Step 2: Check stages with input

    pipeline { agent any stages { stage('Dev') { steps { echo 'Deploying to dev' } } stage('Staging') { steps { input 'Approve Staging?' echo 'Deploying to staging' } } stage('Prod') { steps { input 'Approve Prod?' echo 'Deploying to prod' } } } } uses input before staging and prod deployments, dev runs automatically.
  3. Final Answer:

    Pipeline with input steps before staging and prod deployments -> Option A
  4. Quick Check:

    Manual approval = input step before stage [OK]
Hint: Use input step to pause for approval in Jenkins [OK]
Common Mistakes:
  • Adding input to dev instead of staging/prod
  • Skipping input steps entirely
  • Mixing approval prompts between stages
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard