Usage of Jenkins shared library in Job DSL

2018-08-17 Thread 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 jenkinsci-users+unsubscr...@googlegroups.com.
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.


Re: Usage of Jenkins shared library in Job DSL

2018-08-26 Thread 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 > 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 jenkinsci-use...@googlegroups.com .
>> 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 jenkinsci-users+unsubscr...@googlegroups.com.
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.


jenkins groovy admin script to restart all slaves

2018-09-07 Thread treneva
Hey,

Trying to create a script that restarts all slaves using Jenkins 
Administrative Script console.

Script 1:
import hudson.util.RemotingDiagnostics;

createShellScriptCommand = "echo \"/usr/sbin/shutdown -r now"
grantExecutionPrvilige = "chmod +x /home/nirdevadm/shutdown.sh"
executeShellScriptCommand = "echo \"vm-pass\" | sudo -S 
'/home/nirdevadm/shutdown.sh'"

for (slave in hudson.model.Hudson.instance.slaves) {
println slave.name;
println RemotingDiagnostics.executeGroovy(createShellScriptCommand, 
slave.getChannel());
println RemotingDiagnostics.executeGroovy(grantExecutionPrvilige, 
slave.getChannel());
println RemotingDiagnostics.executeGroovy(executeShellScriptCommand, 
slave.getChannel());
}

Script 1 fails with:

cript1.groovy: 1: unexpected char: 0x @ line 1, column 32.
   echo "/usr/sbin/shutdown -r now
  ^

1 error

at 
org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at 
org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:150)
at 
org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:120)
groovy.lang.MissingPropertyException: No such property: chmod for class: 
Script1
at 
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
at 
org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:52)
at 
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:307)

groovy.lang.MissingPropertyException: No such property: sudo for class: 
Script1
at 
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
at 
org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:52)

Script 2:


import hudson.slaves.SlaveComputer
import jenkins.model.Jenkins

createShellScriptCommand = "echo \"/usr/sbin/shutdown -r now"
grantExecutionPrvilige = "chmod +x /home/nirdevadm/shutdown.sh"
executeShellScriptCommand = "echo \"vm-pass\" | sudo -S 
'/home/nirdevadm/shutdown.sh'"

Jenkins.instance.getComputers()
.findAll { it instanceof SlaveComputer }
.each { computer ->
Process p = createShellScriptCommand.execute()
Process z = grantExecutionPrvilige.execute()
Process x = executeShellScriptCommand.execute() 
}
return;

Script 2 seems to compile and does NOT fail but it does nothing as the 
slaves are not restarted.


It all boils down to these 3 unix commands that I want to execute trough 
groovy (they work well in isolation, executed directly on the slave):
echo "/usr/sbin/shutdown -r now" > /home/nirdevadm/shutdown.sh
chmod +x /home/nirdevadm/shutdown.sh
echo "vm-pass" | sudo -S '/home/nirdevadm/shutdown.sh'


Any suggestions what am I doing wrong? 

-- 
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/6fcf0d22-7bca-409a-ba4d-1cb06ca336ee%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Collecting build job results using [.result] property returns null

2018-10-03 Thread treneva
I would like to collect all triggered jobs results.
My script looks like this:

stage('Integration Tests') {
steps {
script {
def jobs = [:]
def childJob
def childJobName
pipelineIntegrationJobs.each {
i -> jobs["${nirvanaMajor}.${nirvanaMinor}_${i}"] = {
childJob = build (job: "${nirvanaMajor}.${nirvanaMinor}_${i}", 
parameters: [
string(name: 'branch', value: "${svnBranch}"), 
string(name: 'fix', value: "${env.fix}"), 
string(name: 'buildnumber', value: "${env.buildNumber}"), 
string(name: 'revision', value: "${env.buildNumber}"),
string(name: 'joblabel', value: "${pipelineName}")
], 
quietPeriod: 0, propagate: false, wait: true).result
}
childJobName = "${nirvanaMajor}.${nirvanaMinor}_${i}"
results.put(childJobName, childJob)
}
parallel jobs
}
} 
}

In the post section of the pipeline I have this:

script {
println ""
println "Job Status Summary:"
results.each{ k, v -> println "${k}:${v}" }
println ""
}

This yields properly the job names but not job statuses:

*12:22:41* Job Status Summary:[Pipeline] echo*12:22:41* 
10.4_test_foundation_win:null[Pipeline] echo*12:22:41* 
10.4_test_foundation_linux:null[Pipeline] echo*12:22:41* 
10.4_test_client_win:null[Pipeline] echo*12:22:41* 
10.4_test_client_linux:null[Pipeline] echo*12:22:41* 
10.4_test_base_linux:null[Pipeline] echo*12:22:41* 
10.4_test_base_win:null[Pipeline] echo*12:22:41* 
10.4_test_admin_api_win:null[Pipeline] echo*12:22:41* 
10.4_test_admin_api_linux:null[Pipeline] echo*12:22:41* 
10.4_test_admin_api_cluster_win:null[Pipeline] echo*12:22:41* 
10.4_test_admin_api_cluster_linux:null[Pipeline] echo*12:22:41* 
10.4_test_jms_win:null[Pipeline] echo*12:22:41* 
10.4_test_jms_linux:null[Pipeline] echo*12:22:41* 
10.4_test_client_tool_linux:null[Pipeline] echo*12:22:41* 
10.4_test_client_tool_win:null[Pipeline] echo*12:22:41* 
10.4_test_cluster_tool_linux:null[Pipeline] echo*12:22:41* 
10.4_test_cluster_tool_win:null[Pipeline] echo*12:22:41* 
10.4_test_admin_tool_win:null[Pipeline] echo*12:22:41* 
10.4_test_admin_tool_linux:null[Pipeline] echo*12:22:41* 
10.4_test_cpp_linux:null[Pipeline] echo*12:22:41* 
10.4_test_cpp_win:null[Pipeline] echo*12:22:41* 10.4_test_cpp_osx:null



Could this be because of [wait: true]?


I read different topics that this is the right way to collect the results, but 
it is not working in my case.


What I am missing?

-- 
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/d0971a97-63dd-4e2f-aebf-ef2ece00e4e8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Extract test results data from [build(job: "child_job")] object

2018-11-29 Thread treneva
Hey,

Here is my pipeline landscape:

Master pipeline is triggering smaller child pipelines.

Parent pipeline script snippet:

stage('Integration Tests') {
steps {
script {
def jobs = [:]
pipelineIntegrationJobs.each {
i ->
jobs["${nirvanaMajor}.${nirvanaMinor}_${i}"] = {
def buildInstance = build(job: 
"${nirvanaMajor}.${nirvanaMinor}_${i}",
parameters: [
string(name: 'branch', value: 
"${svnBranch}", description: 'svn repository url'),
string(name: 'buildmajor', value: 
'10', description: 'release major identifier'),
string(name: 'buildminor', value: 
'4', description: 'release minor identifier'),
string(name: 'fix', value: 
"${env.fix}", description: 'fix level'),
string(name: 'buildnumber', value: 
"${env.buildNumber}", description: 'artifacts build number'),
string(name: 'revision', value: 
"${env.buildNumber}", description: 'checkout revision'),
string(name: 'joblabel', value: 
"${pipelineName}", description: "optional job description")
], quietPeriod: 0, propagate: false, wait: 
true)
def downstreamJobName = 
"${masterUrl}/job/${nirvanaMajor}.${nirvanaMinor}_${i}/" + 
buildInstance.number
jobStateResults.put(downstreamJobName, 
buildInstance.result)
}
}
parallel jobs
}
}
}


The child pipeline script uses this method below [getTestResults()] to 
extract the build test results and it works perfect.

I would like to push this data up to the master pipeline so that I collect 
all child jobs results.
 
