Skip to content

Instantly share code, notes, and snippets.

@nocode99
Last active November 4, 2020 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nocode99/d4c654514ff2b683af90d7dd5e0156e0 to your computer and use it in GitHub Desktop.
Save nocode99/d4c654514ff2b683af90d7dd5e0156e0 to your computer and use it in GitHub Desktop.
Jenkins Job-DSL Folder for Shared Pipeline config
folder('My_folder') {
properties {
folderLibraries {
libraries {
libraryConfiguration {
name('shared-pipeline')
defaultVersion('master')
implicit(false)
allowVersionOverride(true)
includeInChangesets(true)
retriever {
scmSourceRetriever {
scm {
git {
remote('ssh://git@github.com/org/shared-pipeline.git')
credentialsId('some-credentials')
}
}
}
}
}
}
}
}
configure { node ->
node / properties / 'org.jenkinsci.plugins.workflow.libs.FolderLibraries' / libraries / 'org.jenkinsci.plugins.workflow.libs.LibraryConfiguration' / retriever / scm / traits {
'jenkins.plugins.git.traits.BranchDiscoveryTrait'()
}
}
}
<?xml version='1.1' encoding='UTF-8'?>
<com.cloudbees.hudson.plugins.folder.Folder plugin="cloudbees-folder@6.7">
<actions/>
<description></description>
<properties>
<org.jenkinsci.plugins.workflow.libs.FolderLibraries plugin="workflow-cps-global-lib@2.13">
<libraries>
<org.jenkinsci.plugins.workflow.libs.LibraryConfiguration>
<name>shared-pipeline</name>
<retriever class="org.jenkinsci.plugins.workflow.libs.SCMSourceRetriever">
<scm class="jenkins.plugins.git.GitSCMSource" plugin="git@3.9.3">
<id>unique-id</id>
<remote>ssh://git@github.com/org/shared-pipeline.git</remote>
<credentialsId>some-credentials</credentialsId>
<traits>
<jenkins.plugins.git.traits.BranchDiscoveryTrait/> // <--- HOW DO I CONFIGURE THIS
</traits>
</scm>
</retriever>
@arolfes
Copy link

arolfes commented Nov 4, 2020

Hi @nocode99,

could you solve the problem?

I had to change your code a bit. I'M using the latest git plugin 4.4.1

folder('My_folder') {
  properties {
    folderLibraries {
      libraries {
        libraryConfiguration {
          name('shared-pipeline')
          defaultVersion('master')
          implicit(false)
          allowVersionOverride(true)
          includeInChangesets(true)
          retriever {
            scmSourceRetriever {
              scm {
                git {
                  remote('ssh://git@github.com/org/shared-pipeline.git')
                  credentialsId('some-credentials')
                }
              }
            }
          }
        }
      }
    }
  }
  configure { node ->
    def traits = node / 'properties' / 'org.jenkinsci.plugins.workflow.libs.FolderLibraries' / 'libraries' / 'org.jenkinsci.plugins.workflow.libs.LibraryConfiguration' / 'retriever' / 'scm' / 'traits'
    traits << { 'jenkins.plugins.git.traits.BranchDiscoveryTrait'() }
  }
}

I hope it helps or maybe it helps someone else.

@nocode99
Copy link
Author

nocode99 commented Nov 4, 2020

I ended up doing this:

def scmTrait = { folder ->
  folder / 'properties' / 'org.jenkinsci.plugins.workflow.libs.FolderLibraries' / 'libraries' / 'org.jenkinsci.plugins.workflow.libs.LibraryConfiguration' / retriever / scm / traits {
    'jenkins.plugins.git.traits.BranchDiscoveryTrait'()
  }
}

// and then called the function like below
...
  }
  configure scmTrait

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment