Skip to end of metadata
Go to start of metadata

A Jenkins plugin which allows users with config permission to define properties for a folder which can then be used by any jobs contained within it or any of its sub-folders. 

You can find this plugin's source code on Github.

Plugin Information

View Folder Properties on the plugin site for more information.

 

About this Plugin

The aim here is to remove the need to specify the same properties over and over again for all the jobs inside a folder.

In structures where two or more folders are nested, any property defined for a folder will be overridden by any other property of the same name defined by one of its sub-folders.

Configuring Folder Properties

Just create a folder, go to its configuration page and add as many properties as you need under the Folder Properties section.

Freestyle Jobs

Freestyle jobs must opt into the Folder Properties build wrapper from the Build Environment section of their configuration page in order to be able to access these properties as they would any other environment variable.

Pipeline Jobs

Pipeline jobs can use step withFolderProperties to access them:

Using folder properties in a pipeline job
withFolderProperties{
    echo("Foo: ${env.FOO}")
}

Jenkins deployments using some of the older versions of the Structs Plugin will need to do this using the wrap meta-step:

Using folder properties in older pipeline jobs
wrap([$class: 'ParentFolderBuildWrapper']) {
    echo("Foo: ${env.FOO}")
}

Job DSL

In Job DSL scripts you can define folder properties like so:

Job DSL example
folder('my folder') {
    properties {
        folderProperties {
            properties {
                stringProperty {
                    key('FOO')
                    value('foo1')
                }
            }
        }
    }
}

9 Comments

  1. Hi,

    I have a folder, with some pipelines. I use the same pipelines for different branches, and I have some different definitions on the SCM.

    I want to have a folder property, with the branch name, so I can use it to define the "branches to build" on the pipeline definition.
    I see that on "free style" jobs, I can mark a checkbox to use folder properties, but on pipelines, I have to use "withFolderProperties".

    Is it possible to set an option to use folder properties to define the pipeline?

    In this picture, I want to define "RELENG_BRANCH" on the folder level.

    Thanks.

    1. I don't believe that will work. The way this plugin works for pipeline jobs is by extending SimpleBuildWrapper, which means the properties of the job's ancestor folders get added to env at runtime. Meanwhile, as far as I understand, the evaluation of the environment variables used for the configuration screen happens before the job starts running.

      As an alternative you could use some pipeline code to abort the build if the branch to be built doesn't match the RELENG_BRANCH variable you've defined for the folder –e.g.:

      Jenkinsfile
      Boolean abort = false
      
      stage('Validate branch'){
          node{
              //Let's determine which branch is being built.
              def cmd = 'git name-rev --name-only HEAD > branch'
              isUnix()?sh(cmd):bat(cmd)
              def branch = readFile('branch').trim()
              echo("Branch is '${branch}'")
      
              //Let's see if the branch to be built matches the config for this folder.
              withFolderProperties{
                  abort = branch != env.RELENG_BRANCH
              }        
          }
      }
      
      //This must be top-level to abort the job.
      if(abort){return;} 
      
      

       

      I hope this helps.

  2. Thanks Miguelángel Fernández and sorry for the late response.
    Indeed my original idea didn't work.

    I used your idea, but took it to another direction: I set the properties according branch (not abort).

  3. Is it possible to use folder defined properties on SCM section?
    The "folder properties" check box is in the "build environment" sction, which is after the scm definition.

    I have a "trial" build that echo the value of the property, and works fine, When I try to use the same property on SCM section, I get an error, in which the name pf the property appears, and not the value:

    git rev-parse refs/heads/$folder_version^{commit} # timeout=10
       ERROR: Couldn't find any revision to build. 

    I posted more details and screen captures on https://serverfault.com/questions/935470/jenkins-folder-propery-is-it-possible-to-use-a-folder-defined-property-on-scm-s
  4. Is there a REST command I can use to get and update the properties of a folder?

  5. Hi,

    I am trying to use this plugin in creating a SEED JOB which will internally trigger chain of jobs inside a PROJECT folder to create upstream/downstream dependencies.

    folder('PROJECT') {
        properties {
           folderProperties {
              properties {
                 stringProperty {
                     key('NAME')
                    value('VALUE')
                 }
            }
         }
      }
    }

    I know that to inject folder properties in job, "FOLDER PROPERTIES" has to be checked in the job properties. How do inject the folder properties plugin in my job using DSL script?

    job("PROJECT/Example") {
       logRotator {
            numToKeep(10)
        } 

    // I tried using the below snippet but job executes without any error and when I open the job created it does not contain any Build Step or Post build step mentioned below.
     environmentVariables {
         contributors {
            populateToolInstallations() 
         }
     }

     steps {
       publishOverSsh {
              server('Test') {
                    transferSet {
                        execCommand('''COMMANDS TO EXECUTE ON SOME MACHINE SERVER")
                    }
              }
        }
      }

     publishers {
            textFinder(/^FAIL/,'' ,true, false, true)
     }
     }

    I cannot manually select the check box since I am creating about 100 jobs in a folder with upstream/downstream dependencies.

    Need a solution to resolve this issue through script only.

    Appreciate the quick response.

  6. When reading in the SCM plugin i got this:

    • ${ENV_VARIABLE}
      It is also possible to use environment variables. In this case the variables are evaluated and the result is used as described above.
      E.g. ${TREEISH}, refs/tags/${TAGNAME},...

    so you can use env variables in the "Branches to build", but this properties plugin doesn't really set those kind of variables before everything?

    because doing ${xxxx} doesn't really work (just as i guess explained above and here: https://serverfault.com/questions/935470/jenkins-folder-propery-is-it-possible-to-use-a-folder-defined-property-on-scm-s )

     But this would be the number one reason to use this plugin... I guess i have the same as Hagit and a multiply folders with 1 folder having many projects that all have a common branch (per folder)

    So it would be great if this plugin could expose those properties to the jobs inside the folder the same as ENV_VARIABLES ...


    Also the "use custom workspace" option is not working, which is also used quite a bit in our jobs to point them to the "right branch"

    so for now out of the 3 (Use branch, Use custom workspace and Execute shell) only the last is working,