Setting environment variable in Cloudbees Jenkins folder

3.3k views Asked by At

After I reinstalled Jenkins and Cloudbees Free Enterprise plugins, an environment variable set in my Jenkins folder stopped working and disappeared from the config UI. However, it is still there in the config.xml on the filesystem, as:

<properties>
 <com.cloudbees.hudson.plugins.folder.properties.EnvVarsFolderProperty>

How should I now create environment variables that apply to an entire folder?

3

There are 3 answers

1
Robin Green On BEST ANSWER

The functionality has been moved to the Folders Plus plugin, which requires a Jenkins Enterprise paid license.

1
Tom Sackett On

@Mig82 has provided a great solution. It has one limitation, though, which is that the folder environment variables it provides are not available in the SCM section of a freestyle build, and possibly in some other sections.

I've found I can get around this using a snippet of groovy:

  1. In your freestyle job, enable the "Prepare an environment for the run" section.
  2. In the "Prepare an environment for the run" section, add the following code to the "Evaluated Groovy Script" field:

    import com.mig82.folders.wrappers.ParentFolderBuildWrapper
    import jenkins.tasks.SimpleBuildWrapper

    // Create a temporary Context object into which we can load the folder properties.
    myContext = new SimpleBuildWrapper.Context()

    // Create an object of the class "ParentFolderBuildWrapper".
    // This is the class at the heart of the Folder Properties plugin,
    // and it implements the code that gets the properties from the folder.
    parentFolderBuildWrapper = new ParentFolderBuildWrapper()

    // Load the folder properties into our Context object.
    parentFolderBuildWrapper.loadFolderProperties(currentJob, myContext)

    // Return the map containing the properties.
    return myContext.getEnv()

3
Mig82 On

After much thought, and since I've had to deal with this need for a while, I've put some work into defining my own plugin to cover this need. It allows you to define a list of String properties for a folder, which can then be inherited by the jobs inside it, thus removing the need to specify the same properties over and over again for all the jobs inside a folder.

https://github.com/mig82/folder-properties-plugin

I hope this is useful to others. I plan to submit this to Jenkins CI Org as soon as I've had time to document it better and write some test scripts.

Update:

The new official repo for this plugin in the JenkinsCI org is this:

https://github.com/jenkinsci/folder-properties-plugin