Get environment variable from previous build - batch-file

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.

Related

Script to Replace Text in %PATH% Environment Variable

As of right now when we update Java on a PC which users a certain program we use I have had to manually update the system path environment variables with the latest Java version number in the path (C:\Program Files (x86)\Java\jre1.8.0_141\bin\client).
My goal is to automate this process via a DOS script if possible. I am far from a coder but would like this script to search for the "jre1.8.0_141" then replace it with whatever build number the latest update is in the %PATH% variable. This way I can leave each individual PC's environment variables in tact and only edit the Java string.
I have looked at merging entries with the registry but would like to avoid that as all systems are not identical.
This is my script so far:
This is what I have so far:
REM Exports the Systems Environmental Variables
reg export "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "C:\Software\%COMPUTERNAME%_ENV_VAR_BACKUP.reg" /y
REM Searchs for Java path and changes if necessary
IF "%PATH%" = "*jre1.8.0_141*" (
THEN **NOT SURE WHERE TO GO FROM HERE**
)
I know what I want to accomplish but I have no idea how to translate that into a working batch file so I apologize I have no more information for you.
Thank you for any help.

TeamCity: Change parameter within same build step

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?

jasypt in cmd: cannot find main class

I am using jasypt 1.9.2 in Windows 7 x64 cmd. Here's encrypt.bat content:
ECHO ON
set SCRIPT_NAME=encrypt.bat
set EXECUTABLE_CLASS=org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI
set EXEC_CLASSPATH=.
if "%JASYPT_CLASSPATH%" == "" goto computeclasspath
set EXEC_CLASSPATH=%EXEC_CLASSPATH%;%JASYPT_CLASSPATH%
:computeclasspath
IF "%OS%" == "Windows_NT" setlocal ENABLEDELAYEDEXPANSION
FOR %%c in (%~dp0lib\*.jar) DO set EXEC_CLASSPATH=!EXEC_CLASSPATH!;%%c
IF "%OS%" == "Windows_NT" setlocal DISABLEDELAYEDEXPANSION
set JAVA_EXECUTABLE=java
if "%JAVA_HOME%" == "" goto execute
set JAVA_EXECUTABLE="%JAVA_HOME%\bin\java"
:execute
%JAVA_EXECUTABLE% -classpath %EXEC_CLASSPATH% %EXECUTABLE_CLASS% %SCRIPT_NAME% %*
I got this error(I also tried cd /d [the dir where encrypt.bat is located] and the error persists):
The error message points out that the main class of org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI is no where to be found. I don't understand this. I tried to change JAVA_HOME value in system variables and no luck.
I finally downloaded again the original release and it works. Seems that I messed something up when I was trying to edit the encrypt.bat... I noticed this difference:
With #echo on, I see this output in working version:
C:\Users\myname>FOR %c in (C:\Users\myname\Documents\[APP]\jasypt-1.9.2-dist_new\bin\..\lib\*.jar) DO set EXEC_CLASSPATH=!EXEC_CLASSPATH!;%c
And in my answer I see this:
C:\Users\myname>FOR %c in (C:\Users\myname\Documents\[APP]\jasypt-1.9.2-dist_new\bin\lib\*.jar) DO set EXEC_CLASSPATH=!EXEC_CLASSPATH!;%c
Note the .. before lib.
Also: don't put jaspyt in some path with space! It also cause error, even with quotes.
I also had a same issue when I put unzipped jasypt-1.9.2 folder inside Program Files. You need to move jasypt-1.9.2 folder to C: drive. You should be able to encrypt string without any issues.
I had the same problem with version 1.9.3 . I did the following steps to get it to work:
I cloned the project form its root (https://github.com/jasypt/jasypt.git)
I compiled and packaged the project in /jasypt/ directory using maven (this is the project that is used by the script)
I created the a lib next to bin directory and then I copied the generated jar files into it
then I ran the encrypt.bat again
This way I solved the problem.
call the encrypt.bat/decrypt.bat file instead of the .sh file while you are running from the windows machine.

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

Jenkins always considers a build successful using batch/bat

I just joined a company that uses batch files to build a C++ project. The batch does all sorts of things (updates svn, which is now done by jenkins), creates build folders, deletes unnecessary files after building, copies library files to the build folder, etc.
My problem is Jenkins always considers the build successful, even when it´s not. The .bat file creates a file called errormake.txt when something goes wrong. How do I make jenkins read that and mark the build as a failure?
Also, is there any way I can find out the build folder Jenkins created from inside the .bat file (maybe send a variable when I call the batch file)?
This is the single line I'm currently using to call the .bat file:
call "C:\Source\BuildVersion\AppName\build_version.bat"
Edit: Also, this project is split up into several SVN repositories. %SVN_REVISION% is blank. How can I get the correct %SVN_REVISION% from the first repository (the main one)?
To answer each of your questions -
Jenkins always return "SUCCESS", even when the Job actually failed:
Jenkins sets the status of the Job, based on the return-code of the last command
that ran in each "Execute windows batch command" block.
If your last command is copy some.log D:,
Jenkins thinks everything is OK
(If the 'copy' command went fine...)
Use EXIT xx or EXIT /B xx, depending on your OS,
where 'xx' is some integer greater than zero.
How do I make Jenkins mark the build as a failure, based on a log-file:
Use the Text-finder Plugin, as mentioned by sdmythos_gr .
Is there any way I can find out the build folder Jenkins created:
There are a few parameters that are available as environment-variables
for each script that runs under Jenkins - see here for the list of those parameters:
Jenkins Environment Variables.
To your question:
%WORKSPACE% - The absolute path of the workspace
%BUILD_NUMBER% - The current build number, such as "153"
%BUILD_ID% - The current build id, such as "2005-08-22_23-59-59"
(YYYY-MM-DD_hh-mm-ss)
How can I get the correct %SVN_REVISION% from the first repository:
This answer is from the same link:
%SVN_REVISION% - For Subversion-based projects,
this variable contains the revision number of the module.
If you have more than one module specified, this won't be set.
Hope that helps
Jenkins use the windows error code to know whether a build failed or not.
You should return a value different from 0 when your build failed, with "exit /B 1" for example.
On "newer" versions of Windows (I tested on Server 2012 R2), put the following at the end of each Windows batch command:
#EXIT /b %ERRORLEVEL%
This will pass the error code that the cmd.exe received back to the caller (i.e. Jenkins). The "#" turns off echoing so you don't clutter up your log.
If you have multiple lines in the command and want to stop after the first failure, put the following after each line that you want to check (yes, this is not pretty):
#IF NOT %ERRORLEVEL% == 0 EXIT /b %ERRORLEVEL%
For example:
step1.exe
#IF NOT %ERRORLEVEL% == 0 EXIT /b %ERRORLEVEL%
step2.exe
#IF NOT %ERRORLEVEL% == 0 EXIT /b %ERRORLEVEL%
call "C:\Source\BuildVersion\AppName\build_version.bat"
#EXIT /b %ERRORLEVEL%
I'm also going to answer just part of your question.
There is a Text Finder plugin for Jenkins that you could use.
https://wiki.jenkins-ci.org/display/JENKINS/Text-finder+Plugin
You can mark the build as unstable or failed at the end of the build depending on the contents of a file or the console output.
Maybe this could help...
I know the question is quite older but may be useful to some people. To execute your bat file, instead of using following line,
call "C:\Source\BuildVersion\AppName\build_version.bat"
You can use below format,
<someRelativeOrAbsolutePath>\<.batFileName> <param1> <param2> <and so on>
Executing the command in this way inside Execute Windows Batch Command of Build section of Jenkins will use your last return code of the command. ${BUILD_STATUS} will depend on that. And you will not have to modify your script to return some condition based error codes.
As other users have stated your batch files should use "exit /B 1". Here is a trick to chain together your calls causing Jenkins to return a failure if one fails:
call a.bat &&^
echo "a success" &&^
call b.bat &&^
echo "b success"
"&&" denotes that the next action should only run on success (exit code 0). "^" lets us split the command into multiple lines. The downside to this approach is the build progress bar doesn't display accurately.

Resources