Skip to content

Commit 6555b6f

Browse files
committed
fix(utils): prevent nested github actions log groups when run within nx target
1 parent 068984b commit 6555b6f

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

packages/utils/src/lib/logger.int.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ describe('Logger', () => {
5151
vi.stubEnv('CI', 'false');
5252
vi.stubEnv('GITHUB_ACTIONS', 'false');
5353
vi.stubEnv('GITLAB_CI', 'false');
54+
vi.stubEnv('NX_TASK_TARGET_TARGET', '');
5455
});
5556

5657
afterAll(() => {
@@ -247,6 +248,28 @@ ${ansis.cyan('└')} ${ansis.green(`Total line coverage is ${ansis.bold('82%')}`
247248
└ ESLint reported 4 errors and 11 warnings (1.23 s)
248249
::endgroup::
249250
251+
`);
252+
});
253+
254+
it('should NOT use native GitHub Actions log groups if run within Nx target', async () => {
255+
vi.stubEnv('CI', 'true');
256+
vi.stubEnv('GITHUB_ACTIONS', 'true');
257+
vi.stubEnv('NX_TASK_TARGET_TARGET', 'code-pushup');
258+
performanceNowSpy.mockReturnValueOnce(0).mockReturnValueOnce(1234); // group duration: 1.23 s
259+
const logger = new Logger();
260+
261+
await logger.group('Running plugin "ESLint"', async () => {
262+
logger.info('$ npx eslint . --format=json');
263+
logger.warn('Skipping unknown rule "deprecation/deprecation"');
264+
return 'ESLint reported 4 errors and 11 warnings';
265+
});
266+
267+
expect(ansis.strip(stdout)).toBe(`
268+
❯ Running plugin "ESLint"
269+
│ $ npx eslint . --format=json
270+
│ Skipping unknown rule "deprecation/deprecation"
271+
└ ESLint reported 4 errors and 11 warnings (1.23 s)
272+
250273
`);
251274
});
252275

packages/utils/src/lib/logger.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { formatDuration, indentLines, transformLines } from './formatting.js';
1010
import { settlePromise } from './promises.js';
1111

1212
type GroupColor = Extract<AnsiColors, 'cyan' | 'magenta'>;
13-
type CiPlatform = 'GitHub Actions' | 'GitLab CI/CD';
13+
type CiPlatform = 'GitHub' | 'GitLab';
1414

1515
/** Additional options for log methods */
1616
export type LogOptions = {
@@ -43,9 +43,9 @@ export class Logger {
4343
#isVerbose = isEnvVarEnabled('CP_VERBOSE');
4444
#isCI = isEnvVarEnabled('CI');
4545
#ciPlatform: CiPlatform | undefined = isEnvVarEnabled('GITHUB_ACTIONS')
46-
? 'GitHub Actions'
46+
? 'GitHub'
4747
: isEnvVarEnabled('GITLAB_CI')
48-
? 'GitLab CI/CD'
48+
? 'GitLab'
4949
: undefined;
5050
#groupColor: GroupColor | undefined;
5151

@@ -350,15 +350,23 @@ export class Logger {
350350
start: (title: string) => string;
351351
end: () => string;
352352
} {
353-
switch (this.#ciPlatform) {
354-
case 'GitHub Actions':
353+
// Nx typically renders native log groups for each target in GitHub
354+
// + GitHub doesn't support nested log groups: https://github.com/actions/toolkit/issues/1001
355+
// => skip native GitHub log groups if run within Nx target
356+
const platform =
357+
this.#ciPlatform === 'GitHub' && process.env['NX_TASK_TARGET_TARGET'] // https://nx.dev/docs/reference/environment-variables
358+
? undefined
359+
: this.#ciPlatform;
360+
361+
switch (platform) {
362+
case 'GitHub':
355363
// https://docs.github.com/en/actions/reference/workflows-and-actions/workflow-commands#grouping-log-lines
356364
return {
357365
start: title =>
358366
`::group::${this.#formatGroupTitle(title, { prefix: false })}`,
359367
end: () => '::endgroup::',
360368
};
361-
case 'GitLab CI/CD':
369+
case 'GitLab':
362370
// https://docs.gitlab.com/ci/jobs/job_logs/#custom-collapsible-sections
363371
const ansiEscCode = '\u001B[0K'; // '\e' ESC character only works for `echo -e`, Node console must use '\u001B'
364372
const id = Math.random().toString(HEX_RADIX).slice(2);

0 commit comments

Comments
 (0)