Environment variables are a powerful feature in Jenkins pipelines that allow you to manage and pass configuration data dynamically. They can be used to store sensitive information, such as API keys, or to configure different environments (e.g., development, staging, production).

Key Concepts

  1. Environment Variables: Variables that are set in the environment in which Jenkins jobs run. They can be used to pass data between different stages of a pipeline.
  2. Global Environment Variables: Variables that are available to all jobs and pipelines within Jenkins.
  3. Job-Specific Environment Variables: Variables that are specific to a particular job or pipeline.
  4. Built-in Environment Variables: Predefined variables provided by Jenkins, such as BUILD_NUMBER, JOB_NAME, etc.

Setting Environment Variables

Global Environment Variables

Global environment variables can be set in the Jenkins configuration:

  1. Navigate to Manage Jenkins > Configure System.
  2. Scroll down to the Global properties section.
  3. Check the Environment variables checkbox.
  4. Add the desired environment variables.

Job-Specific Environment Variables

Job-specific environment variables can be set within a pipeline script using the environment directive.

pipeline {
    agent any
    environment {
        MY_VAR = 'Hello, World!'
        API_KEY = credentials('my-api-key')
    }
    stages {
        stage('Example') {
            steps {
                echo "MY_VAR is ${env.MY_VAR}"
                echo "API_KEY is ${env.API_KEY}"
            }
        }
    }
}

Built-in Environment Variables

Jenkins provides several built-in environment variables that can be used in your pipelines:

Variable Description
BUILD_NUMBER The current build number
JOB_NAME The name of the job
WORKSPACE The workspace directory for the job
BUILD_URL The URL of the current build
GIT_COMMIT The commit hash of the current Git revision

Practical Example

Let's create a simple pipeline that demonstrates the use of environment variables.

pipeline {
    agent any
    environment {
        GREETING = 'Hello'
        NAME = 'Jenkins'
    }
    stages {
        stage('Print Greeting') {
            steps {
                script {
                    def message = "${env.GREETING}, ${env.NAME}!"
                    echo message
                }
            }
        }
        stage('Use Built-in Variables') {
            steps {
                echo "This is build number ${env.BUILD_NUMBER} of job ${env.JOB_NAME}"
            }
        }
    }
}

Explanation

  • Environment Directive: Sets the GREETING and NAME variables.
  • Print Greeting Stage: Constructs a message using the environment variables and prints it.
  • Use Built-in Variables Stage: Prints the build number and job name using built-in environment variables.

Practical Exercises

Exercise 1: Setting and Using Environment Variables

  1. Create a new pipeline job in Jenkins.
  2. Use the following pipeline script:
pipeline {
    agent any
    environment {
        ENVIRONMENT = 'development'
        VERSION = '1.0.0'
    }
    stages {
        stage('Print Environment') {
            steps {
                echo "Running in ${env.ENVIRONMENT} environment, version ${env.VERSION}"
            }
        }
    }
}
  1. Run the job and observe the output.

Solution:

The output should display the environment and version as set in the environment directive.

Exercise 2: Using Credentials as Environment Variables

  1. Add a new secret text credential in Jenkins with the ID my-secret.
  2. Create a new pipeline job with the following script:
pipeline {
    agent any
    environment {
        SECRET = credentials('my-secret')
    }
    stages {
        stage('Print Secret') {
            steps {
                echo "The secret is ${env.SECRET}"
            }
        }
    }
}
  1. Run the job and observe the output.

Solution:

The output should display the secret value stored in the Jenkins credentials.

Common Mistakes and Tips

  • Mistake: Forgetting to use the env prefix when accessing environment variables.

    • Tip: Always use env.VARIABLE_NAME to access environment variables in pipeline scripts.
  • Mistake: Hardcoding sensitive information in the pipeline script.

    • Tip: Use Jenkins credentials to securely manage sensitive information.

Conclusion

In this section, we covered the basics of using environment variables in Jenkins pipelines. We learned how to set global and job-specific environment variables, use built-in environment variables, and securely manage sensitive information using Jenkins credentials. Understanding and effectively using environment variables is crucial for creating flexible and maintainable Jenkins pipelines. In the next section, we will explore pipeline best practices to further enhance your Jenkins skills.

© Copyright 2024. All rights reserved