0
0
Jenkinsdevops~5 mins

Environment variables access in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Environment variables store important information like passwords or settings outside your code. Accessing them in Jenkins helps keep your builds safe and flexible without hardcoding sensitive data.
When you want to keep passwords or API keys secret in your Jenkins jobs.
When you need to reuse the same configuration values across multiple build steps.
When you want to change settings without editing the Jenkins pipeline code.
When you want to pass system or custom values to your build scripts.
When you want to debug or log environment information during a build.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  environment {
    MY_SECRET = 's3cr3tValue'
    APP_ENV = 'production'
  }
  stages {
    stage('Show Env') {
      steps {
        echo "The secret is ${MY_SECRET}"
        echo "Running in environment: ${APP_ENV}"
      }
    }
  }
}

The environment block defines variables accessible in all stages.

Here, MY_SECRET and APP_ENV are set and used inside the echo steps to print their values.

Commands
Check that Jenkins CLI or job builder is installed and accessible before running pipelines.
Terminal
jenkins-jobs --version
Expected OutputExpected
1.0.0
Trigger the Jenkins pipeline named 'example-pipeline' and wait for it to finish, so you can see the output including environment variable usage.
Terminal
jenkins-cli build example-pipeline -s
Expected OutputExpected
[Pipeline] echo The secret is s3cr3tValue [Pipeline] echo Running in environment: production Finished: SUCCESS
-s - Waits for the build to complete and shows the console output.
View the console output of the last run of 'example-pipeline' to verify environment variables were accessed correctly.
Terminal
jenkins-cli console example-pipeline
Expected OutputExpected
[Pipeline] echo The secret is s3cr3tValue [Pipeline] echo Running in environment: production Finished: SUCCESS
Key Concept

If you remember nothing else from this pattern, remember: define environment variables in the Jenkinsfile's environment block to safely and easily access them in all pipeline steps.

Common Mistakes
Trying to access environment variables without defining them in the environment block or system settings.
The variables won't exist during the build, causing errors or empty values.
Always define environment variables in the Jenkinsfile environment block or configure them in Jenkins global or job-specific settings.
Hardcoding secrets directly in pipeline steps instead of using environment variables.
This exposes sensitive data in the code and logs, risking security.
Use environment variables or Jenkins credentials plugin to keep secrets safe and separate from code.
Summary
Define environment variables in the Jenkinsfile environment block for easy access.
Use echo steps to print and verify environment variable values during builds.
Trigger and check pipeline runs using Jenkins CLI commands to see environment variable usage.