问题
I am starting to use Jenkins, which is a really great tool. We are using parameterized build, we define parameter such as branch name ${Branch} (e.g. dev, release, main etc).
In the build config, I can add a windows batch command, is there a way I can pass down these parameters to the batch command?
I tried to pass like "%${Branch}%" or "%Branch%", but seems not working.
Can anyone help?
Many Thanks
回答1:
Using Parameterized Build, you need to define parameters. The value of these will be prompted to you when you click "Build" link.
The name of the parameters should be a plain name, preferably without spaces, like Branch
. Do not add any ${}
or %%
to definition of the parameter name.
In the build steps, like Execute Windows Batch Command, you can reference the parameter with regular batch syntax, like %Branch%
.
If you would be on a *nix machine, you would use Execute shell build step and reference the parameter with regular bash syntax, like ${Branch}
Note that even when running on Windows, a lot of Jenkins plugins themselves take the parameters in *nix syntax, however the Execute Windows Batch Command will be batch-like, i.e. %Branch%
.
So, you can try typing:echo %Branch%
I also suggest putting just set
command on a line by itself, and it will show you all environment variables available to you during the build process, which is quite useful.
回答2:
I do not really get exactly what you mean branches because I am a bit new, but this worked for me if you are talking about how to do like yourcmd -a and have it run different code then the original and -b. In the dir, create two files with your parameter names eg -a.bat -b.bat You do not need to write any code in those. In the code for your command eg createfolder.bat Write
@echo off
if %~n1 == -a goto :aran
if %~n1 == -b goto :bran
:aran
Your first parameter code
cls
:bran
Your 2nd parameter code
cls
Hope it helped! You can even add up to 50 parameters!
来源:https://stackoverflow.com/questions/24429976/pass-parameter-from-jenkins-parameterized-build-to-windows-batch-command