jenkins parameter From Properties file - jenkins

I have 3 Jenkins jobs to be run in serial.
Run a Ant File
Run another ANT File
Run a command line
All the above jobs use a file path which is set in a properties file.
Ex Job 1 , Executes ANT file placed in file path location
Job 2 , Executes another file placed in same file path location
Job 3 , Executes command line to do SVN update in same file path location
I need to parameterize the file path in all three builds from properties file.
Can anyone help me with possible approach?
Thanks In Advance

This answer could be a little high level. You can use Jenkins Pipeline as a code for this approach instead of using 3 freestyle jobs.
You can create 3 stages which performs these 3 steps. Pipeline as a code supports reading of properties from different file types (json, yaml etc.)

Look for the "EnvInject" plugin. This lets you inject properties into your build as environment variables; these assignments survive build step boundaries.
If the property file is checked in, you can load it in the Build Environment section before the build steps start executing. If the property file is generated during the build sequence, you can add a build step between where the property file is created and where it is used.
Once set, if the property file contains "FOO=/path/to/folder" then in configuring Jenkins things you would refer to $FOO or ${FOO} (for example, an Ant build step might specify "${FOO}/build.xml"; in Windows batch script execution FOO shows up as an environment variable and is referenced by %FOO% (i.e., "#echo Some_Useful_Piece_Of_Data > %FOO%\data.txt"
More information can be found here: https://wiki.jenkins.io/display/JENKINS/EnvInject+Plugin

Related

Jenkins promoting: empty classpath entries not allowed

I have a freestyle jenkins job B which will run after the run of job A.
Now I choose:
Promote builds when...
Custom Groovy script
I check Groovy Sandbox and I define a simple groovy script.
When I try to save my job I got this error:
java.net.MalformedURLException: JENKINS-37599: empty classpath entries not allowed
I have to define a class path entry: JAR file path or URL
Definition:
A path or URL to a JAR file. This path should be approved by an
administrator or a user with the RUN_SCRIPT permission, or the script
fails. If the file or files are once approved, they are treated
approved even located in another path.
I really don't know to what file I have to point or what I have to do. Why isn't it just working when I check the sandbox?
This is a jenkins bug. It requires removing that Classpath entry every time before saving the job. I found a workaround, to set the value to any of the existing jars, like https://your-jenkins-host/jnlpJars/slave.jar. This won't affect script execution and won't require you to remember removing that stupid UI block every time you update your jenkins job config.
I had a similar issue and I clicked the red box with the white "X" to close that additional classpath window. I then saved the script.

Jenkins Job DSL - behaviour conditional on file contents

I have inherited a system which uses Jenkins Job DSL to build the jobs for all our projects, I have little experience with configuring Jenkins and none at all with Jenkins Job DSL, so please be gentle.
Some of these projects are Gradle projects. There is a function, createGradleJob() which creates the gradle job. In this function we build the task list for the job, as a string, based upon some features of the project. e.g. if it is being built from the master branch we append the 'publish' task. All of these conditional tasks are currently appended based upon the projects branch name, or the presence , or absence of certain files in the projects repo.
I would like to now add a new task into this task list conditional upon the contents of some of these files. Specifically if certain keywords are detected in the projects build.gradle file then certain tasks need to be appended to the task list.
So, is there a way in Jenkins Job DSL to check the contents of a file and use that as a conditional expression?
I have found that I can execute arbitrary shell commands using the shell function, so I thought I could just grep the file, but I can't locate the documentation for this function, so I'm not clear how I can able to access the output of the commands, so as to use them in a conditional expression.
I have found the textFinder function, but this appears to only allow you to fail (or mark as unstable) the build as a result of finding or failing to find, the text, not use the result as a conditional expression.
It sounds like you want to readFileFromWorkspace. It returns the contents of the file as a string. Simply read your file and parse the string as needed using the Groovy and/or Java string utils.
It's not quite clear from your question, but if you're talking about reading files out of the repo to be checked out by the job you're generating, this function won't help. But if the file is already somewhere in the workspace (i.e. it's one of the files checked out by the seed job), you'll be fine.
The shell command you found adds an "Execute Shell Script" build step to the job being generated. It doesn't actually execute the script there and then, it just copies the contents of the parameter verbatim into the build step ready to be executed when Jenkins runs the job.
For your continued sanity, here is a link to the Job DSL API Documentation

Access file parameter in jenkins

I'm working on a multiconfiguration job(Regression_L1) in Jenkins whose task is to run 2 kinds of tests(test1 and test2). In the multiconfiguration job, it triggers an executor job(Regression_executor) to run script for the selected test. The Regression_L1 job is restricted to run in matrix_service_jobs node, while the matrix jobs are to run in the slave node custom_matrix_service_jobs node. The Regression_executor job is restricted to run in remote machines with a specific label(RL1_Test_Machine).
My goal is to test custom build from developers. And so I added a File Parameter(config - File Location: CUSTOMBUILD/mybuild.zip) for the job. The question is how can I access the uploaded file?
Some important info:
Regression_executor's workspace: /home/regressionexec/
Regression_L1's workspace: /var/work/matrix_service_jobs/Regression_L1
Regression_l1 matrix workspaces: /var/work/workspace_user_matrix/workspace/Regression_L1/TEST_PHASE/test1/label/custom_matrix_service_jobs/ and /var/work/workspace_user_matrix/workspace/Regression_L1/TEST_PHASE/test2/label/custom_matrix_service_jobs/
$JENKINS_HOME: var/work/jenkins_home
I did not know where to find the uploaded files so I did a search ung linux find. The result is:
/var/work/jenkins_home/Regression_L1/TEST_PHASE/test2/label/custom_matrix_service_jobs/builds/${BUILD_NUMBER}/fileParameters/CUSTOMBUILD/mybuild.zip.
How can I copy it to the slave node that executes the test script?
Whatever you enter under "File location", that would be the location and the variable that holds the original filename of the uploaded file.
However, on *nix, neither / nor . are valid variable name characters, so in your case, if "File Location" is CUSTOMBUILD/mybuild.zip system cannot create a variable ${CUSTOMBUILD/mybuild.zip}
The file though is still placed under ${WORKSPACE}/CUSTOMBUILD/mybuild.zip. You can access it with this path too.
You can then use Copy To Slave plugin, to copy the file from master to your slaves
I couldn't find my uploaded file under the WORKSPACE, so I ended using something like
"%JENKINS_HOME%\jobs\%JOB_NAME%\builds\%BUILD_ID%\fileParameters\myUploadedFile"

Set a Jenkins variable after reading console log line / value --OR-- using the variables used by scripts / commands what Jenkins calls

I DONT need the following:
How to set a Jenkins env variable or
How to use a environment variables in Jenkins / windows shell / ant / etc scripts.
What I need is opposite of that.
Summary:
1. I have a Jenkins job: ABC_Build
2. This job calls a .bat file (which calls an ANT code / target for packaging / building a
build). As we are creating a build, this job know what's the new build label name and
ANT is storing it in a variable called "new.build.label". File used is build.xml.
(A NOTE to novice users: If you want to call many Windows commands (.bat / .cmd or
commands which creates a windows shell) then, you should call it using "call
script.bat -Dparam1 -Dparam2...." way).
Now, this job calls an another .bat file (which calls an ANT code /target) and uses one
of the parameter value which gets generated by first .bat file / ANT package target
call (i.e. "new.build.label"). As this is a separate .bat command call to call a new
session of ANT code/target, I need to pass the value of "new.build.label" during the
call of this step. File used here is deploy.xml.
Basically, I'm trying to see how can I set a variable in Jenkins, either by using:
a. reading the console output of my Jenkins job as I'm echoing the value of
new build label in the standard output / console output.
b. any other way, where I can set a jenkins variable using "new.build.label" ANT
variable (once first .bat / ANT package target is finished) and I'm ready to call
the 2nd .bat / .cmd / ANT call for doing deployment. Unfortunately, I can't do both
package / deploy at the same time.
I'm also not interested in knowing WHY CAN'T I call target deploy from first ANT
session when I already know the value of "new.build.label" as my main request is:
HOW TO set a jenkins variable using a "variable" which was used by one of the scripts (ANT/Jelly/Groovy/Maven/etc) that Jenkins called.
You can pass environment variables among Jenkins build steps via EnvInject plugin. In your particular case the following is probably the best way:
The first ANT should echo new.build.label into a properties file that can be read by EnvInject plugin, e.g.:
<echo message="new.build.label=${new.build.label}" file="envars.props" />
Create an Inject environment variables build step and set "Properties File Path" to envars.props (make sure you are dealing with paths correctly). Then new.build.label will be available as an environment variable to the rest of your build steps.
By the way, I think it is not a good practice to call ANT from batch files in Jenkins. Use ANT build step instead.

Hudson/Jenkins PMD Configuration

I am new to Jenkins and just started configuring it. This is what i have done till now:
Installed and configured Jenkins to display the home page. Added PMD plugin.
Set the HUDSON_HOME to a specific directory > C:\Work\Jenkins
Configured a test build to run a simple do-nothing ant script. It runs successfully
Written an independent pmdbuild.xml to run checks on a set of files in C:\myview (I am using clearcase). This xml also copies the output pmd_results.xml to the workspace directory in $HUDSON_HOME/[job-name]/workspace
Now I added the pmdbuild.xml as a step in my primary build. So my build has 2 steps:
a. Run a simple script, do-nothing.
b. Run pmdbuild.xml which generate pmd_results.xml and place it in $HUDSON_HOME/[job-name]/workspace (HARD-CODED as Jenkins PMD plugin expects the file there)
Jenkins picks up the pmd_results.xml automatically with the plugin and displays warnings and everything.
Now the problem:
If I click on a filename in the PMD results, it gives a filenotfound exception as it is looking for the source file in $HUDSON_HOME/[job-name]/workspace.
My java code files are placed in C:\myview (a clearcase snapshot view)
My question is, do I need all my code files to be present inside $HUDSON_HOME/[job-name]/workspace ?? Meaning can't I tell Jenkins to look for the PMD input files in C:\myview or any other directory instead of $HUDSON_HOME/[job-name]/workspace ??
Sorry for the extremely long description.
Jenkins expects that all the code is in the workspace. Usually Jenkins is used to check out a copy of the code into the workspace, and then runs all build steps on the Sources in the Workspace.
Might seem restraining at first, but it saves you a lot of trouble if you need to move Jenkins to another server, or create a slave instance.
So I would suggest you let Jenkins check out your code (there should be a clearcase plugin) into the workspace, and run the analysis on the checked out code.
If there are compelling reasons why your code has to stay where it is (C:\myview in your case) you can still set the workspace of your build to that directory (find this in the job configuration page, you need to click on the 'extended' button to see the option).

Resources