<p>It's possible to use jobDSL from Pipeline. (Im using multibranch pipeline as it allows to configure pipelineTriggers)
You can configure your seed job to be a pipeline like this:</p>
<pre><code>def gitCredentialsId      = 'github-jenkins'
def jobsRepoName          = 'https://github.com/my-jobs-repo.git'
def sharedLibraryRepoName = 'https://github.com/shared-library-repo.git'

properties([    
   pipelineTriggers([githubPush()])
])

pipeline {
    agent any
    stages{
        stage('Seed Job') {
            agent any
            steps {

                checkout([
                    $class: 'GitSCM', 
                    branches: [[name: '*/main']], 
                    doGenerateSubmoduleConfigurations: false,
                    extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'shared-library']], 
                    submoduleCfg: [],
                    userRemoteConfigs: [[credentialsId:  gitCredentialsId, url: sharedLibraryRepoName ]]
                    ])

                git url: jobsRepoName, changelog: false, credentialsId: gitCredentialsId, poll: false, branch: 'main'
                jobDsl targets: 'jobs/**/*_job.groovy', additionalClasspath: 'shared-library/src'
            }
        }
    }
}
</code></pre>
<p>This way we can configure our seed job to checkout shared libraries repo into workspace subdirectory <em>shared-library</em> and specify <strong>additionalClasspath</strong> for jobDSL groovy scripts</p>
<p>So in your groovy scripts you can simply use import without @Library annotation</p>

CC BY-SA 4.0
2021-05-01T15:52:57.750Z
Great solution! Do you know if you get a trust warning for loading shared library code this way?
Alexander Pogrebnyak
CC BY-SA 4.0
2023-02-22T22:20:00.203Z
To add to this - https://github.com/jenkinsci/job-dsl-plugin/wiki/User-Power-Moves#use-job-dsl-in-pipeline-scripts
Vetsin
CC BY-SA 4.0
2024-12-09T11:47:44.780Z
unfortunately as per wiki it requires compiled  code, not source code
michael nesterenko
CC BY-SA 4.0
2021-01-31T02:24:18.730Z
2021-01-31T02:24:18.730Z
Silk0vsky
<p>Setup pipeline name and default version (e.g. master) in &quot;Global Pipeline Libraries&quot; settings. Then use them in your Jenkinsfile as following</p>
<pre><code>#!/usr/bin/env groovy
@Library('YourGlobalPipelineName@YourDefaultVersion') _

pipeline {

}
</code></pre>

CC BY-SA 4.0
2020-06-19T14:55:55.213Z
I'm not making a Jenkins pipeline. This is a job dsl seed script.
David West
CC BY-SA 4.0
2020-06-19T09:43:23.723Z
2020-06-19T09:43:23.723Z
chaks
<p>I've stumbled in this issue as well. I'm configuring all jobs via DSL but find the need to define constants in a shared-library that are shared with jobDSL and pipelines.</p>
<p>According to <a href="https://github.com/sheehan/job-dsl-gradle-example" rel="nofollow noreferrer">https://github.com/sheehan/job-dsl-gradle-example</a> it seems that you could have your jobs specified in a location that is shared with the library, and hence access those files directly.</p>
<p>This is the solution I intend to try out. In the projects I'm working this makes sense. I have a git repo for each jenkins server and each jenkins server can read configurations from a single repo. Anything that is specific to that jenkins server is stored in the repo. Anything that is shared among different jenkins servers, we can simply configure different different jenkins servers to use other jenkins's git repo libraries.</p>

