Usage of Jenkins shared library in Job DSL

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Usage of Jenkins shared library in Job DSL

treneva
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/6e3b23aa-873b-40c6-9859-c5cd7356611f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Usage of Jenkins shared library in Job DSL

Jan Monterrubio
Manage Jenkins -> configure system -> global libraries 

You can add your shared library there. 

On Fri, Aug 17, 2018 at 11:05 <[hidden email]> wrote:
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/6e3b23aa-873b-40c6-9859-c5cd7356611f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CADgiF9LMjNNruE_pux-K5Gqy_UabB0KHdZwiHWVG3a7AhzSP-Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Usage of Jenkins shared library in Job DSL

treneva
Yep,
My libraries are already configured there, but the DSL script is obviously not working. Perhaps the syntax needs to be different...

On Sunday, August 26, 2018 at 6:04:36 PM UTC+3, Jan Monterrubio wrote:
Manage Jenkins -> configure system -> global libraries 

You can add your shared library there. 

On Fri, Aug 17, 2018 at 11:05 <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="TcQ7iAu5AQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">tre...@...> wrote:
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: <a href="https://jenkins.io/doc/book/pipeline/shared-libraries/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fjenkins.io%2Fdoc%2Fbook%2Fpipeline%2Fshared-libraries%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzi5Xo827_lODtglxm9wkc-kRKTg&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fjenkins.io%2Fdoc%2Fbook%2Fpipeline%2Fshared-libraries%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzi5Xo827_lODtglxm9wkc-kRKTg&#39;;return true;">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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="TcQ7iAu5AQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/jenkinsci-users/6e3b23aa-873b-40c6-9859-c5cd7356611f%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/6e3b23aa-873b-40c6-9859-c5cd7356611f%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-users/6e3b23aa-873b-40c6-9859-c5cd7356611f%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/jenkinsci-users/6e3b23aa-873b-40c6-9859-c5cd7356611f%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/c73bac9e-3df4-4f83-a3c1-fb994c8cc13d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Usage of Jenkins shared library in Job DSL

Philip Steiner
Hi tre..., I know this thread is getting old, but did you ever get an answer or figure out how to use a Jenkins Shared Library in the Job DSL script, or some workaround?

Thanks
Philip

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/61fd0fff-f7c8-4928-836f-7e7aa07a4877%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Usage of Jenkins shared library in Job DSL

treneva
Well, not exactly. I found a workaround by reworking the entire logic.

My landscape is: one master declarative pipeline kicks off multiple child pipeline jobs. 
Both the master and the child pipelines are instances created out of shared libraries (jobBuilder, pipelineBuilder) and I want to be able to run the child items as standalone pipelines (in isolation) as well as running them from master pipeline context (different params).

The child job's Jenkins file looks like this (the syntax is not declarative but scripted - the shared library is loaded on the first line):

library identifier: 'templates@branches/rel/10.4.0.x/build/change-management/jenkinsfiles/templates', retriever: modernSCM([$class: 'SubversionSCMSource', remoteBase: 'SVN', credentialsId: 'USER'])

stage('Run Test Suite') {
    jobBuilder {
nodeLabels = 'linux'
antExtraArgs = 'test -Dtest.suite=com.pcbsys.suites.nJMS.PromotionTests'
jobTimeoutHours = 3
}
}


jobsdsl syntax was used only to create the actual Jenkins child pipeline:

script {
    for (String jobName : allJobs) {
        jobDsl scriptText: """
            pipelineJob('${nirvanaMajor}.${nirvanaMinor}_${devJobPrefix}${jobName}') {
                 parameters {
                    stringParam('branch','${svnBranch}','svn repository url')
                    stringParam('buildmajor',"${nirvanaMajor}",'release major identifier')
                    stringParam('buildminor',"${nirvanaMinor}",'release minor identifier')
                    stringParam('fix', '${env.fix}','fix level')
                    stringParam('buildnumber', '${env.buildNumber}','artifacts build number')
                    stringParam('revision', '${env.buildNumber}','checkout revision')
                    stringParam('parentjob', '${pipelineName}','optional parent job')
                }
                removedJobAction: 'IGNORE'
                definition {
                    cpsScm {
                        scm {
                            svn {
                                location ('${svnBranch}/${jobName}') {
                                    credentials('user')
                                }
                            }
                        }
                        scriptPath('Jenkinsfile') 
                    }
                }
            }
            """.stripIndent()
    }
}

