jenkins|October 07, 2022|2 min read

Jenkins Pipeline - How to run Automation on Different Environment (Dev/Stage/Prod), with Credentials

TL;DR

Define environment-specific variables in a map and override them using a choice parameter for dev/stage/prod. Combine with withCredentials to inject secrets per environment.

Jenkins Pipeline - How to run Automation on Different Environment (Dev/Stage/Prod), with Credentials

Introduction

I have an automation script, that I want to run on different environments, with a cron schedule. I’m using a web-service, and the endpoints are different for each environment.

Also, I would need credentials for calling that web-service apis.

Recommended read on How to Run job on Cron-schedule

Jenkinsfile for Run Automation on Different Environments

pipeline {
    agent {
        node {
            label "MyNode"
        }
    }
    triggers {
        //every 24 hours at 12 AM
        cron('0 0 * * *')
    }
    stages {
        stage('Install requirements') {
            when {
                not { changeset pattern: ".*", comparator: "REGEXP" }
            }
            steps {
                withCredentials([[
                    $class: 'UsernamePasswordMultiBinding',
                    credentialsId: 'my_api_key',
                    usernameVariable: 'ARTIFACTORY_USERNAME',
                    passwordVariable: 'ARTIFACTORY_PASSWORD']
                ]) {
                    sh """
                    python3.8 -m pip install virtualenv
                    python3.8 -m venv python_env
                    source python_env/bin/activate
                    python3.8 -m pip install --upgrade pip
                    python3.8 -m pip install -r requirements.txt -i https://${ARTIFACTORY_USERNAME}:${ARTIFACTORY_PASSWORD}@artifactory.myorg.com/artifactory/api/pypi/pypi-myorg-release/simple

                    deactivate
                    """
                }
            } 
        }
        stage('Run Workflow Scheduler') {
            parallel {
                stage('Run Scheduler for Dev') {
                    steps {
                        withCredentials([[
                            $class: 'UsernamePasswordMultiBinding',
                            credentialsId: 'mygen',
                            usernameVariable: 'API_USER',
                            passwordVariable: 'API_PASSWORD']
                        ]) {
                            sh """
                            echo "Running for Dev env"

                            export WEB_SERVICE_BASE_URL="https://my-dev.myorg.com/api/"

                            python3.8 -m venv python_env
                            source python_env/bin/activate
                    
                            python3.8 app.py
                            deactivate
                            """
                        }
                    }
                }
                stage('Run Scheduler for Stage') {
                    steps {
                        withCredentials([[
                            $class: 'UsernamePasswordMultiBinding',
                            credentialsId: 'mygen',
                            usernameVariable: 'API_USER',
                            passwordVariable: 'API_PASSWORD']
                        ]) {
                            sh """
                            echo "Running for Stage env"

                            export WEB_SERVICE_BASE_URL="https://my-stg.myorg.com/api/"

                            python3.8 -m venv python_env
                            source python_env/bin/activate
                    
                            python3.8 app.py
                            deactivate
                            """
                        }
                    }
                }
                stage('Run Scheduler for Prod') {
                    steps {
                        withCredentials([[
                            $class: 'UsernamePasswordMultiBinding',
                            credentialsId: 'mygen',
                            usernameVariable: 'API_USER',
                            passwordVariable: 'API_PASSWORD']
                        ]) {
                            sh """
                            echo "Running for Prod env"

                            export WEB_SERVICE_BASE_URL="https://my-prod.myorg.com/api/"

                            python3.8 -m venv python_env
                            source python_env/bin/activate
                    
                            python3.8 app.py
                            deactivate
                            """
                        }
                    }
                }
            }
        }
    }
}

Description

I’m doing following in above file:

  • Schedule job every 24 hours: cron('0 0 * * *')
  • Not to run on code commit
  • Taking credentials from Jenkins using UsernamePasswordMultiBinding, and exposing in environment variables
  • Installing python requirements
  • Run automation via shell script
  • Then running automation run step in parallel, for each environment (dev/stage/prod)
  • Setting web service URL in environment variable

Related Posts

Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Introduction In this post we will see following: How to schedule a job on cron…

How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

Introduction There are some cases, where I need another git repository while…

How to Fetch Multiple Credentials and Expose them in Environment using Jenkinsfile pipeline

How to Fetch Multiple Credentials and Expose them in Environment using Jenkinsfile pipeline

Introduction In this post, we will see how to fetch multiple credentials and…

Jenkinsfile - How to Create UI Form Text fields, Drop-down and Run for Different Conditions

Jenkinsfile - How to Create UI Form Text fields, Drop-down and Run for Different Conditions

Introduction I had to write a CICD system for one of our project. I had to…

Example Jenkin Groovy Pipeline Script for Building Python Projects with Git Events and Push to Artifactory

Example Jenkin Groovy Pipeline Script for Building Python Projects with Git Events and Push to Artifactory

Introduction In this post, we will see a sample Jenkin Pipeline Groovy script…

Python - How to Maintain Quality Build Process Using Pylint and Unittest Coverage With Minimum Threshold Values

Python - How to Maintain Quality Build Process Using Pylint and Unittest Coverage With Minimum Threshold Values

Introduction It is very important to introduce few process so that your code and…

Latest Posts

Deep Dive on Elasticsearch: A System Design Interview Perspective

Deep Dive on Elasticsearch: A System Design Interview Perspective

“If you’re searching, filtering, or aggregating over large volumes of semi…

Deep Dive on Apache Kafka: A System Design Interview Perspective

Deep Dive on Apache Kafka: A System Design Interview Perspective

“Kafka is not a message queue. It’s a distributed commit log that happens to be…

Deep Dive on Redis: Architecture, Data Structures, and Production Usage

Deep Dive on Redis: Architecture, Data Structures, and Production Usage

“Redis is not just a cache. It’s a data structure server that happens to be…

Deep Dive on API Gateway: A System Design Interview Perspective

Deep Dive on API Gateway: A System Design Interview Perspective

“An API Gateway is the front door to your microservices. Every request walks…

REST API Design: Pagination, Versioning, and Best Practices

REST API Design: Pagination, Versioning, and Best Practices

Every time two systems need to talk, someone has to design the contract between…

Efficient Data Modelling: A Practical Guide for Production Systems

Efficient Data Modelling: A Practical Guide for Production Systems

Most engineers learn data modelling backwards. They draw an ER diagram…