How do I return a specific exit code from NANT? - nant

I have a nant script and I see that even if my exec fails, the exit code of NANT is 0 anyway. How do I change NANT exit code?

The exec task has an attribute called "resultproperty", which is there to get the exit code.

Returning a Specific Exit Code from NANT
If the build succeeds NANT will exit with code 0, if the build fails NANT with code 1 and you can use <fail> to force this. NANT gives no other way of controlling the exit code however you could hack it:
<script language="C#">
<code>
<![CDATA[
public static void ScriptMain(Project project)
{
System.Environment.Exit(3);
}
]]>
</code>
</script>
This will end nant immediately, so perhaps you might want to create a target to be run at the very end by putting it into nant.onsuccess.
Why NANT Exit Code is 0 when <exec> Failed?
<exec> fails when the command exits with anything but 0. Normally this causes the whole build to fail in consequence and NANT to exit with code 1, with two exceptions (see 2 and 3). This gives us three possible explanations:
Your command fails but exits with 0 anyway (this is a relatively common behavior). Set the resultproperty attribute and check the property.
failonerror="False" attribute has been set on <exec> or on a higher level (<nant> or <call>). Check the NANT output to see in which order targets are being called or search for failonerror=.
The <exec> is executed as part of target started via the nant.onsuccess resp. nant.onfailure property hook. Check if and where by searching for nant.on.
To say anything more a sample NANT script or perhaps a log file would be useful.

Related

DevOps Powershell step calls bat file successfully but returns 1