I see the return type of build(job: "child_job") is a RunWrapper (
https://javadoc.jenkins.io/plugin/workflow-support/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapper.html)
 
but how do I extract AbstractTestResultAction from it?


Child pipeline script snippet:
import hudson.tasks.test.AbstractTestResultAction

@NonCPS
def getTestResults () {
def results = [:]
AbstractTestResultAction testResultAction = 
currentBuild.rawBuild.getAction(AbstractTestResultAction.class)
if (testResultAction != null) {
def total = testResultAction.getTotalCount()
def failed = testResultAction.getFailCount()
def skipped = testResultAction.getSkipCount()
def passed = total - failed - skipped
results.put ("Total", total)
results.put ("Passed", passed)
results.put ("Failed", failed)
results.put ("Skipped", skipped)

if (failed == 0) {
currentBuild.result = 'SUCCESS'
}
echo ""
echo "Test Results Summary:"
results.each{ k, v -> echo "${k}: ${v}" }
echo ""
} else {
echo 'No tests results'
}
return results
}

-- 
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/1a65a9dc-b093-4ed8-9fd0-de433757c202%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Usage of Jenkins shared library in Job DSL

2018-12-18 Thread 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 jenkinsci-users+unsubscr...@googlegroups.com.
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.


collect the result from pipeline triggered downstream jobs

2018-10-16 Thread Vassilena Treneva
Hey,

I have been trying different ways to collect the result from pipeline
triggered downstream jobs without any success. Any idea how to do it?

Apprioach #1 - Mapping the build data trough Cause.UpstreamCause (as
suggested here:
https://groups.google.com/forum/#!topic/jenkinsci-dev/nNvy6YLgP58):

pipelineIntegrationJobs.each {
i -> jobs["${nirvanaMajor}.${nirvanaMinor}_${i}"] = {
build (job: "${nirvanaMajor}.${nirvanaMinor}_${i}",
parameters: [
string(name: 'branch', value: "${svnBranch}", description: 'svn repository
url'),
string(name: 'buildmajor', value: '10', description: 'release major
identifier'),
string(name: 'buildminor', value: '4', description: 'release minor
identifier'),
string(name: 'fix', value: "${env.fix}", description: 'fix level'),
string(name: 'buildnumber', value: "${env.buildNumber}", description:
'artifacts build number'),
string(name: 'revision', value: "${env.buildNumber}", description:
'checkout revision'),
string(name: 'joblabel', value: "${pipelineName}", description: "optional
job description")
], quietPeriod: 0, propagate: false, wait: true).result
def upstreamJob =
Jenkins.instance.getItem("${nirvanaMajor}.${nirvanaMinor}_${env.JOB_NAME}")
def job = Jenkins.instance.getItem("${nirvanaMajor}.${nirvanaMinor}_${i}")
job.builds.each { Run build ->
if (build.result != null) {
Cause.UpstreamCause cause = build.getCause(Cause.UpstreamCause.class)
if (cause.pointsTo(upstreamJob)) {
println(">" + build.number)
}
}
}
}
}

This results in a NPE:
05:34:41 Finished
[Pipeline] script
[Pipeline] {
[Pipeline] echo
05:34:41 
[Pipeline] echo
05:34:41 Job Status Summary:
[Pipeline] echo
05:34:41 10.4:null
[Pipeline] echo
05:34:41 
[Pipeline] echo
05:34:41 
[Pipeline] echo
05:34:41 Test Results Summary:
[Pipeline] echo
05:34:41 
[Pipeline] }
[Pipeline] // script
[Pipeline] echo
05:34:41 Failure
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // timestamps
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
[BFA] Scanning build for known causes...
[BFA] No failure causes found
[BFA] Done. 0s
java.lang.NullPointerException
at hudson.model.Cause$UpstreamCause.pointsTo(Cause.java:262)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
at
org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:47)
at
org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:157)
at
org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
at
org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:133)
at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:155)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:159)
at
com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
at
pipelineBuilder.call(/FS/fslocal/jenkinsWorkspace/jobs/10.4_release_pipeline/builds/84/libs/templates/vars/pipelineBuilder.groovy:182)
at
com.cloudbees.groovy.cps.CpsDefaultGroovyMethods.each(CpsDefaultGroovyMethods:2030)
at
com.cloudbees.groovy.cps.CpsDefaultGroovyMethods.each(CpsDefaultGroovyMethods:2015)
at
com.cloudbees.groovy.cps.CpsDefaultGroovyMethods.each(CpsDefaultGroovyMethods:2056)
at
pipelineBuilder.call(/FS/fslocal/jenkinsWorkspace/jobs/10.4_release_pipeline/builds/84/libs/templates/vars/pipelineBuilder.groovy:179)
at ___cps.transform___(Native Method)

Approach #2 - Using the .result property of the build job (as suggested in
the official documentation here:
https://jenkins.io/doc/pipeline/steps/pipeline-build-step/):

pipelineIntegrationJobs.each {
i -> jobs["${nirvanaMajor}.${nirvanaMinor}_${i}"] = {
childJob = build (job: "${nirvanaMajor}.${nirvanaMinor}_${i}",
parameters: [
string(name: 'branch', value: "${svnBranch}", description: 'svn repository
url'),
string(name: 'buildmajor', value: '10', description: 'release major
identifier'),
string(name: 'buildminor', value: '4', description: 'release minor
identifier'),
string(name: 'fix', value: "${env.fix}", description: 'fix level'),
string(name: '