Skip to main content

Command Palette

Search for a command to run...

Part 10: Environment Variables in Pipelines

Make it variables and reusable

Published
3 min read
Part 10: Environment Variables in Pipelines

Jenkins CI/CD Series

Part Article
1 Install Jenkins with Docker
2 Creating Your First Jenkins Pipeline
3 Jenkins SSH Remote Execution
4 Automating MySQL Backup to AWS S3
5 Making Jenkins Automation Scalable
6 Running Ansible from Jenkins
7 Jenkins Pipeline from GitHub
8 Multibranch Pipelines
9 Parameterized Pipelines
10 Environment Variables in Pipelines

Goal

Understand how to use environment variables in Jenkins pipelines

  • Define variables at:

    • Global level

    • Pipeline level

    • Stage level

Use environment variables to control pipeline behavior dynamically

Learn how to manage sensitive data securely


Purpose

The purpose of this part is to make your pipelines configurable, reusable, and secure.

So far, you have:

  • Static pipelines

  • Parameterized inputs

But real-world pipelines require:

  • Managing configuration like URLs, ports, and credentials

  • Handling different environments (Dev, UAT, Prod)

  • Avoiding hardcoding sensitive data in scripts

Environment variables help you:

  • Centralize configuration

  • Reuse pipelines across environments

  • Improve maintainability and readability

This is a core DevOps practice used in:

  • CI/CD pipelines

  • Kubernetes deployments

  • Cloud-native applications


Prerequisite

Before starting this part, ensure:

  • Ready to use Host and the directory structure to run Dockerfiles and docker-compose.yml (Refer to Part 1)

Step-by-step implementation

  • Prepare the code
pipeline {
    agent any

    environment {
        def myString = "Hello World"
        def myNumber = 10
        def myBool = true
    }

    stages{
        stage("Demo") {
            steps {
                echo "myString: ${myString}"
                echo "myNumber: ${myNumber}"
                echo "myBool: ${myBool}"
            }
        }
    }
}
  • Create pipeline "variable-jenkins."
  • Paste the code into the pipeline script
  • Build Now

  • Now we can see the pipeline using the variables given in the environment block

Done!!!


Conclusion

In this part, you upgraded your pipeline to a fully configurable and production-ready CI/CD system

You learned how to:

  • Use environment variables effectively in pipelines

  • Avoid hardcoding values

  • Manage configurations across environments

  • Improve pipeline reusability and security

This is how modern DevOps pipelines are designed:

  • Flexible across environments

  • Secure with proper secret handling

  • Easy to maintain and scale


🔚 Final Note (Series Completion)

🎉 Congratulations! we have completed the Jenkins CI/CD Mastery Series

We have built:

  • End-to-end CI/CD pipelines

  • Scalable automation workflows

  • Production-ready DevOps practices


🔗 Continue the Series

⬅️ Previous Article: Part 9 Parameterized Pipelines
➡️ Next Article: Home


⭐ If you found this article useful, follow https://ask-abhi.com for more DevOps tutorials.