On Monday, December 17, 2018 at 11:51:39 PM UTC+2, Philip Steiner wrote:
Hi tre..., I know this thread is getting old, but did you ever get an answer or figure out how to use a Jenkins Shared Library in the Job DSL script, or some workaround?

Thanks
Philip

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/75affe01-2fb0-4766-9867-d1873d5048e0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Usage of Jenkins shared library in Job DSL

Philip Steiner
Thanks for the detailed followup! Lots to digest. It looks like you were having trouble accessing the Shared Library from generated pipeline scripts, which is a little different from my problem: I want to reference YAML data files stored in the Jenkins Shared Library from inside a Job DSL script, but now it appears that's not possible (at least I don't see anything like @Library for the Job DSL API), so I think I also need to rework the overall logic for my build environment. 

On Tuesday, December 18, 2018 at 4:27:57 AM UTC-8, [hidden email] wrote:
Well, not exactly. I found a workaround by reworking the entire logic.

My landscape is: one master declarative pipeline kicks off multiple child pipeline jobs. 
Both the master and the child pipelines are instances created out of shared libraries (jobBuilder, pipelineBuilder) and I want to be able to run the child items as standalone pipelines (in isolation) as well as running them from master pipeline context (different params).

The child job's Jenkins file looks like this (the syntax is not declarative but scripted - the shared library is loaded on the first line):

library identifier: 'templates@branches/rel/10.4.0.x/build/change-management/jenkinsfiles/templates', retriever: modernSCM([$class: 'SubversionSCMSource', remoteBase: 'SVN', credentialsId: 'USER'])

stage('Run Test Suite') {
    jobBuilder {
nodeLabels = 'linux'
antExtraArgs = 'test -Dtest.suite=com.pcbsys.suites.nJMS.PromotionTests'
jobTimeoutHours = 3
}
}


jobsdsl syntax was used only to create the actual Jenkins child pipeline:

script {
    for (String jobName : allJobs) {
        jobDsl scriptText: """
            pipelineJob('${nirvanaMajor}.${nirvanaMinor}_${devJobPrefix}${jobName}') {
                 parameters {
                    stringParam('branch','${svnBranch}','svn repository url')
                    stringParam('buildmajor',"${nirvanaMajor}",'release major identifier')
                    stringParam('buildminor',"${nirvanaMinor}",'release minor identifier')
                    stringParam('fix', '${env.fix}','fix level')
                    stringParam('buildnumber', '${env.buildNumber}','artifacts build number')
                    stringParam('revision', '${env.buildNumber}','checkout revision')
                    stringParam('parentjob', '${pipelineName}','optional parent job')
                }
                removedJobAction: 'IGNORE'
                definition {
                    cpsScm {
                        scm {
                            svn {
                                location ('${svnBranch}/${jobName}') {
                                    credentials('user')
                                }
                            }
                        }
                        scriptPath('Jenkinsfile') 
                    }
                }
            }
            """.stripIndent()
    }
}

On Monday, December 17, 2018 at 11:51:39 PM UTC+2, Philip Steiner wrote:
Hi tre..., I know this thread is getting old, but did you ever get an answer or figure out how to use a Jenkins Shared Library in the Job DSL script, or some workaround?

Thanks
Philip

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/8b6b6b48-7a95-43a3-9cfc-112c51ead9f0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Usage of Jenkins shared library in Job DSL

Nick Muzika
hi Philip,
It's possible


@Grab
('org.yaml:snakeyaml:1.17')

