Skip to content

Commit

Permalink
Remove cli singleton in favor of creating a new instance for proper t…
Browse files Browse the repository at this point in the history
…race propagation
  • Loading branch information
andreiborza committed Jan 9, 2025
1 parent 248798c commit 93f5b5e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
47 changes: 23 additions & 24 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,34 @@ import {version} from '../package.json';
*
* When the `MOCK` environment variable is set, stub out network calls.
*/
let cli: SentryCliReleases;
export const getCLI = (): SentryCliReleases => {
// Set the User-Agent string.
process.env['SENTRY_PIPELINE'] = `github-action-release/${version}`;

if (!cli) {
cli = new SentryCli(null, {
headers: {
// Propagate sentry trace if we have one
...getTraceData(),
},
}).releases;
if (process.env['MOCK']) {
// Set environment variables if they aren't already
for (const variable of [
'SENTRY_AUTH_TOKEN',
'SENTRY_ORG',
'SENTRY_PROJECT',
])
!(variable in process.env) && (process.env[variable] = variable);
const cli = new SentryCli(null, {
headers: {
// Propagate sentry trace if we have one
...getTraceData(),
},
}).releases;

cli.execute = async (
args: string[],
// eslint-disable-next-line @typescript-eslint/no-unused-vars
live: boolean
): Promise<string> => {
return Promise.resolve(args.join(' '));
};
}
if (process.env['MOCK']) {
// Set environment variables if they aren't already
for (const variable of [
'SENTRY_AUTH_TOKEN',
'SENTRY_ORG',
'SENTRY_PROJECT',
])
!(variable in process.env) && (process.env[variable] = variable);

cli.execute = async (
args: string[],
// eslint-disable-next-line @typescript-eslint/no-unused-vars
live: boolean
): Promise<string> => {
return Promise.resolve(args.join(' '));
};
}

return cli;
};
12 changes: 5 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ withTelemetry(
},
async () => {
try {
const cli = getCLI();

// Validate options first so we can fail early.
options.checkEnvironmentVariables();

Expand All @@ -33,7 +31,7 @@ withTelemetry(
const workingDirectory = options.getWorkingDirectory();

core.debug(`Version is ${version}`);
await cli.new(version, {projects});
await getCLI().new(version, {projects});

const currentWorkingDirectory = process.cwd();
if (workingDirectory !== null && workingDirectory.length > 0) {
Expand All @@ -43,7 +41,7 @@ withTelemetry(
if (setCommitsOption !== 'skip') {
await traceStep('set-commits', async () => {
core.debug(`Setting commits with option '${setCommitsOption}'`);
await cli.setCommits(version, {
await getCLI().setCommits(version, {
auto: true,
ignoreMissing,
ignoreEmpty,
Expand All @@ -65,7 +63,7 @@ withTelemetry(
urlPrefix,
stripCommonPrefix,
};
return cli.uploadSourceMaps(version, sourceMapOptions);
return getCLI().uploadSourceMaps(version, sourceMapOptions);
})
);
});
Expand All @@ -74,7 +72,7 @@ withTelemetry(
if (environment) {
await traceStep('add-environment', async () => {
core.debug(`Adding deploy to release`);
await cli.newDeploy(version, {
await getCLI().newDeploy(version, {
env: environment,
...(deployStartedAtOption && {started: deployStartedAtOption}),
});
Expand All @@ -84,7 +82,7 @@ withTelemetry(
if (shouldFinalize) {
await traceStep('finalizing-release', async () => {
core.debug(`Finalizing the release`);
await cli.finalize(version);
await getCLI().finalize(version);
});
}

Expand Down

0 comments on commit 93f5b5e

Please # to comment.