Change an environment variable in between Jenkins build steps - batch-file

I have a Jenkins job with a string parameter called MyPath, a groovy script build step and then a batch command build step.
If I kicked off a job so that the value of MyPath is "C:\Temp\", I want the groovy script to change the value of MyPath to be "C:\Temp\3.4\setup.exe" and then I want to use the batch file to execute MyPath.
I know I can launch a process from groovy and that I can perform my groovy logic in batch, it would just be nice if I could pass variables between build steps.
To use a parameter in groovy you have to use the library to resolve it (below). Is there a method that can set it?
def path = build.buildVariableResolver.resolve("MyPath");
Something like this would be perfect if it existed:
buildVariableChanger.change("MyPath") = "C:\3.4\setup.exe"
I have also looked into the EnvInject plugin but from what I understand it does not support what I want to do.
Thanks.

I don't think it is possible since field value for StringParameterValue.class is final: http://javadoc.jenkins-ci.org/hudson/model/StringParameterValue.html
The solution may be creating other parameter in groovy script
import hudson.model.StringParameterValue
import hudson.model.ParametersAction
def newPath = build.buildVariableResolver.resolve("MyPath") + "3.4\\setup.exe"
build.addAction(new ParametersAction (new StringParameterValue ('path2', newPath)))
and then use parameter path2 in batch command

Why don't you simply pass a variable to your batch? You could start the batch from your first script instead of starting it with an own build step. Just call it with yourscript.bat C:\3.4\setup.exe. You can access the argument with %1. If you insert the line %1 into your bat file it will execute C:\3.4\setup.exe.

One option should be to use the SETX Windows command to set the value of user (or system) environment variable.
If you set this variable in your Groovy step, then the batch step should be able to use it.

Related

Batch file running Python script

What I would like to do is create a batch file that will temporarily add a environment variable to the Python executable. From there call the executable and open another script from the batch file. I'm very new to batch and have been researching this, so far I've found that to add a environment variable I would need something like:
set env="path/to/exe"
And to call a script I would need to use:
call "path/to/script"
My question being, if I where to combine the two two and then call the script, would I be able to do something like this:
set env="python.exe"
call "python script.py"
Will this work like I would expect it to?
Well the CALL is used for calling (opening) batch files. If you wanted to open a python script through batch (and set the environment) you would have use the STARTcommand and code it like this:
set env=python.exe
start python script.py
This should work (combined) if you have the Python environment.

Install4j running Executable not using environment variables

I am trying to run a groovy script using Install4j. I have laid down the files for groovy and added to an environment variable "Groovy_Home". Then I added that to the Path Environment Variable. When I open the command window I am able to run "Groovy buildenv.groovy". It works! Almost magically! However when I use Install4j to create the .bat file that has a working dir of the .groovy file and runs this script:
#echo on
groovy buildenv.groovy > buildenv.output.log
It doesn't seem to work! it complains that it has no idea where the groovy is at. I have added the "Specific environment variables" to "PATH=${PATH}". But that doesn't seem to work either... Any help is greatly appreciated.
Check if the "Include parent environment variables" property of the "Run executable or batch file" action is selected.
If yes, select the "Show console window" property and its "Keep console window" child property and add
SET
to your batch file so you can check the environment variables.
The solution for me was to use Specific environment variables as you told, but not setting PATH=${PATH} (what it does if path is already set?).
Instead in the installer process I add a step of Directory selection type to let user choose the directory where groovy is installed, and use the user entry to set a installer variable. Then I use this variable in the Specific environment variables property of the Run executable or batch file action to set something like PATH=${installer:userGroovyHome}, where userGroovyHome is the Variable name for selection property of Directory selection

.PIF With ? Allowed for a Variable Request

Up until now I have been using the .PIF shortcut with "?" to call for a variable that is then used in a batch file to produce specific results. We have over project 10,000 folders, and the JobFind.PIF tool really satisfied a quick search. It is like a moving or floating Shortcut.LNK to any one folder in the larger directory.
Program Line Call Inside JobFind.PIF
S:\YoursTruly\JobFind\JobFind.bat ?
JobFind.bat Contents Where %1 = ?
explorer "P:\SDIT_L~1\Projects\000030%1"
Is there a simple replacement for my olde fashion JobFind.pif tool?
Thank you,
GPB
You could replace it with either a command-line or GUI VBScript. Here's an example:
strJob = InputBox("Enter the job number:")
With CreateObject("WScript.Shell")
.Run "explorer.exe P:\SDIT_L~1\Projects\000030" & strJob
End With

Start another batch file with multiple parameters with the start command?

Is it possible to start another batch file and pass along multiple parameters with spaces, using the start command?
Here is how my program currently works:
main program starts > sees its outdated > calls updater (data1.exe) > updater copies new version over > It tries to delete the old version, but it can't. The old version is still marked as being used, from when it called the updater.
That's why the call command won't work. Do I need to use start then? How would that work?
This was the original line of code... the one that calls the updater and passes the variables along:
call "%dirofbatch%data1.exe" "%downloc%" "%dirofbatch%" "%lver%" "%lget%"
I'm stumped.
EDIT: I should mention that "data1.exe" is just an exe'd batch file.
How to read parameters in a batch file:
caller batch
start "" "%dirofbatch%data1.exe" "%downloc%" "%dirofbatch%" "%lver%" "%lget%"
called batch
set "parm1=%~1"
set "parm2=%~2"
set "parm3=%~3"
set "parm4=%~4"
echo %parm1% %parm2% %parm3% %parm4%

Write a batch script to edit text in a .cs file

I wonder if anyone knows how to write a batch script to edit some text in a .cs file.
What I want to do is change "AssemblyVersion("1.0.0.0")" "AssemblyVersion("1.0.0.x")" where x++ for every time the job in jenkins is being built.
Best Regards Jan
Do you want to use only a batch script for this? You could also use Execute Groovy Script option and write some simple groovy script to achieve this
file = new File("folder/path/myfile.cs")
fileText = file.text;
fileText = fileText.replaceAll(srcExp, replaceText);
file.write(fileText);
You can also use the availabe environment variables from your jenkins job to construct your replace text. These variables will be present at /env-vars.html
Stay away from "batch-file automation" - will only cause you grief.
(for a starter, different versions of Windows support a different set of batch-commands)
You should incorporate the build-number in the script as an Environment Variable -
use either the "built-in" %BUILD_NUMBER% parameter or set your own format with
the Formatted Version-Number Plugin .
If you do need to edit that 'CS' file, I suggest using either Perl or PowerShell.
Cheers

Resources