diff --git a/apps/cli-docs/src/content/docs/getting-started.mdx b/apps/cli-docs/src/content/docs/getting-started.mdx index 9e0606f00..fc94a3600 100644 --- a/apps/cli-docs/src/content/docs/getting-started.mdx +++ b/apps/cli-docs/src/content/docs/getting-started.mdx @@ -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: diff --git a/packages/cli/install b/packages/cli/install index f8db5bf11..6708951bb 100755 --- a/packages/cli/install +++ b/packages/cli/install @@ -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" @@ -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. @@ -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/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/null; then "$binary" cli setup "${args[@]}" <&3 3<&- exec 3<&- else diff --git a/packages/cli/src/commands/cli/setup.ts b/packages/cli/src/commands/cli/setup.ts index a863ea6d9..1f731a276 100644 --- a/packages/cli/src/commands/cli/setup.ts +++ b/packages/cli/src/commands/cli/setup.ts @@ -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, @@ -909,13 +910,17 @@ 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; } @@ -923,11 +928,16 @@ export const setupCommand = buildCommand({ 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(); diff --git a/packages/cli/src/lib/ci.ts b/packages/cli/src/lib/ci.ts index d49f4481b..048723e79 100644 --- a/packages/cli/src/lib/ci.ts +++ b/packages/cli/src/lib/ci.ts @@ -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", @@ -17,6 +21,7 @@ const CI_ENV_VARS = [ "GO_SERVER_URL", "TF_BUILD", "BUILDKITE", + "BITBUCKET_BUILD_NUMBER", ]; /** diff --git a/packages/cli/test/commands/cli/setup.test.ts b/packages/cli/test/commands/cli/setup.test.ts index 929c9e836..a4be89e28 100644 --- a/packages/cli/test/commands/cli/setup.test.ts +++ b/packages/cli/test/commands/cli/setup.test.ts @@ -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(); diff --git a/packages/cli/test/lib/install-script.test.ts b/packages/cli/test/lib/install-script.test.ts index fbea20883..5130aa5e4 100644 --- a/packages/cli/test/lib/install-script.test.ts +++ b/packages/cli/test/lib/install-script.test.ts @@ -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"); @@ -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";