Not sure if this helps, but: I needed to share code between Pipeline and 
Job DSL and I solved it by using Job DSL from Pipeline. I found that easy 
because using shared library in Pipeline is well documented.

Something like:
...
    stage("Create jobs") {
      jobDsl targets: 'work/jobs/*.groovy',
        removedJobAction: 'DELETE',
        removedViewAction: 'DELETE',
        lookupStrategy: 'SEED_JOB',
        additionalClasspath: [].join('\n'),
        additionalParameters: [repos: repos]
    }
...

Example here:
https://github.com/tomasbjerre/jenkins-configuration-as-code-sandbox/blob/master/vars/createJobs.groovy#L44

Den fredag 17 augusti 2018 kl. 18:04:56 UTC+2 skrev tre...@gmail.com:
>
> Hey,
>
> I am looking for advice how to achieve usage of Jenkins shared library in 
> Job DSL – 
> I use this snipped to seed a job whose functionality is in Jenkins file:
>
>
> job('my-small-test-job') {
> parameters {
> stringParam('branch', '${branch}')
> } 
> removedJobAction: 'IGNORE'
> def releaseScript = readFileFromWorkspace('Jenkinsfile')
> steps {
>       dsl {
>       text(releaseScript)
>       }
> }
> }
>
> The newly created [my-small-test-job] has this content (taken out of the 
> Jenkinsfile): 
>
> @Library('templates')_
>
> stage('Demo') {
>     echo 'Hello world'
>     jobBuilder {
>       nodeLabels = 'linux && test'
>       antExtraArgs = 'test 
> -Dtest.suite=com.softwareag.suites.custom.ToolsTestSuite'
> }
> }
>
> I have a shared library under [templates] that is a generic job builder 
> (the syntax is typical groovy like jenkins pipeline, similar to what is 
> explained here: https://jenkins.io/doc/book/pipeline/shared-libraries/)
>
>
> When I try to run the newly created [my-small-test-job] I get this: 
>
> Processing provided DSL script
> ERROR: startup failed:
> script: 1: unable to resolve class Library ,  unable to find class for 
> annotation
>  @ line 1, column 1.
>    @Library('templates')_
>    ^
>
> 1 error
>
>
> The shared library is obviusly not added to classpath. How do I fix this?
>
 

-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jenkinsci-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/0f40aa87-af1d-4f5c-b263-76d3159cb0db%40googlegroups.com.

Reply via email to