Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions apps/cli-docs/src/content/docs/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,9 @@ Or run directly without installing:
### OAuth Device Flow (Recommended)

On a fresh installation, the install script starts OAuth login automatically
when running in an interactive terminal with no detected AI agent or existing
credentials. Upgrades and non-interactive installations skip this first-login step.
when running in an interactive terminal outside CI with no detected AI agent or
existing credentials. Upgrades, CI, and non-interactive installations skip this
first-login step; in CI, set `SENTRY_AUTH_TOKEN` instead.

The easiest way to authenticate is via OAuth device flow:

Expand Down
26 changes: 23 additions & 3 deletions packages/cli/install
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ GHCR_REPO="ghcr.io/v2/getsentry/toolkit"
# --retry-all-errors (7.71).
curl_retry=(--retry 3 --retry-connrefused --retry-max-time 60)

# Environment variables that CI providers set: CI_ENV_VARS in src/lib/ci.ts,
# which the installer test runs this script with one by one. CI jobs often run
# in a PTY, so the terminal checks in run_setup alone would let setup log in.
ci_env_vars=(CI JENKINS_URL HUDSON_URL TEAMCITY_VERSION CIRCLECI CIRCLE_BUILD_URL
bamboo_resultsUrl GITHUB_ACTIONS GITLAB_CI TRAVIS_JOB_ID BITRISE_BUILD_URL
GO_SERVER_URL TF_BUILD BUILDKITE BITBUCKET_BUILD_NUMBER)

# Where there is no standalone binary, the npm package runs on Node.js.
npm_fallback="Install Node.js 20 or later and run: npm install -g sentry"

Expand Down Expand Up @@ -690,6 +697,19 @@ fetch_ghcr_blob() {
# Step 5: hand off to the binary
# ---------------------------------------------------------------------------

# Succeeds when a CI marker is set to anything but "", "false", or "0", as
# isCi() in src/lib/ci.ts does.
in_ci() {
local name
for name in "${ci_env_vars[@]}"; do
case "${!name:-}" in
"" | false | 0) ;;
*) return 0 ;;
esac
done
return 1
}

