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

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.

Related

Unable to access environment variable in Bamboo Build Plan

I have a Bamboo Build Plan, with the following set of tasks.
Source Code Checkout
Artifactory Generic Resolve (To Get the zip file from Artifactory)
Script (To Extract the contents of zip file and to set to CATALINA_HOME & PATH environment variable)
Ant (For Build)
Task 3 has the following content in it:
APP_HOME=${bamboo.build.working.directory}
unzip $APP_HOME/tomcat/apache-tomcat-6.0.45-windows-x64.zip
export CATALINA_HOME=$APP_HOME/tomcat/apache-tomcat-6.0.45
export PATH="$PATH:$CATALINA_HOME/bin"
But when I execute 4th Task (Ant), the Build is not considering the CATALINA_HOME & PATH variable which is set as part of Task 3. What is wrong here? Why am I not able to access the environment variable that is set in Task 3?
Every Script Task runs in its own non-interactive shell, eventually invoked through the ExternalProcessBuilder. Existing environment variables are made available to the process (i.e. shell), as well as the additional environment variables defined in the task itself as documented. However, newly exported variables are not carried over to the next task as it is an entirely new, isolated shell.
What you could do is to dump the export statements to a file, and 'source' that file at the start of the next script task.

jenkins parameter From Properties file

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

How to declare a global variable in Jenkins and use it in an MSBuild task within each individual project

I am converting our CI platform from CruiseControl to Jenkins, and can't seem to figure something out that seems like it should be relatively simple to do (Disclaimer - I'm no CI or build automation expert, but this was dumped into my lap and I find it interesting)
In CruiseControl, I am able to declare variables like this:
<cb:define rootdir="J:\SOURCES\" />
<cb:define logdir="J:\SOURCES\buildlogs" />
<cb:define iisdir="J:\IIS\" />
<cb:define artifacts="artifacts\" />
Then use them as part of an MSBuild task
<msbuild>
<executable>C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe</executable>
<workingDirectory>$(rootdir)$(ProjectName)</workingDirectory>
<projectFile>$(ProjectName).sln</projectFile>
<buildArgs>/p:BuildDate="1";OutDir="$(iisdir)$(ProjectName)\bin\\";WebProjectOutputDir="$(iisdir)$(ProjectName)\\"</buildArgs>
<targets>Rebuild;$(ProjectName)</targets>
<timeout>180</timeout>
<logger>C:\Program Files (x86)\CruiseControl.NET\server\ThoughtWorks.CruiseControl.MsBuild.dll</logger>
</msbuild>
If the root or IIS directories change, it can easily be applied to all projects at once. We have ~60 projects setup, so doing this project by project would be very time consuming. Migrating this to Jenkins, the MSBuild command line arguments now look like this (partial sample but includes what is relevant):
OutDir="J:\IIS\ProjectName\bin\\";WebProjectOutputDir="J:\IIS\ProjectName\\"
The IIS directory is hard coded. I need that to be something more like this:
OutDir="${IIS_DIR}\ProjectName\bin\\";WebProjectOutputDir="${ITEM_ROOTDIR}\ProjectName\\"
Is there a way to do that? I tried the configuration slicing plugin, which is useful, but doesn't fit this need from what I see
You can do this with built-in Jenkins' functionality:
Then you need to expand your variable. This, actually, depends on where you would use it.
For example: %MSBuild% and %IIS_DIR% for "Execute windows batch command" build step. Other build steps (and plugins) may use it differently.
For global variables, you need EnvInject plugin. This allows you (among other things) to setup variables at the Global (node) level, at job level or as a step. You can set variables directly, or from properties file, or from scripts.
Once set, the variables are available as environment variables to the rest of Jenkins and its steps (within scope).
For passing arguments to MSBuild, when you configure an MSBuild step, there is an option to pass "Command line arguments" in the format /p:Param=Value.
The "value" could be an environment variable. On Windows environment you would reference it as %myvar%
So, if you configure a global GLOBAL_IIS_DIR=C:\path\to\IIS using EnvInject, you can then reference it on command line with /p:IIS_DIR=%GLOBAL_IIS_DIR%

Retrieval of Jenkins environment variables while invoking ANT

I am invoking a windows batch command from Jenkins, after i get the latest version of my project from SVN. the windows batch command just performs certain file copying, after the all the files are retrieved from SVN and runs an ANT build. In the ANT build process, i am generating a JSP file where i have tried to capture the in the following fashion.
%BUILD_TAG%-%BUILD_NUMBER%-%BUILD_ID%-%SVN_REVISION%
Unfortunately none of this information is understood by the build process and it just writes %BUILD_TAG%-%BUILD_NUMBER%-%BUILD_ID%-%SVN_REVISION% into the file.
Could you please let me know if there is a way to capture these information into a file in the way i am trying to do? if not, could you direct me on how these information could be captured into a JSP file during the process that we are following?
BUILD_TAG, SVN_REVISION, etc are all environment variables that are present during a Jenkins build, and to use them in Ant, you would use them as any other environment variable from Ant
First, add a line:
<property environment="env"/>
Then you can reference any environment variable with this prefix, like:
${env.VAR_NAME}
So in your case, you'd do:
${env.BUILD_TAG}-${env.BUILD_NUMBER}-${env.BUILD_ID}-${env.SVN_REVISION}

How do i pass parameters from Jenkins to Ant scripts?

For some GUI testing I'm creating a Jenkins task for each GUI module to be tested.
Once created I'm using Ant to build these tests, but I'm not aware of how to actually pass parameters from Jenkins to Ant build file? Basically how do I do variable substitution in Ant?
I'm using the Sahi framework to test GUI components, so the flow goes like this...
Jenkins → Ant build script → Sahi file to execute
Can anyone please take a look at it?
"Using ant -Dname=value lets you define values for properties on the Ant command line." http://ant.apache.org/faq.html#passing-cli-args
To use a jenkins parameter as a variable when you call any use ${variablename}
https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Build
Click Advance under configure section of your job in jenkins and use "Properties" section to pass parameter value to Ant script.
e.g
jenkins.param=10
ant.prop=$jenkins.param where jenkins.param is the parameter defined in the jenkins job .
Now in your ant build script ,you can get the value by using ${ant.prop}.
From Jenkins to SAHI Pro via ANT.
In the ant target that you call from Jenkins, give the following within sahi tag.
<customfield key="variable_name" value=" variable _value"/>
Now, such values from Jenkins will be available in SAHI Pro through the ant target. To retrieve them in SAHI, you should set them in “CUSTOM_FIELDS” of testrunner file.
For example:
SET CUSTOM_FIELDS= -variable_name jenkinsToSahiVariable
Where -variable_name should be same key that you set in ant target. And second string will contain the value you set from Jenkins. To get this in a sahi file, use sahiSuite API like following.
$jenkinsValues = _suiteInfo();
$sahiVariable = $ jenkinsValues ["jenkinsToSahiVariable"];

Resources