Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins powershell plugin always builds successfully

I'm using Jenkins PowerShell plugin to build a project.

However, I found that Jenkins always considers my build successful no matter what I type inside Windows PowerShell command.

Here's an example:

enter image description here

As you can see, asdf isn't a legal command. Jenkins should give me FAILURE after the build.

But the console output gives me:

Started by user admin
Building in workspace C:\Users\Administrator\.jenkins\jobs\Test\workspace
[workspace] $ powershell.exe -NonInteractive -ExecutionPolicy ByPass "& 'C:\Users\ADMINI~1\AppData\Local\Temp\hudson2092642221832331776.ps1'"
The term 'asdf' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\ADMINI~1\AppData\Local\Temp\hudson2092642221832331776.ps1:1 char:5
+ asdf <<<< 
    + CategoryInfo          : ObjectNotFound: (asdf:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Finished: SUCCESS

I think the execution result of PowerShell should depend on $lastexitcode.

Is this a bug of PowerShell plugin?

like image 802
Brian Avatar asked Dec 18 '15 05:12

Brian


3 Answers

As of 1.3, the plugin will not handle exceptions such as those from missing commands. You can do this yourself with try/catch:

try
{
    asdf
}
catch
{
    write-host "Caught an exception"
    exit 1
}

See MSDN for more.

like image 164
Chris Nelson Avatar answered Oct 10 '22 22:10

Chris Nelson


For me, I wanted the script to stop and fail in Jenkins soon as it hit an error. This was accomplished by adding this to the start of the script:

$ErrorActionPreference = "Stop"

This is discussed here: [How to stop a PowerShell script on the first error?][1]

[1]: How to stop a PowerShell script on the first error?. ..................

like image 9
Robert Patterson Avatar answered Oct 10 '22 22:10

Robert Patterson


Per the latest version of the plugin (Version 1.3 Sept 18 2015), you must use $LastExitCode to fail a build.

Version 1.3 (Sept 18 2015)

  • PowerShell now runs in Non-Interactive mode to prevent interactive prompts from hanging the build
  • PowerShell now runs with ExcecutionPolicy set to "Bypass" to avoid execution policy issues
  • Scripts now exit with $LastExitCode, causing non-zero exit codes to mark a build as failed
  • Added help and list of available environment variables (including English and French translations)
like image 5
rrirower Avatar answered Oct 10 '22 22:10

rrirower