0
0
Jenkinsdevops~5 mins

Environment variables in builds in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
When building software, you often need to use values that can change, like passwords or server addresses. Environment variables let you store these values outside your code so you can reuse and change them easily during the build process.
When you want to keep sensitive data like API keys out of your code.
When you need to use different settings for testing and production builds.
When multiple builds share common settings like server URLs or version numbers.
When you want to pass custom values to scripts or commands during the build.
When you want to avoid hardcoding values that might change often.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
    agent any
    environment {
        APP_ENV = 'production'
        API_KEY = '12345-abcde'
    }
    stages {
        stage('Build') {
            steps {
                echo "Building in environment: ${APP_ENV}"
                sh 'echo API key is $API_KEY'
            }
        }
    }
}

This Jenkinsfile defines a pipeline with environment variables under the environment block.

APP_ENV and API_KEY are set once and used in the build steps.

This keeps sensitive or changeable data outside the code logic but accessible during the build.

Commands
Check that Jenkins CLI or job builder tools are installed and ready to use.
Terminal
jenkins-jobs --version
Expected OutputExpected
1.0.0
Start the Jenkins pipeline named 'my-pipeline' which uses environment variables defined in its Jenkinsfile.
Terminal
jenkins-cli build my-pipeline
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] echo Building in environment: production [Pipeline] sh + echo API key is 12345-abcde API key is 12345-abcde [Pipeline] End of Pipeline Finished: SUCCESS
View the console output of the last build to verify environment variables were used correctly.
Terminal
jenkins-cli console my-pipeline
Expected OutputExpected
Building in environment: production API key is 12345-abcde
Key Concept

If you remember nothing else from this pattern, remember: environment variables let you safely and flexibly pass data into your build without hardcoding it.

Common Mistakes
Hardcoding sensitive values directly in build scripts.
This exposes secrets in code repositories and makes changing values harder.
Use environment variables in the Jenkinsfile or Jenkins credentials store instead.
Referencing environment variables with wrong syntax like ${VAR} in shell steps.
Shell steps require $VAR syntax, so ${VAR} may not expand correctly.
Use $VAR inside shell commands and ${VAR} in Groovy strings.
Defining environment variables inside steps block instead of environment block.
Variables inside steps are not globally available to all stages or steps.
Define environment variables in the pipeline's environment block for global access.
Summary
Define environment variables in the Jenkinsfile environment block to share data across build steps.
Use $VAR syntax inside shell commands and ${VAR} in Groovy strings to access variables.
Run the pipeline and check console output to confirm variables are used correctly.