How to write on Jenkins console output? - jenkins

I'm coding a library for my Jenkins pipelines. I would like to print different formats for my messages, like [INFO], [WARNING] and so on.
So far I have this:
import org.foo.Output
def call(body) {
def config = [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = config
body()
def out = new Output()
node("${config.slaveNodeName}") {
try {
stage ('CLONE') {
out.info("SOME VERY USEFUL INFORMATION")
...
And at my org.foo.Output class:
package org.foo
import java.util.logging.Logger
class Output {
private static final Logger LOGGER = Logger.getLogger(Output.class.getName());
def info(msg){
LOGGER.info("${msg}")
echo "[INFO] ${msg}" <-- gives me an exception described below
}
}
I can see the [INFO] SOME VERY USEFUL INFORMATION on my Jenkins log, however, I would like to redirect this message to Jenkins output console.
How can I do that?
Exception:
hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: org.foog.Output.echo() is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl) values: [[INFO] SOME VERY USEFUL INFORMATION]
Possible solutions: each(groovy.lang.Closure), info(java.lang.Object), wait(), grep(), any(), find()
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:54)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall(DefaultInvoker.java:19)

Valter, try this one. It's working in my scripts, I think it would also work in your scripts.
In groovy script, add this:
node("${config.slaveNodeName}") {
try {
stage ('CLONE') {
out.info(this,"SOME VERY USEFUL INFORMATION")
and change the class groovy script like this:
def info(script,msg){
LOGGER.info("${msg}")
script.echo "[INFO] ${msg}"
}

Related

How to run jenkins job from groovy script

is it possible to run jenkins job from groovy script?
I know it is possible to run jenkins job from pipeline like this
build job: 'test'
this also doesn't work
build('test')
error for
build job: 'test'
Caught: groovy.lang.MissingMethodException: No signature of method: hudson8298793087824999032.build() is applicable for argument types: (LinkedHashMap) values: [[job:test]]
Possible solutions: find(), find(groovy.lang.Closure), wait(), run(), run(), dump()
groovy.lang.MissingMethodException: No signature of method: hudson8298793087824999032.build() is applicable for argument types: (LinkedHashMap) values: [[job:test]]
Possible solutions: find(), find(groovy.lang.Closure), wait(), run(), run(), dump()
at hudson8298793087824999032.run(hudson8298793087824999032.groovy:32)
Build step 'Execute Groovy script' marked build as failure
After edit Execute system Groovy script and added
def currentBuild = Thread.currentThread().executable
def job = hudson.model.Hudson.instance.getJob("jobname")
println(job)
def params = new StringParameterValue('version', '1.0.0')
println(params)
def params1 = new StringParameterValue('target', "dev")
println(params1)
def paramsAction = new ParametersAction(params, params1)
println(paramsAction)
def cause = new hudson.model.Cause.UpstreamCause(currentBuild)
println(cause)
def causeAction = new hudson.model.CauseAction(cause)
println(causeAction)
println('test')
Hudson.instance.queue.schedule(job, 0, causeAction, paramsAction)
I got error on this line Hudson.instance.queue.schedule(job, 0, causeAction, paramsAction)
java.lang.NullPointerException
at hudson.model.queue.Tasks.getOwnerTaskOf(Tasks.java:60)
at com.cloudbees.hudson.plugins.folder.ItemDeletion.getItemOf(ItemDeletion.java:197)
at com.cloudbees.hudson.plugins.folder.ItemDeletion.shouldSchedule(ItemDeletion.java:174)
at hudson.model.Queue.schedule2(Queue.java:589)
at hudson.model.Queue.schedule2(Queue.java:712)
at hudson.model.Queue.schedule(Queue.java:705)
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:497)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoCachedMethodSite.invoke(PojoMetaMethodSite.java:192)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:56)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:149)
at Script1.run(Script1.groovy:47)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:585)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:623)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:594)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript.evaluate(SecureGroovyScript.java:343)
at hudson.plugins.groovy.SystemGroovy.run(SystemGroovy.java:95)
at hudson.plugins.groovy.SystemGroovy.perform(SystemGroovy.java:59)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:741)
at hudson.model.Build$BuildExecution.build(Build.java:206)
at hudson.model.Build$BuildExecution.doRun(Build.java:163)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:504)
at hudson.model.Run.execute(Run.java:1818)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)
Build step 'Execute system Groovy script' marked build as failure
For me, I needed the code to run on the master to have quick access to the Jenkins objects and build a job that has the appropriate permissions to send a mail within the information acquired.
I used the build module Execute system Groovy Script to run the script and appended the following code:
// The job you want to run from groovy
def job = hudson.model.Hudson.instance.getJob("SendEmailJob")
// jobsList is a list of jobs of interest formated as HTML
def body = jobsList
def params = []
params += new StringParameterValue('TO', "<team#example.com>")
params += new StringParameterValue('BODY_AS_HTML', body)
params += new StringParameterValue('SUBJECT', "List of jobs of interest")
def paramsAction = new ParametersAction(params)
def cause = new hudson.model.Cause.UpstreamCause(build)
def causeAction = new hudson.model.CauseAction(cause)
hudson.model.Hudson.instance.queue.schedule(job, 0, causeAction, paramsAction)
This is based on question how-do-i-dynamically-trigger-downstream-builds-in-jenkins

