Is there a way to read a string (version numbers) from a file into jenkins variable?

10,507 views
Skip to first unread message

Pan Luo

unread,
Apr 5, 2012, 3:04:21 AM4/5/12
to jenkins...@googlegroups.com
I have my version number stored in a file. I would like my jenkins job to read the version from that file and store it in a variable so that I can used it some publish task. Is there a way to do it? Thanks.

Jan Seidel

unread,
Apr 5, 2012, 4:10:45 AM4/5/12
to Jenkins Users
Hi,

you could try the Jenkins Environment File Plugin
https://wiki.jenkins-ci.org/display/JENKINS/Envfile+Plugin

Else you would have to run some string manipulation to get the value
into a file and execute it at each build step like you would assign an
environment variable.
In DOS it would be a content like: SET var_name=var_value.
You have to execute it at each build step which needs the value as the
variable is destroyed when the build step is done.

Take care
Jan

Pan Luo

unread,
Apr 5, 2012, 4:46:02 PM4/5/12
to jenkins...@googlegroups.com
Thanks Jan. The Envfile plugin doesn't work for me as it requires the
java property format. My version file is just 3 digits in the plain
text file.

I could go with the second option. I need the version number in
post-build action, where I'm sending the files to a remote server
using scp. I need the version number included in the remote path. Is
there a way I can run any script in post-build actions?

By build steps, you mean pre-build, build, post-build? So if I set any
environment variables using Ant (or Phing) in build step, it will not
exist in post-build actions? Thanks.

Cheers,
Pan

shanz

unread,
Apr 5, 2012, 7:12:06 PM4/5/12
to Jenkins Users
You could create another file with the version number in the java
property (key=value) format.
Then you can use the EnvInject plugin to inject the environment
variable (MY_NEW_VAR) into your job/build.
The advantage of this approach is that the variable can be visible
throughout the whole job/build.
You just enter the path/myVersion.txt in the Properties File field of
the plugin.

Alternatively just use DOS, eg: To place the first line of a file into
a variable, do this
Set /P MyVar=<aFilename>
The downside to this later approach is that you may have to repeat it
if you open several shells.

shanz

unread,
Apr 13, 2012, 9:52:56 AM4/13/12
to Jenkins Users
REM create a file to hold environment variables for all subsequent
labelled build jobs
del C:\hudson\jobs\shareableCustomWorkspace\labelledJobsEnvVars.txt
echo APPLICATION_BINARY_VERSION=%APPLICATION_BINARY_VERSION% > C:
\hudson\jobs\shareableCustomWorkspace\labelledJobsEnvVars.txt
echo BUILD_STRING=%BUILD_STRING% >> C:\hudson\jobs
\shareableCustomWorkspace\labelledJobsEnvVars.txt
echo SVN_TAG_DATE=%SVN_TAG_DATE% >> C:\hudson\jobs
\shareableCustomWorkspace\labelledJobsEnvVars.txt
echo SVN_TAG=IQ4 WinCE OS v%BUILD_STRING% %SVN_TAG_DATE% >> C:\hudson
\jobs\shareableCustomWorkspace\labelledJobsEnvVars.txt

Then in any subsequent jobs ...
Tick "Inject environment variables to the build process"
Properties File Path
C:\hudson\jobs\shareableCustomWorkspace\labelledJobsEnvVars.txt

then just use the variables as normal.

That's it!
Reply all
Reply to author
Forward
0 new messages