Jenkins environment variables
source link: https://medium.com/@mukeshsingal/access-jenkins-global-environment-variables-using-groovy-or-java-b5c1e6b53685
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
The leading open source automation server, Jenkins provides hundreds of plugins to support building, deploying and automating any project.
Jenkins exposes characteristics of components using Environment variables. This blog will tell you everything about Environment variables.
- Creating Global environment variables
- Access Global environment variables.
- Creating local environment variables during build.
1. Creating global environment variables:
1.1 Using Jenkins Console
we can easily create global environment variables.
Manage Jenkins => Configure
Under section Global Properties, check Environment variables checkbox. Now Jenkins allow us to add key and value pairs which will be accessible with in each node and each build.
1.2 Using Groovy/Java
To create global environment variable, we need to use below code.
import hudson.EnvVars;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import hudson.slaves.NodeProperty;
import hudson.slaves.NodePropertyDescriptor;
import hudson.util.DescribableList;
import jenkins.model.Jenkins;public createGlobalEnvironmentVariables(String key, String value){
Jenkins instance = Jenkins.getInstance();
DescribableList<NodeProperty<?>, NodePropertyDescriptor> globalNodeProperties = instance.getGlobalNodeProperties();
List<EnvironmentVariablesNodeProperty> envVarsNodePropertyList = globalNodeProperties.getAll(EnvironmentVariablesNodeProperty.class);
EnvironmentVariablesNodeProperty newEnvVarsNodeProperty = null;
EnvVars envVars = null;
if ( envVarsNodePropertyList == null || envVarsNodePropertyList.size() == 0 ) {
newEnvVarsNodeProperty = new hudson.slaves.EnvironmentVariablesNodeProperty();
globalNodeProperties.add(newEnvVarsNodeProperty);
envVars = newEnvVarsNodeProperty.getEnvVars();
} else {
envVars = envVarsNodePropertyList.get(0).getEnvVars();
}
envVars.put(key, value)
instance.save()
}createGlobalEnvironmentVariables('Var1','DummyValue')
2. Access Global Environment variables
2.1 In jenkins Pipeline
To use above defined variable in pipeline we can use env.Key give us access to out jenkins global variables.
pipeline {
agent any
stages {
stage('Build') {
steps {
echo env.Var1
}
}
}
}
In jenkins pipeline there are lot of other useful environment variables which can be accessed during build execution.
Most useful environment variables. Highly recommended to check out.
- env: Environment variables are accessible from Groovy code as
env.VARNAME
or simply asVARNAME
. You can write to such properties as well (only using theenv.
prefix): - currentBuild: The
currentBuild
variable may be used to refer to the currently running build. - params: Exposes all parameters defined in the build as a read-only map with variously typed values.
- docker : The
docker
variable offers convenient access to Docker-related functions from a Pipeline script.
In pipeline job -> pipeline Syntax -> Global Variable Reference
2.2 In Shell/Batch Script
In Shell script we can use environment variables using $Key or ${Key}. Similarly in batch we can use %Key% to access Environment Variables.
2.3 In FreeStyle projects
In freestyle project we can use EnvEnject Plugin.
3. Creating local environment variables during build.
3.1 Using Declarative pipeline
Jenkinsfile (Declarative Pipeline)
pipeline {
agent any
environment {
DISABLE_AUTH = 'true'
}
stages {
stage('Build') {
steps {
echo env.DISABLE_AUTH
}
}
}
}
3.2 Using Scripted pipeline.
node{
stage('Build') {
withEnv(["DISABLE_AUTH=true"]) {
echo env.DISABLE_AUTH
}
}
}
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK