Skip to content

Commit d6d847a

Browse files
fix(cli): Tell self-hosted Android projects to set SENTRY_URL in CI
On self-hosted Sentry, sentry.properties also holds the server URL, so machines without the file need SENTRY_URL along with SENTRY_AUTH_TOKEN. Say so in the warning and the outro line.
1 parent 0255bd1 commit d6d847a

3 files changed

Lines changed: 69 additions & 7 deletions

File tree

‎apps/cli-docs/src/fragments/commands/wizard.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,5 @@ The Android wizard writes the auth token to `sentry.properties` and adds the
8585
file to `.gitignore`. The Sentry Android Gradle plugin uploads during Release
8686
builds, so those builds fail on machines without the file: set
8787
`SENTRY_AUTH_TOKEN` in CI, or create a `sentry.properties` on each machine.
88+
On self-hosted Sentry, the file also holds the server URL, so set `SENTRY_URL`
89+
in CI too.

‎packages/cli/src/lib/wizard/android/android-wizard.ts‎

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import { addManifestSnippet, getMainActivity } from "./manifest.js";
3535

3636
/** Env var that gives the Gradle plugin an auth token where `sentry.properties` has none. */
3737
const AUTH_TOKEN_ENV_VAR = "SENTRY_AUTH_TOKEN";
38+
/** Env var that gives the Gradle plugin a self-hosted server URL where `sentry.properties` has none. */
39+
const URL_ENV_VAR = "SENTRY_URL";
3840

3941
const proguardMappingCliSetupConfig: CliSetupConfig = {
4042
...propertiesCliSetupConfig,
@@ -187,19 +189,22 @@ async function runAndroidWizardWithTelemetry(
187189
)} to ${chalk.cyan("sentry.properties")}.`,
188190
);
189191

192+
const propertiesUrl = selfHosted ? sentryUrl : undefined;
190193
await addSentryCliConfig(
191-
{ authToken, url: selfHosted ? sentryUrl : undefined },
194+
{ authToken, url: propertiesUrl },
192195
proguardMappingCliSetupConfig,
193196
);
194197

195-
// The Gradle plugin's Release upload tasks fail without a token, and the
196-
// file that holds it stays on this machine.
198+
// The Gradle plugin's Release upload tasks fail without a token (and, on
199+
// self-hosted, the server URL), and the file that holds them stays on this
200+
// machine.
197201
const placeholderToken = authToken === DUMMY_AUTH_TOKEN;
198202
Sentry.setTag("auth-token-placeholder", placeholderToken);
199203
clack.log.warn(
200204
authTokenWarning(
201205
placeholderToken,
202206
buildAuthTokenSettingsUrl(selectedProject.organization.slug, sentryUrl),
207+
propertiesUrl,
203208
),
204209
);
205210

@@ -222,8 +227,8 @@ ${chalk.greenBright("Successfully installed the Sentry Android SDK!")}
222227
223228
${chalk.yellow(
224229
placeholderToken
225-
? `Replace the placeholder auth token in ${SENTRY_PROPERTIES_FILE} before building Release, and set ${AUTH_TOKEN_ENV_VAR} for Release builds on other machines and in CI.`
226-
: `Release builds on other machines and in CI need ${AUTH_TOKEN_ENV_VAR}, since ${SENTRY_PROPERTIES_FILE} is gitignored.`,
230+
? `Replace the placeholder auth token in ${SENTRY_PROPERTIES_FILE} before building Release, and set ${ciEnvVars(propertiesUrl)} for Release builds on other machines and in CI.`
231+
: `Release builds on other machines and in CI need ${ciEnvVars(propertiesUrl)}, since ${SENTRY_PROPERTIES_FILE} is gitignored.`,
227232
)}
228233
229234
${chalk.cyan(
@@ -236,22 +241,38 @@ https://docs.sentry.io/platforms/android/
236241
`);
237242
}
238243

244+
/**
245+
* Env vars that Release builds need where `sentry.properties` is missing:
246+
* the auth token, plus the server URL when the file holds one (self-hosted).
247+
*/
248+
function ciEnvVars(serverUrl: string | undefined): string {
249+
return serverUrl
250+
? `${AUTH_TOKEN_ENV_VAR} and ${URL_ENV_VAR}`
251+
: AUTH_TOKEN_ENV_VAR;
252+
}
253+
239254
/**
240255
* Warning that Release builds fail at the Sentry upload tasks wherever
241256
* `sentry.properties` has no real auth token: on every other machine, since
242257
* the file is gitignored, and on this one too when it holds the placeholder.
258+
*
259+
* @param serverUrl - Self-hosted server URL written to the file, if any
243260
*/
244261
function authTokenWarning(
245262
placeholderToken: boolean,
246263
settingsUrl: string,
264+
serverUrl: string | undefined,
247265
): string {
248266
const file = chalk.cyan(SENTRY_PROPERTIES_FILE);
249267
const envVar = chalk.cyan(AUTH_TOKEN_ENV_VAR);
250268
const url = chalk.cyan(settingsUrl);
269+
const setUrl = serverUrl
270+
? `${chalk.cyan(URL_ENV_VAR)} to ${chalk.cyan(serverUrl)}`
271+
: undefined;
251272
if (placeholderToken) {
252273
return `${file} holds a placeholder auth token (${chalk.cyan(
253274
`"${DUMMY_AUTH_TOKEN}"`,
254-
)}), so Release builds on this machine fail at the Sentry upload too. Replace it with a token from:\n${url}\n${file} is also gitignored, so Release builds on other machines fail the same way until they have a token: in CI, set ${envVar}, and teammates can create their own ${file}.`;
275+
)}), so Release builds on this machine fail at the Sentry upload too. Replace it with a token from:\n${url}\n${file} is also gitignored, so Release builds on other machines fail the same way until they have a token: in CI, set ${envVar}${setUrl ? ` and ${setUrl}` : ""}, and teammates can create their own ${file}.`;
255276
}
256-
return `${file} holds your auth token and is gitignored, so other machines, such as a teammate's clone or CI, don't have it. Release builds there fail at the Sentry upload until a token is available: in CI, set ${envVar} to a token from:\n${url}\nTeammates can create their own ${file}.`;
277+
return `${file} holds your auth token${serverUrl ? " and server URL" : ""} and is gitignored, so other machines, such as a teammate's clone or CI, don't have ${serverUrl ? "them" : "it"}. Release builds there fail at the Sentry upload until a token is available: in CI, set ${envVar} to a token from:\n${url}\n${setUrl ? `Also set ${setUrl}.\n` : ""}Teammates can create their own ${file}.`;
257278
}

‎packages/cli/test/lib/wizard/android/android-wizard.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,45 @@ describe("runAndroidWizard", () => {
244244
);
245245
});
246246

247+
it("tells a self-hosted project to set SENTRY_URL in CI too", async () => {
248+
vi.mocked(getOrAskForProjectData).mockResolvedValue(
249+
projectData(true, "https://sentry.example.com/"),
250+
);
251+
252+
await runAndroidWizard(OPTIONS);
253+
254+
const output = stdout();
255+
expect(output).toContain("holds your auth token and server URL");
256+
expect(output).toContain(
257+
"Also set SENTRY_URL to https://sentry.example.com/.",
258+
);
259+
expect(output).toMatch(
260+
/Successfully installed the Sentry Android SDK![\s\S]*Release builds on other machines and in CI need SENTRY_AUTH_TOKEN and SENTRY_URL, since sentry\.properties is gitignored\./,
261+
);
262+
});
263+
264+
it("tells a self-hosted project with the placeholder token to set SENTRY_URL in CI too", async () => {
265+
vi.mocked(getOrAskForProjectData).mockResolvedValue(
266+
projectData(true, "https://sentry.example.com/", DUMMY_AUTH_TOKEN),
267+
);
268+
269+
await runAndroidWizard(OPTIONS);
270+
271+
const output = stdout();
272+
expect(output).toContain(
273+
"in CI, set SENTRY_AUTH_TOKEN and SENTRY_URL to https://sentry.example.com/,",
274+
);
275+
expect(output).toMatch(
276+
/Successfully installed the Sentry Android SDK![\s\S]*set SENTRY_AUTH_TOKEN and SENTRY_URL for Release builds on other machines and in CI\./,
277+
);
278+
});
279+
280+
it("does not mention SENTRY_URL for sentry.io", async () => {
281+
await runAndroidWizard(OPTIONS);
282+
283+
expect(stdout()).not.toContain("SENTRY_URL");
284+
});
285+
247286
it("ends by asking to replace the placeholder token", async () => {
248287
vi.mocked(getOrAskForProjectData).mockResolvedValue(
249288
projectData(false, "https://sentry.io/", DUMMY_AUTH_TOKEN),

0 commit comments

Comments
 (0)