Jenkins Pipeline Environment Variables: Difference between revisions
Line 56: | Line 56: | ||
=Environment Variables= | =Environment Variables= | ||
{{External|https://www.jenkins.io/doc/pipeline/tour/environment/}} | |||
Environment variables can be set globally or per stage. Setting environment variable per stage means they will only apply to the stage in which they were defined. | |||
The environment variables can be accessed directly (e.g. ${NODE_NAME}) or via env: ${env.NODE_NAME}". | |||
==BUILD_ID== | ==BUILD_ID== |
Revision as of 20:12, 10 March 2021
Internal
Overview
A Jenkins pipeline exposes environment variables via the global variable env
, which is available from anywhere in the Jenkinsfile. Please note that env
is not available from Groovy layer code invoked from Jenkinsfile:
String imageVersion = buildInfo.rpmVersion != null ? buildInfo.rpmVersion : env.PREBUILT_IMAGE_TAG
[...]
hudson.remoting.ProxyException: groovy.lang.MissingPropertyException: No such property: env for class: playground.jenkins.steps.TestSomethingStep
The full list of environment variables accessible from within a Jenkins pipeline is documented at http://<jenkins-instance-url>/pipeline-syntax/globals#env.
Programming Model
Accessing Environment Variables
echo "Running ${env.BUILD_ID} on ${env.JENKINS_URL}"
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
Declarative pipeline supports an environment directive. An environment
directive used in the top-level pipeline block will apply to all steps within the pipeline. An environment directive defined within a stage will only apply the given environment variables to steps within the stage.
pipeline {
agent any
environment {
CC = 'clang'
}
stages {
stage('Example') {
environment {
DEBUG_FLAGS = '-g'
}
steps {
sh 'printenv'
}
}
}
}
Scripted Pipeline
Scripted pipeline must use the withEnv step.
Setting Environment Variables Dynamically
Environment Variables
Environment variables can be set globally or per stage. Setting environment variable per stage means they will only apply to the stage in which they were defined.
The environment variables can be accessed directly (e.g. ${NODE_NAME}) or via env: ${env.NODE_NAME}".
BUILD_ID
The current build ID, identical to BUILD_NUMBER.
BUILD_NUMBER
The current build number, such as "153". Same as BUILD_ID.
BUILD_TAG
Contains job name, branch name, build number:
jenkins-${JOB_NAME}-${BUILD_NUMBER}
BUILD_URL
The URL where the results of this build can be found. Example: http://<jenkins-instance-url>/jenkins/job/MyJobName/17/
EXECUTOR_NUMBER
The unique number that identifies the current executor among executors of the same machine performing this build. This is the number shown in the "build executor status", except that the number starts from 0, not 1.
JAVA_HOME
If your job is configured to use a specific JDK, this variable is set to the JAVA_HOME of the specified JDK. When this variable is set, PATH is also updated to include the bin subdirectory of JAVA_HOME
JENKINS_URL
Full URL of Jenkins, such as https://example.com:port/jenkins/. It is only available if Jenkins URL set in "System Configuration".
JOB_NAME
Name of the project of this build.
JOB_BASE_NAME
NODE_NAME
The name of the node the current build is running on. Set to 'master' for master node.
WORKSPACE
The absolute path of the workspace