CC BY-SA 4.0
2022-08-18T10:14:26.123Z
2022-08-18T10:14:26.123Z
Tiago L. Alves
<p>There are two option</p>
<ol>
<li><p>Using jenkins global shared lib setting in gui see documation  <a href="https://www.jenkins.io/doc/book/pipeline/shared-libraries/" rel="nofollow noreferrer">https://www.jenkins.io/doc/book/pipeline/shared-libraries/</a></p>
</li>
<li><p>Importing shared library inside jenkins file similart like this follow this blog
<a href="https://tomd.xyz/jenkins-shared-library/" rel="nofollow noreferrer">https://tomd.xyz/jenkins-shared-library/</a></p>
<pre><code>library identifier: 'mylibraryname@master',
     //'master' refers to a valid git-ref
     //'mylibraryname' can be any name you like
     retriever: modernSCM([
       $class: 'GitSCMSource',
       credentialsId: 'your-credentials-id',
       remote: 'https://git.yourcompany.com/yourrepo/private-library.git'
 ])

 pipeline {
     agent any
     stages {
         stage('Demo') {
             steps {
                 echo 'Hello world'
                 yourCustomStep 'your_arg'
             }
         }
     }
</code></pre>
</li>
</ol>
<p>P.S.
When using &quot;@Library _&quot; the this means importing all libraries</p>

CC BY-SA 4.0
2021-01-31T16:05:29.877Z
2021-01-31T16:10:43.373Z
2021-01-31T16:10:43.373Z
Galpha
<p>I have a seed job using the plugin Jenkins Job DSL. I also have a shared library.</p>

<p>I have tried using the <code>@Library</code> annotation to load the script and the <code>library</code> method. It cannot find the annotation and using <code>library</code> yields the following error:</p>

<p><code>No signature of method: simple_pipeline.library() is applicable for argument types: (java.lang.String) values: [platform-engineering-library@master]</code></p>

<p>How do I load a Jenkins Shared Library in a Jenkins Job DSL seed?</p>

CC BY-SA 4.0
2022-01-06T02:58:20.127Z
Which solution work for you ?
Nilesh
<jenkins><job-dsl>
How do I load a Jenkins Shared Library in a Jenkins Job DSL seed?
David West
# How do I load a Jenkins Shared Library in a Jenkins Job DSL seed?

(Asked by: username_0 on 2020-06-16)

I have a seed job using the plugin Jenkins Job DSL. I also have a shared library.

I have tried using the `@Library` annotation to load the script and the `library` method. It cannot find the annotation and using `library` yields the following error:

`No signature of method: simple_pipeline.library() is applicable for argument types: (java.lang.String) values: [platform-engineering-library@master]`

How do I load a Jenkins Shared Library in a Jenkins Job DSL seed?

**Tags:** jenkins, job-dsl

> **Comments:**
> **username_1**: Which solution work for you ?  

---

## 4 answers

### Answer by username_2 on 2021-01-31. Score: 4

It's possible to use jobDSL from Pipeline. (Im using multibranch pipeline as it allows to configure pipelineTriggers)
You can configure your seed job to be a pipeline like this:
def gitCredentialsId      = 'github-jenkins'
def jobsRepoName          = 'https://github.com/my-jobs-repo.git'
def sharedLibraryRepoName = 'https://github.com/shared-library-repo.git'

properties([    
   pipelineTriggers([githubPush()])
])

pipeline {
    agent any
    stages{
        stage('Seed Job') {
            agent any
            steps {

                checkout([
                    $class: 'GitSCM', 
                    branches: [[name: '*/main']], 
                    doGenerateSubmoduleConfigurations: false,
                    extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'shared-library']], 
                    submoduleCfg: [],
                    userRemoteConfigs: [[credentialsId:  gitCredentialsId, url: sharedLibraryRepoName ]]
                    ])

                git url: jobsRepoName, changelog: false, credentialsId: gitCredentialsId, poll: false, branch: 'main'
                jobDsl targets: 'jobs/**/*_job.groovy', additionalClasspath: 'shared-library/src'
            }
        }
    }
}

This way we can configure our seed job to checkout shared libraries repo into workspace subdirectory shared-library and specify additionalClasspath for jobDSL groovy scripts
So in your groovy scripts you can simply use import without @Library annotation

> **Comments:**
> **username_3**: Great solution! Do you know if you get a trust warning for loading shared library code this way?  
> **username_4**: To add to this - https://github.com/jenkinsci/job-dsl-plugin/wiki/User-Power-Moves#use-job-dsl-in-pipeline-scripts  
> **username_5**: unfortunately as per wiki it requires compiled  code, not source code  

---

### Answer by username_6 on 2020-06-19. Score: 1

Setup pipeline name and default version (e.g. master) in "Global Pipeline Libraries" settings. Then use them in your Jenkinsfile as following
#!/usr/bin/env groovy
@Library('YourGlobalPipelineName@YourDefaultVersion') _

pipeline {

}

> **Comments:**
> **username_0**: I'm not making a Jenkins pipeline. This is a job dsl seed script.  

---

### Answer by username_7 on 2022-08-18. Score: 1

I've stumbled in this issue as well. I'm configuring all jobs via DSL but find the need to define constants in a shared-library that are shared with jobDSL and pipelines.

According to <https://github.com/sheehan/job-dsl-gradle-example> it seems that you could have your jobs specified in a location that is shared with the library, and hence access those files directly.

This is the solution I intend to try out. In the projects I'm working this makes sense. I have a git repo for each jenkins server and each jenkins server can read configurations from a single repo. Anything that is specific to that jenkins server is stored in the repo. Anything that is shared among different jenkins servers, we can simply configure different different jenkins servers to use other jenkins's git repo libraries.


---

### Answer by username_8 on 2021-01-31. Score: 0

There are two option

Using jenkins global shared lib setting in gui see documation  https://www.jenkins.io/doc/book/pipeline/shared-libraries/

Importing shared library inside jenkins file similart like this follow this blog
https://tomd.xyz/jenkins-shared-library/
library identifier: 'mylibraryname@master',
     //'master' refers to a valid git-ref
     //'mylibraryname' can be any name you like
     retriever: modernSCM([
       $class: 'GitSCMSource',
       credentialsId: 'your-credentials-id',
       remote: 'https://git.yourcompany.com/yourrepo/private-library.git'
 ])

 pipeline {
     agent any
     stages {
         stage('Demo') {
             steps {
                 echo 'Hello world'
                 yourCustomStep 'your_arg'
             }
         }
     }



P.S.
When using "@Library _" the this means importing all libraries


---

