Read file from Jenkins workspace with System groovy script - groovy

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.

Related

Python ImportError when executing through windows scheduler

I did some searches for this topic and found some prior threads, but I did not understand any of them as I am still a total beginner in Python.
I have a Python script which has some long string variables stored in various .py files in a sub-directory. I'm importing the .py files from that sub-directory when I run the script. There is a __init__.py file in the sub-directory. The only reason I'm using this setup is that the long string variables which I'm storing in those other files would make the code very difficult to read as they are SQL strings and can span 50-100 lines each.
Everything works perfectly when I run this script through PyCharm.
However, when I run the script through Windows Scheduler or a batch file, I get an ImportError for all of the .py files in the sub-directory. The problem is definitely related to the python script not knowing where to look for those .py files when it's run through Windows Scheduler. But I'm not sure how to fix it.
The action for the scheduler task is to run the python exe
D:\Python35\python.exe
with the argument as the script
D:\python\tableaudatasourcebuilds\dcitechnicalperformance\dcitechnicalperformance0.py
So the full action looks like:
D:\Python35\python.exe "D:\python\tableaudatasourcebuilds\dcitechnicalperformance\dcitechnicalperformance0.py"
The subdirectory which stores the long string variables .py files is:
D:\python\tableaudatasourcebuilds\dcitechnicalperformance\dcitechnicalperformance0\
The imports look like:
from dcitechnicalperformance.dcitechnicalperformance0.dciquer import nzsqldciwk
Does anyone know how to address this problem? Any help is much appreciated.
Good afternoon,
First of all i don't know how much sense there is to store long SQL querys on a module, I'm not by any means an expert, but something like a JSON file (or hell, even store them in a table inside the sql) seems like a better approach.
About your problem I think it resides on the current directory where the task is launched, let me explain:
In PyCharm when you run the code it launches from the location of the file, and with so, it's able to find the directory with the module.
With the scheduled task it may be launching in another directory and so, it's unable to find the module as the directory is not present.
If you decide to stick with your reproach a plausible solution would be to create a .bat file that browses to the project location:
#ECHO OFF
D:
cd D:\python\tableaudatasourcebuilds\dcitechnicalperformance\
D:\Python35\python.exe dcitechnicalperformance0.py
And that should work.

Jenkins/Hudson Groovy Script Console: Flexibility

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.

Append content from file using Email Ext Jenkins plugin

I have been modifying the default groovy template that the Email Ext plugin supplies.
Firstly, I had to modify the JUnitTestResult and need to format it accordingly to my need. I found in the it.JUnitTestResult, it is a reference to the ScriptContentBuildWrapper class. And then I was able to format the JUnitTestResult according to my need.
Now I am facing a second difficulty:
Along with those contents, I need to append more content from a file that resides in the job workspace. How to access the files that reside in the workspace directory.
I would be interested to know how I can access the build context object. Whats the java class name and things like that.
Just use build which returns an AbstractBuild
Try -
build.workspace
Which returns the FilePath of the directory where the build is being built.
See AbstractBuild.getWorkspace.
Tip: in Groovy, you can avoid the "get" and use field-like access notation.
Depending on which version of email-ext you are using, you can use the tokens provided to get access to things, so if you look at the token help, you'll see lots of tokens. These can be used in the groovy templates to do the same thing. For instance, the FILE token can be used in the Groovy by doing FILE(path: 'path/to/file') and it will replace with the contents of the file (only works on files that are below the workspace).
The build object is not available directly in all groovy scripts (e.g. groovy build script, groovy system build script, groovy post-build script, groovy script as evaluated in email-ext). The most portable way of obtaining build object in groovy script for a running build is:
import hudson.model.*
def build = Thread.currentThread().executable
Then you can get workspace and access files inside like this:
workspace = build.getEnvVars()["WORKSPACE"]
afilename = workspace + "/myfile"
afile = new File(afilename);
// afile.write "write new file"
// afile << "append to file"
// def lines = afile.readLines()

get path to groovy source file at runtime

Given the following directory structure:
/home/some/random/foler/myScript.grooy
... how can I programmatically obtain the path to myScript.grooy parent directory right in the script itself?
Ultimately I'm trying to read in several files from the same directory the script is in.
EDIT: trying to run it on Windows 7, Groovy 2.0.1, groovy console
Well, the solution is in Java's File class:
println new File(".").absolutePath
If you want to obtain every groovy script in the same directory, maybe you could use some of other facilities in the Groovy JDK, like eachFile:
def files = []
new File(".").eachFile {
if (it.name.endsWith(".groovy") ) files << it
}
println files
If you want the running script name, well, you've got a problem (https://issues.apache.org/jira/browse/GROOVY-1642)
Accordingly to that JIRA, this is the current workaround (which doesn't always work):
URL scriptUrl = getClass().classLoader.resourceLoader
.loadGroovySource(getClass().name)

How to get specific information about the current build project in Jenkins with Groovy?

In Jenkins/Hudson, with the help of a Postbuild Groovy script, I would like to get one of the following:
an environment variable (e.g. current JOB_NAME, BUILD_NUMBER etc.)
the result of a specific build number of the current project
the build number of the last not successful build in the current project
At the moment I only found the following way, but it's rather limited:
def item = hudson.model.Hudson.instance.getItem("GroovyMultipleFailTest")
def build = item.getLastBuild()
build.getNumber()
Using Jenkins v2.17 this works for me:
echo "BUILD_NUMBER=${env.BUILD_NUMBER}"
${manager.build.getEnvironment(manager.listener)['BUILD_NUMBER'] }
Bo Persson had the best answer, but was a little short.
To access the environment variables from the build in the Groovy Postbuild, you can grab them from the build. This sample code is useful for dumping all of the BUILD's environment variables to the console:
manager.build.getEnvironment(manager.listener).each {
manager.listener.logger.println(it);
}
If you're using Groovy script within "Env Inject", you can get current build and current job by:
currentJob.getName()
currentBuild.toString()
an environment variable (e.g. current JOB_NAME, BUILD_NUMBER etc.)
String jobName = System.getenv('JOB_NAME')
The only way I got it to work for me was with build.properties.environment.BUILD_NUMBER
I tried various approaches in this article and others, and it looks like the only one put below and also build.properties.environment.BUILD_NUMBER (upvoted) are working for me in Jenkins Execute system Groovy Script build step groovy command type
EnvVars envVars = build.getEnvironment(listener)
def n = envVars.get('BUILD_NUMBER')

Resources