Trigger Jenkins Job from shared library

This is what I have in my shared library file
build job: 'Job Name',
parameters:
[
string(name: 'ENVIRONMENT', value: 'sit'),
string(name: 'param1', value: 'value1' )
]
It is failing with below error :
hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: build.call() is applicable for argument types: (java.util.LinkedHashMap) values: [[job:**********, parameters:[#string(name=ENVIRONMENT,value=sit), ...]]]
Possible solutions: call(java.lang.Object, java.lang.Object, java.lang.Object), wait(), any(), wait(long), main([Ljava.lang.String;), any(groovy.lang.Closure)
Any help here?
Library classes cannot directly call steps such as sh or git. They can however implement methods, outside of the scope of an enclosing class, which in turn invoke Pipeline steps, for example:
// src/org/foo/Zot.groovy
package org.foo;
def checkOutFrom(repo) {
git url: "git#github.com:jenkinsci/${repo}"
}
return this
Which can then be called from a Scripted Pipeline:
def z = new org.foo.Zot()
z.checkOutFrom(repo)
This approach has limitations; for example, it prevents the declaration of a superclass.
Alternately, a set of steps can be passed explicitly using this to a library class, in a constructor, or just one method:
package org.foo
class Utilities implements Serializable {
def steps
Utilities(steps) {this.steps = steps}
def mvn(args) {
steps.sh "${steps.tool 'Maven'}/bin/mvn -o ${args}"
}
}
When saving state on classes, such as above, the class must implement the Serializable interface. This ensures that a Pipeline using the class, as seen in the example below, can properly suspend and resume in Jenkins.
#Library('utils') import org.foo.Utilities
def utils = new Utilities(this)
node {
utils.mvn 'clean package'
}
If the library needs to access global variables, such as env, those should be explicitly passed into the library classes, or methods, in a similar manner.
Instead of passing numerous variables from the Scripted Pipeline into a library,
package org.foo
class Utilities {
static def mvn(script, args) {
script.sh "${script.tool 'Maven'}/bin/mvn -s ${script.env.HOME}/jenkins.xml -o ${args}"
}
}
The above example shows the script being passed in to one static method, invoked from a Scripted Pipeline as follows:
#Library('utils') import static org.foo.Utilities.*
node {
mvn this, 'clean package'
}
For more info see jenkins shared library documentation: https://jenkins.io/doc/book/pipeline/shared-libraries/
Try adding propagate and wait like below:
build job: 'Job Name', parameters: [ string(name: 'ENVIRONMENT', value: 'sit'), string(name: 'param1', value: 'value1' ) ],propagate: true, wait: true
Ok. So I figured out the problem.
One of the shared file name was build.groovy which was causing conflicts with build pipeline step. Renamed the file and that fixed the issue.

Unable to run shell command in Groovy in Jenkins

I am trying to get certain values from the slave by running shell commands such as :
git rev-parse HEAD
git config --get remote.origin.url
The method that I have tried to write for this is :
def executeCommand(String command) {
stdout = sh script: command, returnStdout: true
return stdout.trim()
}
Now when I try to run the first command :
output = executeCommand('git rev-parse HEAD')
I get the ERROR :
[Running] groovy "/Users/user-a/Documents/cmd.groovy"
Caught: groovy.lang.MissingMethodException: No signature of method: cmd.sh() is applicable for argument types: (LinkedHashMap) values: [[script:git rev-parse HEAD, returnStdout:true]]
Possible solutions: is(java.lang.Object), use([Ljava.lang.Object;), run(), run(), any(), tap(groovy.lang.Closure)
groovy.lang.MissingMethodException: No signature of method: cmd.sh() is applicable for argument types: (LinkedHashMap) values: [[script:git rev-parse HEAD, returnStdout:true]]
Possible solutions: is(java.lang.Object), use([Ljava.lang.Object;), run(), run(), any(), tap(groovy.lang.Closure)
at cmd.executeCommand(cmd.groovy:2)
at cmd.run(cmd.groovy:6)
I also tried:
output = command.execute().text
But this returns nothing.
Im running out of ideas on how to run shell commands in Groovy in Jenkins and record the output.
MORE DETAILS
I am working with Jenkins shared Libraries. I have exposed a method in for my Jenkinsfile by the name getLatestBuildDetails(). This method is defined within my library. One of the actions within the method is to execute the git commands locally. So inorder to run any shell command locally, I have created the executeCommand function which takes the actual command to run as a String and executes it and returns the output to be used later by getLatestBuildDetails()
Library classes cannot directly call steps such as sh or git. They can however implement methods, outside of the scope of an enclosing class, which in turn invoke Pipeline steps, for example:
// src/org/foo/Zot.groovy
package org.foo;
def checkOutFrom(repo) {
git url: "git#github.com:jenkinsci/${repo}"
}
return this
Which can then be called from a Scripted Pipeline:
def z = new org.foo.Zot()
z.checkOutFrom(repo)
This approach has limitations; for example, it prevents the declaration of a superclass.
Alternately, a set of steps can be passed explicitly using this to a library class, in a constructor, or just one method:
package org.foo
class Utilities implements Serializable {
def steps
Utilities(steps) {this.steps = steps}
def mvn(args) {
steps.sh "${steps.tool 'Maven'}/bin/mvn -o ${args}"
}
}
When saving state on classes, such as above, the class must implement the Serializable interface. This ensures that a Pipeline using the class, as seen in the example below, can properly suspend and resume in Jenkins.
#Library('utils') import org.foo.Utilities
def utils = new Utilities(this)
node {
utils.mvn 'clean package'
}
If the library needs to access global variables, such as env, those should be explicitly passed into the library classes, or methods, in a similar manner.
Instead of passing numerous variables from the Scripted Pipeline into a library,
package org.foo
class Utilities {
static def mvn(script, args) {
script.sh "${script.tool 'Maven'}/bin/mvn -s ${script.env.HOME}/jenkins.xml -o ${args}"
}
}
The above example shows the script being passed in to one static method, invoked from a Scripted Pipeline as follows:
#Library('utils') import static org.foo.Utilities.*
node {
mvn this, 'clean package'
}
In your case you should write something like:
def getLatestBuildDetails(context){
//...
executeCommand(context, 'git rev-parse HEAD')
//...
}
def executeCommand(context, String command) {
stdout = script.sh(script: command, returnStdout: true)
return stdout.trim()
}
Jenkins file:
#Library('library_name') _
getLatestBuildDetails(this)
For more info see jenkins shared library documentation: https://jenkins.io/doc/book/pipeline/shared-libraries/
I am also using shared libraries. This is how I have used in my code:
String getMavenProjectName() {
echo "inside getMavenProjectName +++++++"
// mavenChartName = sh(
// script: "git config --get remote.origin.url",
// returnStdout: true
// ).trim()
def mavenChartName = sh returnStdout:true, script: '''
#!/bin/bash
GIT_LOG=$(env -i git config --get remote.origin.url)
basename "$GIT_LOG" .git; '''
echo "mavenChartName: ${mavenChartName}"
return mavenChartName
}
PS: Ignore the commented lines of code.
Try out the sh step instead of execute. :)
EDIT:
I would go with execute() or which I think it is even better, grgit.
I think you are not getting any output when you run cmd.execute().text because .text returns the standard output of the command and your command might only use the standard error as its output, you can check both:
def process = cmd.execute()
def stdOut = process.inputStream.text
def stdErr = process.errorStream.text

How do I solve "Expected named arguments" for freeStyleJob?

I have several different projects that will be compiled in Jenkins and shall be uploaded to my Nexus3 repository. For that I am using the NexusArtifcalUploader. For some reason I get the following error message although the code is essentially copied from the plugin page of the Jenkins wiki.
java.lang.IllegalArgumentException: Expected named arguments but got [clientmoduleNexusArtifactUploaderJob, org.jenkinsci.plugins.workflow.cps.CpsClosure2#63d801fc]
at org.jenkinsci.plugins.workflow.cps.DSL.parseArgs(DSL.java:511)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeDescribable(DSL.java:291)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:153)
at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:108)
at sun.reflect.GeneratedMethodAccessor463.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
...
My Jenkinsfile calls the uploadToNexus method I created which creates freeStyleJobs:
def uploadToNexus(module) {
def groupId = "com.example"
def moduleVersions = [
"client-module": "1.0.0-SNAPSHOT",
"server-module": "1.0.0-SNAPSHOT",
]
def moduleVersion = moduleVersions.get(module)
def jobName = "${fixModuleName(module)}NexusArtifactUploaderJob"
echo "will run freeStyleJob ${jobName} now..."
freeStyleJob(jobName) {
steps {
nexusArtifactUploader {
nexusVersion('nexus3')
protocol('http')
nexusUrl('nexus:8081')
groupId(groupId)
version(moduleVersion)
repository('maven2_central')
credentialsId('nexus_admin')
artifact {
artifactId('${module}')
type('war')
classifier('debug')
file('${module}.war')
}
}
}
}
}
To my knowledge freeStyleJob expects a string which I pass, don't I? What am I missing and doing wrong?
It seems that I mixed up Job DSL and Pipeline DSL. I wasn't aware there's a difference.
Here's a way to use Job DSL inside Pipeline DSL:
https://github.com/jenkinsci/job-dsl-plugin/wiki/User-Power-Moves#use-job-dsl-in-pipeline-scripts

Assigning a step to a variable in Jenkins Pipeline

I have the following pipeline script:
node {
def myStep = sh
myStep "ls -la"
}
I thought steps were visible as variables and could be assigned to variables so that they can be used later (for example choosing a different step depending on some conditions).
However, this fails with:
[Pipeline] End of Pipeline
groovy.lang.MissingPropertyException: No such property: myStep for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:232)
at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:282)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:286)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:28)
at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
at WorkflowScript.run(WorkflowScript:3)
at ___cps.transform___(Native Method)
How can I put a step in a variable to use it later without hardcoding its name?
You can write a method in your pipeline that wraps the behavior you want. It will have access to the script variables.
node {
myStep("ls -la")
}
def myStep(String script) {
sh(script)
}
My current workaround:
node {
def myStep = { script ->
sh script
}
myStep("ls -la")
}

Resources