# `sentry cli setup --install` places the binary and sets up PATH,
# completions, agent skills, and the welcome message. --channel makes later
# `sentry cli upgrade` calls track the same channel.
Expand All @@ -704,9 +724,9 @@ run_setup() {
if $no_agent_skills; then args+=(--no-agent-skills); fi

# curl | bash leaves stdin on the script pipe. Give setup the controlling
# terminal only when both output streams are interactive; setup checks
# whether this is a fresh human install before starting login.
if [[ -t 1 && -t 2 ]] && { exec 3</dev/tty; } 2>/dev/null; then
# terminal only outside CI and when both output streams are interactive;
# setup checks whether this is a fresh human install before starting login.
if ! in_ci && [[ -t 1 && -t 2 ]] && { exec 3</dev/tty; } 2>/dev/null; then
"$binary" cli setup "${args[@]}" <&3 3<&-
exec 3<&-
else
Expand Down
22 changes: 16 additions & 6 deletions packages/cli/src/commands/cli/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { completeAgentHandoff } from "../../lib/agent-handoff.js";
import { installAgentSkills } from "../../lib/agent-skills.js";
import { sentry } from "../../lib/app-identity.js";
import { assertAutoLoginHostTrusted } from "../../lib/auto-auth.js";
import { isCi } from "../../lib/ci.js";
import {
determineInstallDir,
getBinaryFilename,
Expand Down Expand Up @@ -909,25 +910,34 @@ export const setupCommand = buildCommand({
});
}

// CI jobs often run in a PTY, so the TTY checks alone would start a
// device login there that waits for its code to expire.
const ci = isCi(getEnv());
if (
!freshInstall ||
flags.method !== "curl" ||
flags.quiet ||
flags["ensure-auth-scopes"] ||
Boolean(handoffCode || setupCode) ||
!(interactivePromptsAllowed() && isatty(0) && isatty(1) && isatty(2))
!interactivePromptsAllowed() ||
!(ci || (isatty(0) && isatty(1) && isatty(2)))
) {
return;
}

await bestEffort(
"Authentication",
async () => {
if (
getAuthConfig() ||
detectAgent() ||
(await detectAgentFromProcessTree())
) {
if (getAuthConfig()) {
return;
}
if (ci) {
log.info(
"Skipped login in CI. Set SENTRY_AUTH_TOKEN to authenticate.",
);
return;
}
if (detectAgent() || (await detectAgentFromProcessTree())) {
return;
}
assertAutoLoginHostTrusted();
Expand Down
9 changes: 7 additions & 2 deletions packages/cli/src/lib/ci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

import { normalizeSha, parseRemoteUrl } from "./git.js";

/** Environment variables commonly set by CI providers. */
const CI_ENV_VARS = [
/**
* Environment variables commonly set by CI providers. The installer
* (`packages/cli/install`) keeps a copy, and its test checks each one.
*/
export const CI_ENV_VARS = [
"CI",
"JENKINS_URL",
"HUDSON_URL",
"TEAMCITY_VERSION",
"CIRCLECI",
"CIRCLE_BUILD_URL",
"bamboo_resultsUrl",
"GITHUB_ACTIONS",
Expand All @@ -17,6 +21,7 @@ const CI_ENV_VARS = [
"GO_SERVER_URL",
"TF_BUILD",
"BUILDKITE",
"BITBUCKET_BUILD_NUMBER",
];

/**
Expand Down
39 changes: 39 additions & 0 deletions packages/cli/test/commands/cli/setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1472,6 +1472,45 @@ describe("sentry cli setup", () => {
expect(fixture.context.process.exitCode).toBe(0);
});

const ciHint = "Set SENTRY_AUTH_TOKEN to authenticate.";

// CI jobs often run in a PTY, so a terminal alone does not mean a person.
test.each([{ CI: "true" }, { BUILDKITE: "true" }])(
"skips login in CI with a terminal and prints a hint: %j",
async (env) => {
const fixture = createInstall({ env });
await fixture.run();
expect(interactiveLogin.runInteractiveLogin).not.toHaveBeenCalled();
expect(fixture.getOutput()).toContain(ciHint);
expect(fixture.context.process.exitCode).toBe(0);
},
);

test("prints the CI hint without a terminal", async () => {
vi.mocked(isatty).mockReturnValue(false);
const fixture = createInstall({ env: { CI: "true" } });
await fixture.run();
expect(interactiveLogin.runInteractiveLogin).not.toHaveBeenCalled();
expect(fixture.getOutput()).toContain(ciHint);
});

test("offers login in a terminal when CI is false", async () => {
const fixture = createInstall({ env: { CI: "false" } });
await fixture.run();
expect(interactiveLogin.runInteractiveLogin).toHaveBeenCalledOnce();
expect(fixture.getOutput()).not.toContain(ciHint);
});

test("prints no CI hint when SENTRY_AUTH_TOKEN is set", async () => {
const fixture = createInstall({
env: { CI: "true", SENTRY_AUTH_TOKEN: "existing-token" },
});
await fixture.run();
expect(interactiveLogin.runInteractiveLogin).not.toHaveBeenCalled();
expect(fixture.getOutput()).not.toContain(ciHint);
expect(fixture.context.process.exitCode).toBe(0);
});

test("skips an agent identified by the environment", async () => {
const fixture = createInstall({ env: { AI_AGENT: "claude" } });
await fixture.run();
Expand Down
29 changes: 29 additions & 0 deletions packages/cli/test/lib/install-script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
type ServedAsset,
toolkitDownload,
} from "./install-scenarios.js";
import { CI_ENV_VARS } from "../../src/lib/ci.js";
import { at } from "../helpers.js";

const installScript = join(import.meta.dirname, "..", "..", "install");
Expand Down Expand Up @@ -866,6 +867,34 @@ exec /usr/bin/gunzip "$@"
expect(recorded("post-args")).toEqual([]);
});

// CI jobs often run in a PTY, where setup must not start a device login.
test.each(CI_ENV_VARS)(
"keeps the terminal from setup when %s is set",
(name) => {
env[name] = "true";
const result = runInTerminal();
expect(result.status, result.stdout + result.stderr).toBe(0);
expect(recorded("setup-tty")).toEqual([
"0:false",
"1:true",
"2:true",
"controlling:true",
]);
},
);

test("connects setup to the terminal when CI is false", () => {
env["CI"] = "false";
const result = runInTerminal();
expect(result.status, result.stdout + result.stderr).toBe(0);
expect(recorded("setup-tty")).toEqual([
"0:true",
"1:true",
"2:true",
"controlling:true",
]);
});

test("runs init after setup, which keeps the terminal, when SENTRY_INIT is set", () => {
env["SENTRY_INIT"] = "1";
env["SENTRY_TEST_POST_EXIT"] = "7";
Expand Down
Loading