Setting environment variable in Cloudbees Jenkins

2019-07-19 07:03发布

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条回答
何必那么认真
2楼-- · 2019-07-19 07:21

@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楼-- · 2019-07-19 07:26

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.

查看更多
SAY GOODBYE
4楼-- · 2019-07-19 07:37

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

查看更多
登录 后发表回答