Environment variable in pipeline

Jenkins Pipeline exposes environment variables via the global variable env, which is available from anywhere within a Jenkinsfile

Setting environment variables
setting an environment variable within a Jenkins Pipeline is accomplished differently depending on whether Declarative or Scripted Pipeline is used.
Declarative Pipeline supports an environment directive, whereas users of Scripted Pipeline must use the withEnv step

// Declarative //
pipeline {
    agent any
    environment { 
        CC = 'clang'
    }
    stages {
        stage('Example') {
            environment { 
                DEBUG_FLAGS = '-g'
            }
            steps {
                sh 'printenv'
            }
        }
    }
}
// Script //
node {
    /* .. snip .. */
    withEnv(["PATH+MAVEN=${tool 'M3'}/bin"]) {
        sh 'mvn -B verify'
    }
}

An environment directive used in the top-level pipeline block will apply to all steps within the Pipeline and the environment directive defined within a stage will only apply the given environment variables to steps within the stage.

Recent Comments

No comments

Leave a Comment