Azure Pipelines: "##[error]Bash exited with code '1'."
This line is not the error. It is the agent saying that the script of a bash: or script: step ended with an exit status other than 0, which fails the step. The command that failed printed the reason in the lines just above it, and the number in the quotes tells you what kind of failure it was.
What it looks like
/home/vsts/work/_temp/6d0f3a2e-5b1c-4e8a-9f7d-2c4b8a1e0f93.sh: line 3: dotnet-format: command not found ##[error]Bash exited with code '127'.
- Microsoft's page on the command line task:
script:runs with Bash on Linux and macOS and with cmd.exe on Windows. So ascript:step says "Bash exited" on a Linux agent and "Cmd.exe exited" on a Windows one. PowerShell steps say "PowerShell exited". - The Stack Overflow questions with "Bash exited with code" in the title have been opened more than 190,000 times, the most viewed about 123,000.
When the pipeline is a pull request's build validation, the pull request waits on it: Azure DevOps Pull Requests, a plugin for JetBrains IDEs, lists the pull request's branch policies with their state in IntelliJ IDEA, Rider or PyCharm, and with Pro runs a failed build policy again once the script is fixed. Its Pipelines tab (version 1.0.86 and later) lists the runs of the repositories you have open and opens a failed task's log in the editor, free to read.
First, find the line that caused it
- Open the failed step's log and read upward from this line:
npm ERR!,error,not found,Permission denied, a stack trace. In one question the cause wasnpm ERR! missing script: build, abuildscript that package.json did not have. - Bash reports the status of the last command it ran. Put
set -euo pipefailat the top of a multi-line script, so it stops at the first command that fails and the useful message is the last one before this line. - For more detail, run the pipeline again with verbose logs (the variable
system.debugset totrue).
What the number usually means
| Code | Usually | What to do |
|---|---|---|
| 1 | The tool reported a failure: a build error, failed tests, npm or dotnet stopping. Also grep that found no line, when it is the last command. | Read the tool's own message above. For a grep that may find nothing, end with || true or test the result in an if. |
| 5 | pytest: no tests were collected (pytest's exit codes). | Test files named test_*.py or *_test.py and test functions named test_*, run from the right folder. |
| 126 | Found but not executable: Permission denied. | A script committed from Windows has no executable bit. Run git update-index --chmod=+x build.sh and commit, or call it as bash ./build.sh. |
| 127 | command not found. | The tool is not on the agent or not on the PATH of this step. See below. |
| 128 + N | Ended by signal N. 137 is 128 + 9, a SIGKILL, often the out-of-memory killer; 143 is 128 + 15, a SIGTERM. | For 137, use less memory (fewer parallel workers, a smaller heap) or a bigger agent. |
126, 127 and 128 + N come from Bash itself, as the Bash manual describes; every other number is whatever the failing program returned.
"command not found" for a tool that is installed
- Microsoft's page on the Bash task: it runs Bash as a non-interactive, non-login shell, so
~/.bashrcand~/.profileare not read. A PATH set there, typical on a self-hosted agent, is missing. Use the full path, setBASH_ENV(the page showsbashEnvValue: '~/.profile') or add the folder with##vso[task.prependpath]. - On a Microsoft-hosted agent, install the version you need first with a tool step such as
UseNode@1,UsePythonVersion@0orUseDotNet@2. - The name in the message is not a tool but a variable:
MY_TOKEN: command not foundafter$(MY_TOKEN). Microsoft's page on variables: a$(name)with no variable of that name stays as it is, so Bash runs it as a command substitution. Check the name, or for a secret map it withenv:, because secret variables are not mapped into the environment by themselves (the Bash task page). $'\r': command not foundon every line means the script file has Windows line endings. Add*.sh text eol=lfto.gitattributesand commit the file again.
Failing tests should still publish their results
A test runner returns 1 when a test fails, the step fails, and the next steps are skipped, including the one that publishes the results. Keep the failure and run the publishing step anyway. Microsoft's page on conditions: succeededOrFailed() runs even if a previous step failed, unless the run was canceled.
- script: pytest --junitxml=junit/test-results.xml
displayName: Run tests
- task: PublishTestResults@2
condition: succeededOrFailed()
inputs:
testResultsFiles: '**/test-results.xml'
Ending the test command with || true, a common answer, makes the run green with failed tests. If you do it, set failTaskOnFailedTests: true on PublishTestResults@2, which fails the task when the results file has a failed test.
"Bash wrote one or more lines to the standard error stream"
With failOnStderr: true, the Bash task fails if anything is written to standard error, whatever the exit code. git, curl, npm and many other tools write progress and warnings there. Leave it at its default, false, and let the exit code decide. To fail on purpose with your own message, use a logging command:
echo "##vso[task.logissue type=error]The service did not start." exit 1
On Windows: "Cmd.exe exited with code" and "PowerShell exited with code"
- cmd.exe. The command line task page: the inline script is put in a temporary .cmd file, and running another batch file without
callends the script there. npm and tools installed withnpm install -gare batch files, so writecall npm ci. The exit code is the errorlevel of the last command. - PowerShell. Microsoft's page on the PowerShell task: unless
ignoreLASTEXITCODEis true, the task appendsif ((Test-Path -LiteralPath variable:\LASTEXITCODE)) { exit $LASTEXITCODE }, so the exit code of the last external program becomes the step's;errorActionPreferenceisstopby default, so an error in a cmdlet ends the script.
Azure DevOps pull requests in IntelliJ IDEA, Rider and PyCharm
Azure DevOps Pull Requests, a plugin by Lesstep for JetBrains IDEs 2024.1 or later and Android Studio, reads the organization, project and repository from your git remote and lists the pull requests that wait for your review in an IDE tool window, on Azure DevOps Services and on Azure DevOps Server. It shows the branch policies of a pull request with their state, so a required build that has not passed is next to its pull request, and with Pro you run a failed or expired build policy again from there. The review inbox is free; voting, commenting, completing and creating are Pro, with a 30-day trial.
Get it on JetBrains Marketplace
Azure DevOps and Azure Pipelines are trademarks of Microsoft Corporation; IntelliJ IDEA, Rider and PyCharm are trademarks of JetBrains s.r.o. This page is not affiliated with or endorsed by Microsoft or JetBrains.
More: No test result files matching were found · Dotnet command failed with non-zero exit code · Unable to locate executable file · No space left on device · Build validation · 日本語