microsoft / azure-pipelines-tasks

Tasks for Azure Pipelines
https://aka.ms/tfbuild
MIT License
3.49k stars 2.61k forks source link

[BUG]: AzurePowerShell@5 should fail if $LASTEXITCODE is nonzero #19951

Open mikeharder opened 4 months ago

mikeharder commented 4 months ago

New issue checklist

Task name

AzurePowerShell

Task version

5.239.10

Issue Description

AzurePowerShell@5 should fail if $LASTEXITCODE is nonzero. PowerShell@2 handles this correctly.

Environment type (Please select at least one enviroment where you face this issue)

Azure DevOps Server type

dev.azure.com (formerly visualstudio.com)

Azure DevOps Server Version (if applicable)

No response

Operation system

Ubuntu 22.04

Relevant log output

*** PowerShell@2: Works Correctly ***
/usr/bin/pwsh -NoLogo -NoProfile -NonInteractive -Command . '/home/vsts/work/_temp/884b6b32-7d9b-4e7c-8fb3-c253bc5fabf4.ps1'
LASTEXITCODE: 19
##[debug]$LASTEXITCODE: 19

##[debug]Exit code 1 received from tool '/usr/bin/pwsh'
##[debug]STDIO streams have closed for tool '/usr/bin/pwsh'
##[debug]task result: Failed
##[error]PowerShell exited with code '1'.
##[debug]Processed: ##vso[task.issue type=error;source=TaskInternal;]PowerShell exited with code '1'.
##[debug]Processed: ##vso[task.complete result=Failed;]PowerShell exited with code '1'.
Finishing: PowerShell

*** AzurePowerShell@5: Task succeeds despite nonzero $LASTEXITCODE ***
/usr/bin/pwsh -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command . '/home/vsts/work/_temp/90fab5af-3bf1-442a-ae59-91e46bcf6422.ps1'
...
LASTEXITCODE: 19
##[debug]Agent environment resources - Disk: / Available 21297.00 MB out of 74244.00 MB, Memory: Used 828.00 MB out of 6921.00 MB, CPU: Usage 34.24%
##[debug]Exit code 0 received from tool '/usr/bin/pwsh'
...
##[debug]Exit code 0 received from tool '/usr/bin/pwsh'
##[debug]STDIO streams have closed for tool '/usr/bin/pwsh'
Finishing: AzurePowerShell

Full task logs with system.debug enabled

No response

Repro steps

pool:
  name: Azure Pipelines
  vmImage: ubuntu-22.04

variables:
  system.debug: true

steps:
- task: PowerShell@2
  inputs:
    targetType: inline
    script: 'node -e "process.exit(19);"; write-output "LASTEXITCODE: $LASTEXITCODE";'
  continueOnError: true

- task: AzurePowerShell@5
  inputs:
    azurePowerShellVersion: LatestVersion
    azureSubscription: Redacted
    ScriptType: InlineScript
    Inline: 'node -e "process.exit(19);"; write-output "LASTEXITCODE: $LASTEXITCODE";'
  continueOnError: true

Workaround

As a workaround, you can add exit $LASTEXITCODE; to the end of the inline script:

- task: AzurePowerShell@5
  inputs:
    azurePowerShellVersion: LatestVersion
    azureSubscription: Redacted
    ScriptType: InlineScript
    Inline: 'node -e "process.exit(19);"; write-output "LASTEXITCODE: $LASTEXITCODE"; exit $LASTEXITCODE;'
  continueOnError: true

But I still think this is a bug in AzurePowerShell, for two reasons:

I still think this is a bug in AzurePowerShell, because:

  1. This shouldn't be necessary
  2. The regular PowerShell task doesn't require it
v-schhabra commented 4 months ago

Hi @mikeharder, Thanks for reporting the issue. Could you please share the debug logs of the pipeline by adding variable system.debug to "true"?

mikeharder commented 4 months ago

@v-schhabra: I'm not comfortable sharing full debug logs due to potentially sensitive information. I have provided a minimal pipeline you can use to reproduce the problem, and I have already shared the relevant debug logs.

danieljurek commented 3 months ago

The AzureCLI@2 task has a specific affordance for this scenario (powerShellIgnoreLASTEXITCODE) which is similar to behavior in PowerShell@2 (ignoreLASTEXITCODE)

Given this, it looks like AzurePowerShell@5 differs from other similar tasks.