I'm calling a bat-file from a step in my yaml file. The bat is located locally on the PC where the build agent is.
The bat is called successfully. It changes the PATH-variable and runs a program afterwards. The program runs successfully however when finished the build step is still marked as unsuccessful because
[error]PowerShell exited with code '1'.
As background information as to why I'm using a bat-file instead of ps1: I'm migrating our current build and it runs over a central bat-file. I don't want to change it.
Is there an environment variable I can change or anything else I can do to ensure the powershell returns 0 instead of 1?
Have you considered changing the batch file exit code?
Exit codes for batch files
Use the command EXIT /B %ERRORLEVEL% at the end of the batch file to
return the error codes from the batch file
EXIT /B at the end of the batch file will stop execution of a batch file.
use EXIT /B < exitcodes > at the end of the batch file to return custom return codes.
Environment variable %ERRORLEVEL% contains the latest errorlevel in the batch file,which is the latest error codes from the last command executed. To know about Environment variable see the below note.
Note: Environment variables are a set of dynamic named values that can
affect the way, running processes will behave on a computer. For
example, an environment variable with a standard name can store the
location that a particular computer system uses to store user profile
this may vary from one computer system to another.
In the batch file , it is always a good practice to use environment
variables instead of constant values. Since the same variable get
expanded to different values on different computers.
Example:
Batch file for Copying File to a Folder
md "C:manageengine"
copy "\\sharename\foldername\samplefile.txt" "C:\manageengine"
exit /b %ERRORLEVEL%
https://www.manageengine.com/products/desktop-central/returning-error-code-on-scripts-how-to.html
EDIT: Some more thoughts about the problem- The batch file probably returned 1 as an exit code because one of the commands or programs used within it returned some kind of error code (or at least didn't return 0 exit code).
for summing up, your question was not very clear mainly because you didn't separate all the factors and variable withing your problem, thus not knowing where the problem is.
Force changing the exit code isn't the right way to fix your problem.

Task exited with exit code null

I am running into a problem of a task exiting with a null exit code. And with this exit code, I noted that I can't access files on node to check the stderr and stdout files. What could be the problem? Also, what does a null exit code mean and how can I set the exit code to be not nullin case of failure?
Thanks!
You will want to check the task's failureInfo field within the executionInfo property.
There is a difference between task failure and application logic failure for the process (command to execute) that is executed under the task. A task failure can be a multitude of things such as a resource file for a task failing to download. A process failing to launch properly for some reason is also a task failure. However, if the process does launch and execute, but the process itself "fails" (as per application logic) and returns a non-zero exit code and no other issues are encountered with the task, this task will have the proper exit code saved. Thus, if a task completes with a null exit code, you will need to consult the failureInfo field as per above along with any stdout/stderr logs if they exist.

How can I get an install4j installer to fail based on a Windows batch command error level

I have a windows batch cmd *.cmd file that runs sql, as part of my installer. This script returns a proper non-zero return code when the sql fails. I can't get my install4j installer to recognize the return code.
I tried two things:
I tried simply setting the failure strategy in the Run batch file action to Quit on Failures, this didn't work.
I tried setting the value of the return code in a variable (sqlReturnCode) and checking that variable in a subsequent action, that didn't work.
When I checked the installation log I'm seeing the SQL errors from stderr redirected to the log, but no matter if it succeeds or fails I see this:
[INFO] com.install4j.runtime.beans.actions.misc.RunExecutableAction [ID 72]: Variable changed: sqlReturnCode=0[class java.lang.Integer]
It's always setting that variable to zero. I DO NOT have the "Show Console Window" option checked. Is this a known bug? How can I fix/work around it?
This ended up being a problem with the cmd script being executed. The ERRORLEVEL enivonrmental variable was being set but "exit /b %ERRORLEVEL%" was not being called.
Install4j relies on the proper error level being set with the exit command.

Phing exec command to set environment variable

I'm trying to set an environment variable in a build script with phing.
This is normally done command line like this:
export MY_VAR=value
In Phing I did the following but it isn't working.
<exec command="export MY_VAR=value" />
I see that this is quite an old question, but I don't think it has been answered in the best way. If you wish to export a shell variable, for example say you are running phpunit from phing and want to do an export before invoking phpunit, try:
<exec command="export MY_VAR=value ; /path/to/phpunit" />
Simply do the export and invoke your command inside the same exec tag. Separate the export statement and the shell executable with a semicolon as shown. Your script will be able to access the value using the standard php function:
$myVar = getenv('MY_VAR');
Bold claim: There is no way to set/export a (Unix) shell variable in PHP so that it is visible inside the scope that started the php script.
php myfile.php (does putenv or shell_exec('export foo=bar');)
echo $foo
Will return nothing.
As PHP can not do it so neither can phing.
Accessing shell environment variables accross multiple script runs (if its that what you want) seems also like an unideal design decision, pretty stateful.
Apart from that I'd urge you to stick to phing and learn its lean lesson. Phing helps stateless thinking to some degree.
I'd never heard of phing before, but this looks very promising as a build tool. Thanks for posting! I looked through the doc on phing.info, I found the following possibility:
#0 I would like to clarify one point. Are you saying that
prompt$ > export MY_VAR=value
prompt$ > phing build.xml
doesn't set MY_VAR to value so it is visible inside the running phing processes? I'd be surprised, but I would understand if this is not how you want to run your build script.
#1 I think in the context of a build tool, a feature like exec is meant to run a stand-alone program, so, while the exec may run and set MY_VAR, this is all happening in a subprocess that disappears immediately as the exec finishes and continues processing the next task in the build.xml.
If you're just trying to ensure that your phing script runs with specific values for env_vars, you could try
Command-line arguments:
....
-D<property>=<value>
// Set the property to the specified value to be used in the buildfile
So presumably, you can do
phing -DMY_VAR=value build.xml
#2 did you consider using a properites file?
See http://www.phing.info/docs/guide/stable/chapters/appendixes/AppendixF-FileFormats.html
and scroll down for info on build.properties
#3 also ...
Phing Built-In Properties
Property Contents
env.* Environment variables, extracted from $_SERVER.
you would access them with something like
${env.MY_VAR}
#4 This looks closer to what you really want
<replacetokens>
<token key="BC_PATH" value="${top.builddir}/"/>
<token key="BC_PATH_USER" value="${top.builddir}/testsite/user/${lang}/"/>
</replacetokens>
I hope this helps.

MSBuild in a Powershell Script - How do I know if the build succeeded?

I am writting a build script with Powershell. The scripts performs various operations such as getting the latest source code off the SVN, backups, etc., and builds the solution using MSBuild:
cmd /c C:\WINDOWS\Microsoft.NET\Framework\v3.5\msbuild.exe "C:\Dev\Path\MyProjct.sln" /p:Configuration=Release
After this instruction, I only want to execute the rest of the script if the compilation succeeded. How can I check this?
The project is a web project, so it's not so easy to check for an output, but I would guess some variables would contain the compilation result. Also since I call msbuild with cmd /c, am I going to be able to access these variables?
Check the value of $LastExitCode right after the call to MSBUILD. If it is 0, then MSBUILD succeeded, otherwise it failed.
BTW there is no need to use cmd /c. Just call MSBUILD.exe directly. We do that in PowerShell build scripts all the time.
To just check for success/failure, use the automatic variable $?.
PS> help about_Automatic_Variables
$?
Contains the execution status of the last operation. It contains
TRUE if the last operation succeeded and FALSE if it failed.
for example:
msbuild
if (! $?) { throw "msbuild failed" }