Setting environment variable in Cloudbees Jenkins folder
Asked Answered
I

3

3

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?

Ingham answered 2/12, 2013 at 17:23 Comment(0)
I
4

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

Ingham answered 2/12, 2013 at 17:48 Comment(1)
Hi @Robin Green, is there a place I can get the old plugin which still had the functionality? I need it to maintain a legacy deployment of Jenkins. Either the hpi or the source code. Thanks.Lording
L
1

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

Lording answered 19/3, 2018 at 14:15 Comment(3)
can you make it available for free style jobs?Pinna
@Pinna it works well for freestyle jobs. It's been a couple of years since I posted this, and the new official repo for it is part of the JenkinsCI group. You can find the Freestyle Jobs documentation hereLording
Thank you! it works perfectly! What I missed is that the "folder properties" in the build job not checked, therefor the folder properties defined at the parent folder are not propagated to the jobs under the folder. This is a great feature and will save me lot of time.Pinna
B
0

@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()

Biotope answered 13/9, 2018 at 1:5 Comment(1)
Good workaround. You'll be happy to know I've recently merged a PR into the plugin that makes properties available now for the SCM step in Freestyle jobs.Lording

© 2022 - 2024 — McMap. All rights reserved.