TeamCity: Change parameter within same build step - batch-file

I know that to change a parameter in a .cmd batch script, we can use:
echo "##teamcity[setParameter name='paramName' value='abcdef']"
But the change takes effect only in the next build step after this has been set in the previous build step. I want to set and change a parameter within the same build step. Is it somehow possible?

Related

Get environment variable from previous build

I'm using the EnvInject Jenkins plugin to set an environment variable during a build step in our deployment pipeline. The next project (according to the order) is a manual step to deploy.I need to access an environment variable set in the build step.
According to the console output I'm doing that properly:
[EnvInject] - Executing scripts and injecting environment variables after the SCM step.
[EnvInject] - Injecting as environment variables the properties content
FOR=%%i IN ("%NewFile%") DO ( SET FileName=%%~ni )
FILE_NAME=%FileName%
[EnvInject] - Variables injected successfully.
However in my next step, where I need to access the $FILE_NAME variable in order to specify the correct folder to scp from I get the following output:
[SCP] No file(s) found: releases\$FILE_NAME\*
Is there a setting I'm missing or am I attempting to access the variable incorrectly?
I see this option below - "Inject Environment variable" - however this is the same step I used previously to create the $FILE_NAME variable. If this is the correct step... how do I use it to get the variable? :)
EDIT
By clicking the options below...
...I'm able to get the following console output when the project builds.
[EnvInject] - Loading node environment variables.
[EnvInject] - Preparing an environment for the build.
[EnvInject] - Keeping Jenkins system variables.
[EnvInject] - Keeping Jenkins build variables.
[EnvInject] - Injecting contributions.
[EnvInject] - Unset unresolved 'USERNAME' variable.
However I'm still unable to access $FILE_NAME. Below is the option I've selected to set the variable in the previous build step.
And here is the script that goes in the "Properties Content":
FOR /F "delims=|" %%I IN ('DIR "C:\Jenkins\workspace\Project\releases\*.nupkg" /B /O:D') DO SET NewFile=%%I
FOR %%i IN ("%NewFile%") DO ( SET FileName=%%~ni )
FILE_NAME = %FileName%
In a previous build step I look for the most recent file name. In that step I simply output the current version of the file I'm working with like so:
#echo FILE_NAME=%FileName%>CurrentVersion.txt
Then I add an "Inject Environment Variables" build step and point to the output file. This adds the environment variable as I was hoping for.

Change an environment variable in between Jenkins build steps

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.

Batch file invoked file details

I invoke a batch file from my own extension file type.In batch file i will show the details of invoking file. I know by passing parameters when invokation we will pass it and we will get by "%~sn1" OR "%1" OR "%~nx1". But i need without passing parameters.
Sample Example
My batch file code looks like this(main.bat)
#ECHO on
set modelname=(here i want help)
java -Djava.library.path="C:\Program Files\Internet Explorer" -Xms1024m -Xmx1024m -jar dist/XYZ.jar -models %modelname%
exit
If i click "Kitchen.xyz" then it'll invoke my batch file "main.bat". Now i want set "modelname" as "Kitchen.xyz". If i click "LivingRoom.xyz" ,:modelname" set as "LivingRoom.xyz".
Can anyone help please...
Thanks
You will need to change the Windows Registry associated with the file type of .xyz
[HKEY_CLASSES_ROOT\.xyz\shell]
[HKEY_CLASSES_ROOT\.xyz\shell\Name Of Your Command]
#="&Name Of Your Command"
[HKEY_CLASSES_ROOT\.xyz\shell\Name Of Your Command\command]
#="\"C:\\Path\\To\\Your\\Batch\\File\\main.bat\" \"%1\""
Save that text in a .reg file and execute it to apply the registry changes. Then when you right click .xyz files, "Name Of Your Command" will be one of the options and possibly the default option which would run when double clicked.
Then your main.bat still needs to accept one parameter and assign it to modelname.
You could also do that programatically with the REG command, but regardless of which method you use to set things up, something needs to be run to setup each machine you want to do this on.

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%

Run batch file in the background

I have a batch file, this batch file will not start automatically, it will only run when i double click on it.
Can I run the batch file in the background when I double click on it.
Well, you can start it minimized with start, if that is enough. Really hiding it is difficult (although I can think of an option right now).
Basically you need to determine whether the batch has been started by double-clicking it. You can do this by defining a special variable and look for it:
#echo off
if not defined FOO (
set FOO=1
start /min "" %~0
exit /b
)
rem here whatever you wanted to do originally in the batch
As long as the FOO variable isn't defined (which is probably the default almost everywhere), this batch will launch itself minimized again, but with the variable defined first. Environments are passed to subprocesses, which is why this works.
you would generally need something else to run the script in that manor
i.e.
Create a shortcut, and set the “Run” field for the shortcut to “Minimized’.
Once you click or tab away from the cmd.exe window that the batch file is running it, it's "in the background" -- I'm not really sure what you want but it sounds like you might be asking how to run the batch file without displaying the cmd.exe window.
If so I can think of two ways: first, you can create a shortcut to the batch file, right click it, and in the properties there set the shortcut to run minimized (should be a drop down option next to Run).
You can also wrap invocation of the batch file in a VBScript file using Windows Script Host's shell object (calling the Run method) to run the batch file invisibly. Passing 0 as the intWindowStyle parameter will suppress display of a window or anything.
#Ghyath Serhal
I have used cmdow to do this on another program, it is an external application that can be used to modify the command prompt. To use it, you will need to either enter this code (see below) into it's own batch file, or into the command prompt, where it will run 'BatchFile.bat' with a hidden terminal window. I haven't found a way to use this in a single batch file, but I only found out about this today.
cmdow /run /hid 'BatchFile.bat'
Hope this helps.

Resources