I'm trying to write a groovy method for Jenkins pipeline. The method gets two variables, put them inside a string, and execute the string as a shell command.
Here is the method:
def method(A, B) {
test = "cp app/scripts/" + A + "config." + B + "js app/scripts/config.js"
def rc = sh(script: test, returnStatus: true)
if (rc != 0) {
error "Failed, exiting now..."
}
}
Here is the error i get when i execute this code:
The current scope already contains a variable of the name rc
# line 148, column 13.
def rc = sh(script: """
^
Does anyone know what could be the issue?
Related
I need to run a python script in windows system using groovy script.
Example:
python.exe c:/main.py argument1
I am new to groovy and I don't know, how to do it.
Please share me groovy syntax to run python as mentioned in the above example
I am preparing this script for jenkins.
so, "command".execute() is the right start.
But this command only starts a thread and you don't wait for the result.
try this code:
def task = "python main.py".execute()
task.waitFor()
println task.text
These lines start the execution, wait for it to finish and print the result.
To output already during execution for longer running tasks, I've written myself a small helper:
String.metaClass.executeCmd = { silent ->
//make sure that all paramters are interpreted through the cmd-shell
//TODO: make this also work with *nix
def p = "cmd /c ${delegate.value}".execute()
def result = [std: '', err: '']
def ready = false
Thread.start {
def reader = new BufferedReader(new InputStreamReader(p.in))
def line = ""
while ((line = reader.readLine()) != null) {
if (silent != false) {
println "" + line
}
result.std += line + "\n"
}
ready = true
reader.close()
}
p.waitForOrKill(30000)
def error = p.err.text
if (error.isEmpty()) {
return result
} else {
throw new RuntimeException("\n" + error)
}
}
This defines through meta programming a new method on String called executeCmd.
Put this on top of your file and then your line
"python c:/main.py".executeCmd()
This should show you all output during execution and it will help you to handle the paramaters the correcct way through the "cmd /c"-prefix. (If you just call execute on a string, you often run into problems with spaces and other characters in your command.
If you already have the parameters as a list and need some code which also runs on a *nix machine, try to call execute() on a list:
["python", "c:/main.py"].execute()
hope this helps
ps: http://mrhaki.blogspot.com/2009/10/groovy-goodness-executing-string-or.html
I have a Jenkinsfile project that requires me to include an 'if statement to ascertain if shell commands within certain methods return an exit code outside of 0.
The first method method 1 works as expected. However i would like to include an if statement to skip the second stage since that shell command in method 2 doesn't exit with 0.
def method1(setup){
sh """
echo ${setup}
"""
}
def method2(setup){
sh """
ech ${setup}
"""
}
node {
stage('print method1'){
method1('paul')
}
// I need an if statement to skip this method since the if statement returns non=zero
stage('This method should be skipped'){
if(method2 returns != 0) //This if condition fails but need something similar to this
method1('paul')
}
}
Any help with this is much appreciated.
You use a default sh step execution in your example which means that exit code is not returned from the command. If exist status is something else than 0 in such case, pipeline fails with the exception. If you want to return exit status and allow the pipeline to continue you have to pass returnStatus: true option, for instance:
int status = sh(script: """
echo ${setup}
""", returnStatus: true)
if (status != 0) {
// do something
}
Source: sh step pipeline documentation
I'm quite new in Groovy.
Basically I load a text file, then I need to get a specific value at one line (actually the 6th).
The line is like:
STATIC_ASSERT(VERSION == 888888, "blablabla");
I need to get the 888888 value.
I found a way using multiple split but it's ugly.
I also think of using something like:
line.substring(ind+"VERSION ==".length(), line.length()-10).trim();
But the "blablabla" length can change..
Thank you.
Edit: It works using an hardcoded string like this.
But when I try to run it from the file I get this error:
test' is failed: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
script1516208488151762512206.groovy: 4: expecting '}', found '' # line 4, column 69.
ne.contains('VERSION ==')
^
1 error
Here is my code:
${groovy:
String Ver
def file = new File("C:\\test.cpp")
def data = file.filterLine { line ->
line.contains('VERSION ==')
}
Ver = data.split("==")[1].split(",")[0].trim()
logger.info(Ver)
}
--
I also tried something like this:
${groovy:
String Ver
def file = new File("C:\\test.cpp")
while ((line = file.readLine())!=null)
{
int ind = line.indexOf("VERSION ==")
if (ind >= 0)
{
Ver = line.split("==")[1].split(",")[0].trim()
}
}
logger.info(Ver)
}
But I get same kind of weird error:
expecting '}', found '' # line 9, column 58.
("==")[1].split(",")[0].trim()
^
1 error
:(
You do as shown below:
def line = 'STATIC_ASSERT(VERSION == 888888, "blablabla");'
println line.split('==')[1].split(',')[0].trim()
When executing a bat file from Groovy, the output of this is not printed from the Groovy script until the bat file is complete. To compare, I tested the same exact bat file from C# and Perl. These both print the output of the bat file as it's being written to STDOUT.
def cmd = "batfile.bat param1"
println cmd.execute().text()
Is there a way to tell Groovy to read the stream and print immediately?
Thank you for the response! I addition to note is when using the recommendation the exec did not wait for the process to complete, which we desire in this case, so adding process.waitFor() accomplished this. Working code example below. (Note test.bat is anything you like, such as: sleep 5)
import groovy.time.*
def times = [:]
def progStartTime = new Date()
String[] caches = ["cache1", "cache2", "cache3"]
def cmd
def batFile = "test.bat "
println new Date()
for (String item : caches) {
def timeStart = new Date()
cmd = [batFile, item]
//println cmd.execute().text
def process = cmd.execute()
process.consumeProcessOutput(System.out, System.err)
process.waitFor()
def timeStop = new Date()
TimeDuration duration = TimeCategory.minus(timeStop, timeStart)
println "cache: " + item + " : " + duration
times.put(item,duration)
}
times.each{ k, v -> println "cache: ${k}, took: ${v}" }
def progStopTime = new Date()
TimeDuration duration = TimeCategory.minus(progStopTime, progStartTime)
println "Total Program duration: " + duration
println new Date()
First of all I believe it should read:
cmd.execute().text
without parenthesis so that we call the groovy Process.getText() method. However that will not solve your problem as the getText() method waits for process completion before returning.
If you don't need control of the output but just want it directly on standard out and standard err, you can use the groovy Process.consumeProcessOutput() method:
def process = "batfile.bat param1".execute()
process.consumeProcessOutput(System.out, System.err)
This will output the process out and err stream output directly on the system out and err streams as the output becomes available.
If you need processing or control, something like the following should solve your problem:
def process = "batfile.bat param1".execute()
process.in.withReader { r ->
r.eachLine { line ->
// some token processing
println "batfile output> ${line.toUpperCase()}"
}
}
also parameters with spaces tend to cause havoc so I have found it is often safer to use the groovy List.execute() form instead as in:
def process = ["batfile.bat", "param1"].execute()
which does the same thing but keeps parameter integrity with regards to spaces.
I have a closure working properly on traverse, but another of the same kind is failing. I'm suspecting scope or timing is causing this to fail. The working code sums the size of files in the file system. The code not working is inspecting the content of the file and only prints one match. Running these with Grails 2.3.7
working code:
def groovySrcDir = new File('.', 'plugins/')
def countSmallFiles = 0
def postDirVisitor = {
if (countSmallFiles > 0) {
println "Found $countSmallFiles files with small filenames in ${it.name}"
}
countSmallFiles = 0
}
groovySrcDir.traverse(type: FILES, postDir: postDirVisitor, nameFilter: ~/.*\.groovy$/) {
if (it.name.size() < 15) {
countSmallFiles++
}
}
problem code:
def datamap = [:]
def printDomainFound = {
//File currentFile = new File(it.canonicalPath)
def fileText = it.text
if(fileText.indexOf("#Table ") > 0){
//println "Found a Table annotation in ${it.name} "
datamap.put(it.name, it.name)
}
}
groovySrcDir.traverse type: FILES, visit: printDomainFound, nameFilter: filterGroovyFiles
datamap.each {
println it.key
}
I tested your code and worked fine.
Which behaviour are you expecting?
I find a couple of suspicious things:
If fileText begins with "#Table " then indexOf will return 0 and the condition if(fileText.indexOf("#Table ") > 0) will not be satisfied.
"#Table " has a trailing space, then a file containing, for example: "#Table(", will not be printed.
You can also check that filterGroovyFiles has the appropiate value.
I hope it'll help.
-- EDIT --
Running the code with def filterGroovyFiles = ~/.*\.groovy$/ and this file tree:
plugins
|--sub1
| |-dum.groovy
| |-dum2.groovy
dum3.groovy
And all three groovy files containing the (but not starting with!!) "#Table " (with trailing space!!). I get the expected output:
dum3.groovy
dum.groovy
dum2.groovy
(Note both dum.groovy and dum2.groovy from the same folder sub1 are appearing).
I'm using groovy 2.0.5.
Please recheck your files :
Have the correct extension
Contain but not at the begining (index==0) the String "#Table "