PowerShell Script tasks in Bamboo will not follow the script's return code

お困りですか?

アトラシアン コミュニティをご利用ください。

コミュニティに質問

プラットフォームについて: Server および Data Center のみ。この記事は、Server および Data Center プラットフォームのアトラシアン製品にのみ適用されます。

サーバー*製品のサポートは 2024 年 2 月 15 日に終了しました。サーバー製品を利用している場合は、アトラシアンのサーバー製品のサポート終了のお知らせページにて移行オプションをご確認ください。

*Fisheye および Crucible は除く

From Bamboo 10.2.2, script tasks using the PowerShell interpreter can benefit from exit code error handling through task options. Check the following feature request for details.

  • BAM-21162 - Getting issue details... STATUS

要約

Running PowerShell scripts in Bamboo will always exit with 0, even if the invoked script returns a non-zero value

環境

  • Any Bamboo version
  • Powershell Script tasks (Builds & Deployments)

診断

When running a PowerShell script task that invokes an Invoke-Command cmdlet, the Script task will always succeed in Bamboo, even if the script clearly fails and returns a non-zero exit code.

原因

Bamboo will create a PS1 script with the commands assigned to the Script Task and run them. The script task will be executed on the Agent as:

powershell -NonInteractive -ExecutionPolicy bypass -Command C:\temporary\location\of\the\script.ps1]

As the invoked PS1 script content may invoke a second script via Powershell's Invoke-Command cmdlet, that will result in the exit code not being passed to the script's main shell, thus the Script task will always exit with success unless forced. That's by PowerShell's design and can't be changed, but there are workarounds that can be implemented.

ソリューション

Choosing the code that best suits you will be up to each customer, check the examples below for some ideas on how to force the $LastExitCode to be passed to the main PowerShell session. For example:

Invoke-Command -ScriptBlock {
  Command123 --options --setting 456
}

If ($LastExitCode -ne 0) { 
  Exit $LastExitCode 
}

他の例

$ExitCode = Invoke-Command -ScriptBlock { 
  Command123 --options --setting 456
  Return $LastExitCode
}
Exit $ExitCode

Please check the following public links for more ideas:


最終更新日: 2025 年 2 月 5 日

この内容はお役に立ちましたか?

はい
いいえ
この記事についてのフィードバックを送信する
Powered by Confluence and Scroll Viewport.