import org.yaml.snakeyaml.Yaml ......
def params = readFileFromWorkspace(param.getPath())
yamlMap = new Yaml().load(params)
yamlMap.each { key, value ->
....
}

Works for me just use yaml module & groovy. In my case shared libraries are placed in the same repository where the seed job dsl is sitting.


вторник, 18 декабря 2018 г., 14:28:45 UTC-5 пользователь Philip Steiner написал:
Thanks for the detailed followup! Lots to digest. It looks like you were having trouble accessing the Shared Library from generated pipeline scripts, which is a little different from my problem: I want to reference YAML data files stored in the Jenkins Shared Library from inside a Job DSL script, but now it appears that's not possible (at least I don't see anything like @Library for the Job DSL API), so I think I also need to rework the overall logic for my build environment. 

On Tuesday, December 18, 2018 at 4:27:57 AM UTC-8, [hidden email] wrote:
Well, not exactly. I found a workaround by reworking the entire logic.

My landscape is: one master declarative pipeline kicks off multiple child pipeline jobs. 
Both the master and the child pipelines are instances created out of shared libraries (jobBuilder, pipelineBuilder) and I want to be able to run the child items as standalone pipelines (in isolation) as well as running them from master pipeline context (different params).

The child job's Jenkins file looks like this (the syntax is not declarative but scripted - the shared library is loaded on the first line):

library identifier: 'templates@branches/rel/10.4.0.x/build/change-management/jenkinsfiles/templates', retriever: modernSCM([$class: 'SubversionSCMSource', remoteBase: 'SVN', credentialsId: 'USER'])

stage('Run Test Suite') {
    jobBuilder {
nodeLabels = 'linux'
antExtraArgs = 'test -Dtest.suite=com.pcbsys.suites.nJMS.PromotionTests'
jobTimeoutHours = 3
}
}


jobsdsl syntax was used only to create the actual Jenkins child pipeline:

script {
    for (String jobName : allJobs) {
        jobDsl scriptText: """
            pipelineJob('${nirvanaMajor}.${nirvanaMinor}_${devJobPrefix}${jobName}') {
                 parameters {
                    stringParam('branch','${svnBranch}','svn repository url')
                    stringParam('buildmajor',"${nirvanaMajor}",'release major identifier')
                    stringParam('buildminor',"${nirvanaMinor}",'release minor identifier')
                    stringParam('fix', '${env.fix}','fix level')
                    stringParam('buildnumber', '${env.buildNumber}','artifacts build number')
                    stringParam('revision', '${env.buildNumber}','checkout revision')
                    stringParam('parentjob', '${pipelineName}','optional parent job')
                }
                removedJobAction: 'IGNORE'
                definition {
                    cpsScm {
                        scm {
                            svn {
                                location ('${svnBranch}/${jobName}') {
                                    credentials('user')
                                }
                            }
                        }
                        scriptPath('Jenkinsfile') 
                    }
                }
            }
            """.stripIndent()
    }
}

On Monday, December 17, 2018 at 11:51:39 PM UTC+2, Philip Steiner wrote:
Hi tre..., I know this thread is getting old, but did you ever get an answer or figure out how to use a Jenkins Shared Library in the Job DSL script, or some workaround?

Thanks
Philip

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/9108f06d-c129-4d46-b0ee-e259bc948ef4%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Usage of Jenkins shared library in Job DSL

Tomas Bjerre
In reply to this post by treneva
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 [hidden email]:
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: <a href="https://jenkins.io/doc/book/pipeline/shared-libraries/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fjenkins.io%2Fdoc%2Fbook%2Fpipeline%2Fshared-libraries%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzi5Xo827_lODtglxm9wkc-kRKTg&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fjenkins.io%2Fdoc%2Fbook%2Fpipeline%2Fshared-libraries%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzi5Xo827_lODtglxm9wkc-kRKTg&#39;;return true;">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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/0f40aa87-af1d-4f5c-b263-76d3159cb0db%40googlegroups.com.