Jenkins/Hudson Groovy Script Console: Flexibility - groovy

I really like being able to run Groovy scripts in Hudson (or Jenkins, but I use Hudson).
For example, see my question In Groovy, how do I get the list of parameter names for a given job? Hudson parameter names question][1]
The thing is, now I'd like use these Groovy scripts like a keyboard macro or utility. I want to be visiting one of my jobs, hit the special keystroke, and voila, the Groovy script is run. I leave it as an exercise for myself to pick up parameters from environment of current job.
Does anybody out there do this sort of thing, and if so, what strategy has been useful. So far, all I know how to do is bring up the script console, pasted in my code, edit the variable containing the name of the Hudson job, and hit "run". Kinda klunky. Suggestions appreciated.

You can use jenkins Remote access method to do this. The Jenkins wiki describes how to use Remote access:
User can execute groovy scripts remotely sending post request to
/script/ url or /scriptText/ to have response returned without the
html wrapping.
$ curl -d "script=<your_script_here>" http://jenkins/script
$ # or
$ curl -d "script=<your_script_here>" http://jenkins/scriptText
Also, Jenkins CLI offers the possibility to execute groovy
scripts remotely using groovy command or execute groovy interactivelly
via groovysh.

Related

How to clear the consul in jmeter - using groovy

I have a question about the printing that I perform in Jmeter, I use
System.out.println and the consul is full with data.
the problem is that if I want in each iteration to clear the consul how can I do it?
the consul is full with data from last run, or Prev thread and I just want to clear it.
regards (it is the black consul that opened in a new window.)
You should log messages using log.info("Hello World"); (log is a script global variable) instead of using OUT in your scripts. Therefore, the logs will be sent to jmeter.log file.
See the documentation about JSR223 Sampler for more information.
For Windows you can use something like:
new ProcessBuilder('cmd', '/c', 'cls').inheritIO().start().waitFor()
For other operating system it will depend on SHELL implementation, you will need to amend the command line according to your requirements, most likely using clear command.
References:
ProcessBuilder documentation
Groovy is the New Black

integrating protractor with Jenkins

When I want to run specific test or suites. I run them from terminal.
I've installed jenkins and configured my first free style project.
I added shell command (ex: protractor conf.js --suites A --params.user =A).
Everything works fine. If I want to run multiple suites I must edit my shell command inside jenkins. Is there any workaround?, like checkboxes, so I can check which suites I want to run.
Also I want to know about extensible parameters. I want to select which parameters I want to run. Instead of putting command protractor conf.js --params.user=oneuser I want to be able to choose it from GUI.
Look into parameterized builds.
"First, you need to define parameters for your job by selecting "This build is parameterized", then using the drop-down button to add as many parameters as you need."
"String parameters are exposed as environment variables of the same name. Therefore, a builder, like Ant and Shell, [or protractor] can use the parameters."
So if you make "protractorSuites" a string parameter, you can reference it like:
protractor conf.js --suites ${protractorSuites} --params.user =A
Then when you "Build with parameters" you can supply the appropriate suite.

Read file from Jenkins workspace with System groovy script

I have a question very similar to this: Reading file from Workspace in Jenkins with Groovy script
However I need to read the file from a System Groovy script so the solution of using Text-finder or the Groovy PostBuild plugin will not work.
How can I get the workspace path from a system groovy script? I have tried the following:
System.getenv('WORKSPACE')
System.getProperty("WORKSPACE")
build.buildVariableResolver.resolve("WORKSPACE")
Thanks!
If you have a file called "a.txt" in your workspace, along with a script called "sysgvy.groovy" that you want to execute as a system groovy script. Suppose your "sysgvy.groovy" script needs to read the file "a.txt".
The issue of this topic is that if your script read "a.txt" directly without providing any path, "sysgvy.groovy" executes and will throw an error saying cannot find "a.txt".
I have tested and found that the following method works good.
def build = Thread.currentThread().executable
Then use
build.workspace.toString()+"\\a.txt"
as the full location string to replace "a.txt".
It's also important to run on the Jenkins master machine by placing "a.txt" and "sysgvy.groovy" onto Jenkins master machine's workspace. Executing on slave machine does not work.
Try it, the file should be found and get read in the script without any problem.
If there is problem with variable Thread, it is just that some modules need to be imported. So add these lines to the start of code:
import jenkins.*
import jenkins.model.*
import hudson.*
import hudson.model.*
Each build has a workspace, so you need to find the desired project first. (The terms "job" and "project" are used rather interchangeable in Jenkins - also in the API.)
After that, you can either cross your fingers and just call getWorkspace(), which is deprecated (see JavaDoc for details).
Or you can find a specific build (e.g. the last), which can give you the workspace used for that specific build via the getWorkspace() method as it is defined in the AbstractBuild class.
Example code:
Jenkins.instance.getJob('<job-name>').lastBuild.workspace;
Just use
build.workspace
The "build" variable is available keyword in System Groovy Script.

GGTS can't run Groovy Shell

I'm using the Groovy Grails Tool Suite to practice Groovy. I want to run a Groovy Shell, but when I create a new shell and try to run it, I get this error:
Could not find $jarName on the class path. Please add it manually
What does this mean, and how do I resolve this?
I believe this is happening because JLine can't be found on your classpath. I submitted a PR to make the error message in this case actually useful.
I had a similar problem with this exact same message, but the reason was that I was attempting to run the script without specifying which script to run. Ensure you have the script open in the editing window and trying running it again - that got rid of the message for me.

Fail hudson build with groovy script

I have a couple of post build actions that I run on my Hudson build. They are currently bash scripts, and if I want to fail the biuld from them, I just return a non-zero value. Is there a way to fail the build from another scripting language (such as groovy)? Do I just write a script that also returns non-zero? Or is there a particular plugin I need to enable this?
thanks,
Jeff
A way to do this programmatically within the Groovy script is:
throw new hudson.AbortException("Something was in my eye...")
The Groovy Postbuild Plugin seems to be what you need, and has a method called buildFailure() which sets the build result to FAILURE.
If your post build action is a standard build step (like a shell script). it is enough to exit that shell script with a non-zero value. I don't know if all scripting languages allow you to return a non-zero return value, that will then become the return value of the script (if you don't call exit or an equivalent command specifically in a script than the return value of the last executed command becomes the return value of the script).
If you have troubles with this approach, there is always the option to use the Log Parser Plugin to fail a build on error.

Resources