I am into a scenario where I need to read the console output and find a specific string and set this as environment variable. This variable I would be using in input to run a different script in same job.
for example: my jenkins job's console would contain something like
build_id: 123456
Can somebody help in finding this number and pass it to input.environment variable to other script in same job?
I have looked into this answer but its not working, I am getting groovy errors while running it in post build groovy script.
Jenkins pipeline, is there a way to set environment variable from console output
Script I am using:
import jenkins.model.*
jenkins = Jenkins.instance
def consoleLog = Jenkins.getInstance().getItemByFullName(env.JOB_NAME).getBuildByNumber(Integer.parseInt(env.BUILD_NUMBER)).logFile.text
def buildId = (consoleLog =~ 'build_id="(.*)"')[0][1]
echo "build_id: $buildId"
env.build_id = buildId
Error I am getting:
ERROR: Failed to evaluate groovy script.
groovy.lang.MissingPropertyException: No such property: env for class: Script1
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:52)
From jenkins script console, how can I initiate a build for a job?
Tried:
for(job in Hudson.instance.getView(view_name).items) {
job.startBuild()
}
Error:
groovy.lang.MissingMethodException: No signature of method: hudson.model.FreeStyleProject.startBuild() is applicable for argument types: () values: []
You can use run.scheduleBuild, as example
cause = new hudson.model.Cause.RemoteCause(startServer, startNote)
failedRuns.each{run -> run.scheduleBuild(cause)}
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
I am trying to execute below commands in Jenkins groovy script.
import hudson.model.*
def buildA = build("Master-Build")
println(buildA.getProject().getLastSuccessfulBuild())
and I am getting below errors
00:00:00.652 FATAL: No signature of method:
hudson.model.FreeStyleBuild.call() is applicable for argument types:
(java.lang.String) values: [Master-Build] 00:00:00.652 Possible
solutions: wait(), save(), any(), wait(long),
each(groovy.lang.Closure), any(groovy.lang.Closure) 00:00:00.652
groovy.lang.MissingMethodException: No signature of method:
hudson.model.FreeStyleBuild.call() is applicable for argument types:
(java.lang.String) values: [Master-Build]
How can I fix this?
Looking at the error it looks like you are calling Execute system Groovy script build step in a freestyle build. When you use groovy in that step, then the variable build is predefined to the current build and not a function for building other jobs (you might have confused it with the predefined functions/variables in Build Flow Job where the build variable is a function which starts a new build).
So in order to start a new build, you need to access the Jenkins instance directly and tell it to start a new job (inspired by this script):
// Import Jenkins
import jenkins.model.Jenkins;
// Get Jenkins instance
def j = Jenkins.getInstance();
// Get the job we wan't to trigger
def job = j.getItem("B");
// Finally we schedule a new build which starts directly (the zero in the argument)
job.scheduleBuild2(0)
I have written a groovy script to collect some statistics from my Jenkins server, which works ok in the default case.
However, when I want to pass options to it, I am getting trouble:
(1) The standard groovy CliBuilder exists, but fails to instantiate under jenkins-cli:
import jenkins.model.*
import groovy.util.CliBuilder
println("CliBuilder imported; calling constructor...")
def cli = new CliBuilder(usage: 'myscript.groovy [-halr] [name]')
results in
$ java -jar "jenkins-cli.jar" -s https://myjenkins1/ groovy myscript.groovy
CliBuilder imported; calling constructor...
ERROR: Unexpected exception occurred while performing groovy command.
java.lang.NoClassDefFoundError: org/apache/commons/cli/ParseException
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:195)
at RemoteClass.class$(RemoteClass)
at RemoteClass.$get$$class$groovy$util$CliBuilder(RemoteClass)
at RemoteClass.run(RemoteClass:4)
at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:266)
....
(2) the options starting with a dash are intercepted by jenkins-cli, instead of passing it to the script
$ java -jar "jenkins-cli.jar" -s https://myjenkins/ groovy ./myscript.groovy -h
ERROR: "-h" is not a valid option
java -jar jenkins-cli.jar groovy [SCRIPT] [ARGUMENTS ...] [--username VAL] [--password VAL] [--password-file VAL]
Executes the specified Groovy script.
SCRIPT : Script to be executed. File, URL or '=' to represent
stdin.
ARGUMENTS : Command line arguments to pass into script.
--username VAL : User name to authenticate yourself to Jenkins
--password VAL : Password for authentication. Note that passing a
password in arguments is insecure.
--password-file VAL : File that contains the password
Does it mean that the jenkins-cli groovy interface is meant only for simple tasks and should not handle complicated command lines?
Or are these known caveats with known workarounds?
A few issues here:
The jenkins-cli.jar requires groovy to be piped in via stdin.
Command line arguments are read via the args array
It does appear that args with a - are passed to the local jenkins-cli.jar and not the groovy script running on the server.
So given the following groovy script saved as cli.groovy:
println(args)
You would run it like this:
$ java -jar "jenkins-cli.jar" -s https://myjenkins/ groovy = foo bar baz bat < myscript.groovy
Which prints:
[foo, bar, baz, bat]