---
title: Read pipeline logs
description: Read Azure DevOps build logs with the SAIF CLI, preserve build context, and compare failing steps with earlier jobs.
---

# Read pipeline logs

Use `saif pipeline logs <build>` to read logs without rerunning a build. Start with the failing step, then inspect earlier jobs in the same run when a later lookup says a resource or artifact does not exist.

## Reading Build Logs from the CLI

| Mode | Input | Context |
| --- | --- | --- |
| Remote | Full build URL | The URL supplies the host, collection, project, and build ID |
| Contextual | Build ID | Repository selector, explicit coordinates, current repository, or interactive selection |

### Remote mode (by URL)

Paste the build URL from the browser:

```powershell
saif pipeline logs "https://dev.azure.com/SAIFCorporation/Customer/_build/results?buildId=162774"
```

Do not combine a URL with `--host`, `--collection`, `--project`, or `--repo`; remote mode already has its coordinates and rejects those context flags.

A URL copied from a stage, job, or step can include `si`, `j`, or `t`. Those identifiers scope the output to that node. Use `--all-steps` to remove the URL-derived scope and inspect the whole build:

```powershell
saif pipeline logs "https://dev.azure.com/SAIFCorporation/Customer/_build/results?buildId=162774" --all-steps
```

For example, if Terraform apply fails, inspect the preceding **Check Terraform Cloud Workspace Existence** step before assuming the workspace is missing. The check logs the resolved workspace name and ID. Compare them with the apply step's target and error; a successful existence check rules out that particular missing-workspace diagnosis, not every later Terraform failure.

### Contextual mode (by id)

From a repository clone, the CLI can infer context from its git remote. Outside a clone, select a repository or provide coordinates:

```powershell
saif pipeline logs 162774
saif pipeline logs 162774 --repo my-service
saif pipeline logs 162774 --host dev.azure.com --collection SAIFCorporation
```

`saif pipeline monitor <build-id>` resolves a bare build ID through the same context resolution, so it also infers the repository from the current clone.

### Build context

Every `saif pipeline logs` run returns one payload with two parts: `Build`, the build's source context, and `Logs`, the matching steps. Every output format renders that same payload. The one exception is `--follow`, which streams, so it renders `Build` once and then streams raw log lines instead of returning a `Logs` collection. `Build` lists each repository resource the build consumed or referenced (the pipeline's `resources.repositories`, typically `self` plus a `templates` repository; resources are not necessarily checked out) with its alias, whether it is primary, repository name, provider (`azure_devops` or `github`), ref, full commit SHA, and a commit URL. `self` is the pipeline's own repository, the one that contains the pipeline definition, and is marked primary. It is usually the application repository, but not always, so check which repository the failing path belongs to. `templates` typically holds shared pipeline templates.

For a pull request build, `Build` also includes a `Pull Request` section with the PR number, title, source and target branches, the PR source commit, the merge commit the build actually ran, and a link to the PR. For a CI build on an Azure Repos repository, the same section shows the completed PR that merged the built commit, when there is one, so the change under test is one hop away. GitHub-hosted CI builds don't get this lookup, because it would need a GitHub token.

The build context renders once with `--follow`, is not affected by `--tail`, and still renders when no steps match the filters. If the CLI cannot fetch the context, it still returns the logs, and the exit code does not change.

When an error points at a file in a built repository, read that file at the listed commit instead of the current `main`, using the repository and full commit SHA from the build context:

- Azure Repos: `az rest --resource 499b84ac-1321-427f-aa17-267ca6975798 --url "https://dev.azure.com/{org}/{project}/_apis/git/repositories/{repo}/items?path={path}&versionDescriptor.version={sha}&versionDescriptor.versionType=commit&%24format=text&api-version=7.1"` (needs `az login`), or, when your session exposes the Azure DevOps MCP `repo_file` tool, that tool with `action: get_content`, `repositoryId: <repo>`, `project: <project>`, `path: <file>`, `version: <full sha>`, and `versionType: Commit`.
- GitHub: `gh api "repos/{owner}/{repo}/contents/{path}?ref={sha}"`, or the GitHub MCP `get_file_contents` tool with `sha`.

For example, if a build fails while reading `infra/auth/ext/app/authorized-apps.yml`, fetch that path from the `self` repository at its listed commit.

### Filtering and output

```powershell
# Only failed steps
saif pipeline logs 162774 --status failed

# Scope to a stage and step
saif pipeline logs 162774 --stage Build --step Compile

# Last 50 lines across matching steps
saif pipeline logs 162774 --tail 50

# Structured output
saif pipeline logs 162774 --format json
```

Filters are case-insensitive and combinable. `--stage`, `--job`, and `--step` accept a name or timeline-record GUID; `--stage` also accepts the stage's YAML identifier. Explicit filters override the corresponding URL-derived scope. `--all-steps` ignores URL scope, not filters you explicitly supplied.

`--format json` returns one object with the build context and the step logs:

```json
{
  "build": {
    "id": "162774",
    "url": "https://dev.azure.com/SAIFCorporation/Customer/_build/results?buildId=162774",
    "repositories": [
      { "alias": "self", "isPrimary": true, "name": "my-service", "provider": "azure_devops", "ref": "refs/heads/main", "commit": "<full sha>", "url": "<commit url>" }
    ],
    "pullRequest": null
  },
  "logs": []
}
```

`logs` holds the same step entries as before and is an empty array when nothing matches. For a PR build, `pullRequest` carries `number`, `title`, `sourceBranch`, `targetBranch`, `sourceCommit`, `mergeCommit`, and `url`; a CI build on Azure Repos fills it with the completed PR that merged the commit. `--format jsonlines` and `--format plain` write this same single payload, as one compact JSON line or one flattened line.

For a running build, `--follow` streams new lines until completion: it renders the build context once, then streams raw lines rather than the single `{ build, logs }` payload. It cannot combine with `--tail` or machine-readable output (`json`, `jsonlines`, or `plain`). A log command can return a failure exit code because the selected build steps failed, even when it successfully retrieved their logs.

## Compare earlier and later jobs

1. Identify the first failing operation: initialization, checkout, restore/build, test, or deployment.
2. If it consumes a resource from an earlier job, read that job's result and recorded identifier.
3. Compare environment, workspace, resource name, and lookup filter. A successful create followed by an empty lookup suggests a lookup or timing problem, not proof that creation never ran.
4. Preserve the build URL and relevant error before changing configuration. Redact tokens and sensitive log values before sharing.

See [Troubleshooting](../troubleshoot/index.md) for symptom-specific articles, and [Pipelines](pipelines.md) for the pipeline and variable contracts.

Command behavior comes from [`PipelineLogsCommand`](https://github.com/saif-corp/forge/blob/main/src/dotnet/SAIF.Platform.CLI/Commands/Pipeline/PipelineLogsCommand.cs). Use `saif pipeline logs --help` for the installed CLI's complete option surface.
