0
votes

I recently upgraded to Jenkins 2.0.

I’m trying to add a build step to a jenkins job of "Inject environment variables" along the lines of this SO post, but it’s not showing up as an option.

Is this not feature in Jenkins 2.0 (or has it always been a separate plugin)? Do I have to install another plugin, such as Envinject?

1
Your jenkins job is a pipeline or a standard job?Bruno Lavit
Standard. I ended up installing that plugin and it now seems to work fine. Just seemed like I never had to do that with earlier versions, but maybe I'm wrong…MrColes

1 Answers

0
votes

If you are using Jenkins 2.0 you can load the property file (which consists of all required Environment variables along with their corresponding values) and read all the environment variables listed there automatically and inject it into the Jenkins provided env entity.

Here is a method which performs the above stated action.

def loadProperties(path) {
    properties = new Properties()
    File propertiesFile = new File(path)
    properties.load(propertiesFile.newDataInputStream())
    Set<Object> keys = properties.keySet();
    for(Object k:keys){
    String key = (String)k;
    String value =(String) properties.getProperty(key)
    env."${key}" = "${value}"
    }
}

To call this method we need to pass the path of property file as a string variable For example, in our Jenkins file using groovy script we can call like

path = "${workspace}/pic_env_vars.properties"
loadProperties(path)

Please ask me if you have any doubt