If this may help someone, here's my finding so far (I must admit I found 
Groovy inside Jenkins to be painful and by far one of my less favorite 
scripting language so far, especially loosing my head with a few things. 
Here's some traps I found:


   - the .each fct bug that iterating only once (work inside all groovy 
   console except into Jenkins project file
   - Editing the pipline script of a pipleine project (when not into from 
   SCM) doesn't show the script box, just right click on the Script tag and 
   inspect content and search for the html tag div 
   class="workflow-editor-wrapper" and uncheck display: none from the CSS 
   property.
   - importing tools file is not funny and I still cannot println() into 
   them so far, the output doesn't display for some reason I don't known.
   - "." path is Jenkins home and not the current workspace, you need to 
   use pwd() to access the workspace
   - printing env options is nearly impossible don't waste your time on 
   this, search for for the available value on google
   - uncheck the sandbox, else you pretty much cannot use anything even 
   basic stuff (regex, string, match)

If anybody want to import a tools file that can be used into multiple 
JenkinsFile or project, here's an example:

My Jenkinsfile stage look like this
stage 'VcxProj Inspection'
GroovyObject bcadTools = (GroovyObject) (new 
GroovyClassLoader(getClass().getClassLoader()).parseClass(new File(pwd() + 
"\\Tools.groovy"))).newInstance();
for(String l : bcadTools.findIntoFiles(true, pwd(), ~/.*\.vcxproj/, 
~/Optimization/))
{
  println(l)
}
// Clean up to avoid CPS problems
errorLines = null
bcadTools = null

My Tools.groovy look like this:
/*!
 * \warning     Copyright (c) 2016 Laboratoires Bodycad inc., All rights 
reserved
 * 
 * \date        2016-07-13
 * \author      Jerome Godbout
 *              
 * \file        Tools.groovy
 * \info        This is common function to use with JenkinsFile or other 
groovy scripts
 *
 */
import static groovy.io.FileType.*
import java.util.regex.Pattern

class BCadTools
{
/* Find pattern into files from src path that will recursively search
* \param isError, print error or warning
* \param path, path to recursively search for file. Ex: "MyPath\\SubDir"
* \param extension, the file extension to check for. Ex: ~/.*\.vcxproj$/
* \param content, the content regex to seek. Ex: /Optimization/
* \return the match list for each files
*/
def findIntoFiles(boolean isError, String path, Pattern fileFilter, Pattern 
lineExp)
{
def rv = []
def srcDir = new File(path)
def levelStr = (isError ? "ERROR" : "WARNING")
srcDir.traverse(type: FILES, nameFilter: fileFilter) 
{
def fileName = it.path
if(!(fileName =~ /[\\\\\/]+\.hg[\\\\\/]+/) &&
  !(fileName =~ /[\\\\\/]+external[\\\\\/]+/))
{
def fileCheck = new File(fileName)
def lineNumber = 0
fileCheck.eachLine 
{
def matches = it =~ lineExp
if(matches.count > 0)
{
rv.push("FI " + levelStr + " : " + fileName + " at line " + lineNumber + " 
: " + it)
}
++lineNumber
}
}
}
return rv
}
}


I should be able to parse the console output to make warning or error with 
this. I have yet figure out if I can split the consle parsed output results 
from each stage into different output, but that's something else.

I hope there's a better way to acheive all this, but it already took me way 
too much time to do this simple thing. Just hope it may help people to 
start with.

Jerome

-- 
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/adf87754-86cb-4ded-8d42-7d258d9f6d7c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to