From ae6d0463192501f4a81ffbe58a85e5c87d9aaca8 Mon Sep 17 00:00:00 2001 From: Alex Szabo Date: Tue, 7 Jan 2025 12:42:45 +0100 Subject: [PATCH] [CI] Prevent console.log in pipeline.ts (#204724) ## Summary ~Logging to stdout from this file would result uploading the logged string to buildkite as a pipeline definition, causing errors (https://buildkite.com/elastic/kibana-pull-request/builds/261721#0193d94b-f05c-41d6-9865-3d3c331a6cc4)~ Adds an inline eslint rule to warn about `console.log/stdout` usage, as this has happened before by oversight. Overlaps with: https://github.com/elastic/kibana/pull/204672 --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com> --- .../pipeline-utils/buildkite/emitPipeline.ts | 13 ++++++++++ .buildkite/pipeline-utils/buildkite/index.ts | 1 + .../pipelines/pull_request/pipeline.ts | 24 ++++++++++++++----- 3 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 .buildkite/pipeline-utils/buildkite/emitPipeline.ts diff --git a/.buildkite/pipeline-utils/buildkite/emitPipeline.ts b/.buildkite/pipeline-utils/buildkite/emitPipeline.ts new file mode 100644 index 0000000000000..b0f12075eee79 --- /dev/null +++ b/.buildkite/pipeline-utils/buildkite/emitPipeline.ts @@ -0,0 +1,13 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the "Elastic License + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +export function emitPipeline(pipelineSteps: string[]) { + const pipelineStr = [...new Set(pipelineSteps)].join('\n'); + console.log(pipelineStr); +} diff --git a/.buildkite/pipeline-utils/buildkite/index.ts b/.buildkite/pipeline-utils/buildkite/index.ts index fbfdea985e92b..3e99ee03f3f2e 100644 --- a/.buildkite/pipeline-utils/buildkite/index.ts +++ b/.buildkite/pipeline-utils/buildkite/index.ts @@ -9,3 +9,4 @@ export * from './client'; export * from './types'; +export * from './emitPipeline'; diff --git a/.buildkite/scripts/pipelines/pull_request/pipeline.ts b/.buildkite/scripts/pipelines/pull_request/pipeline.ts index 9751cb75e589d..c6de1f4956092 100644 --- a/.buildkite/scripts/pipelines/pull_request/pipeline.ts +++ b/.buildkite/scripts/pipelines/pull_request/pipeline.ts @@ -7,9 +7,22 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ +/* eslint "no-restricted-syntax": [ + "error", + { + "selector": "CallExpression[callee.object.name='console'][callee.property.name!=/^(warn|error)$/]", + "message": "Debug logging to stdout in this file will attempt to upload the log message as yaml to buildkite, which might result in pipeline syntax error. Use emitPipeline() to upload steps, or log to stderr." + } + ] */ + import fs from 'fs'; import prConfigs from '../../../pull_requests.json'; -import { areChangesSkippable, doAnyChangesMatch, getAgentImageConfig } from '#pipeline-utils'; +import { + areChangesSkippable, + doAnyChangesMatch, + getAgentImageConfig, + emitPipeline, +} from '#pipeline-utils'; const prConfig = prConfigs.jobs.find((job) => job.pipelineSlug === 'kibana-pull-request'); const emptyStep = `steps: []`; @@ -35,7 +48,7 @@ const getPipeline = (filename: string, removeSteps = true) => { const skippable = await areChangesSkippable(SKIPPABLE_PR_MATCHERS, REQUIRED_PATHS); if (skippable) { - console.log(emptyStep); + emitPipeline([emptyStep]); return; } @@ -44,8 +57,8 @@ const getPipeline = (filename: string, removeSteps = true) => { const onlyRunQuickChecks = await areChangesSkippable([/^renovate\.json$/], REQUIRED_PATHS); if (onlyRunQuickChecks) { pipeline.push(getPipeline('.buildkite/pipelines/pull_request/renovate.yml', false)); - - console.log([...new Set(pipeline)].join('\n')); + console.warn('Isolated changes to renovate.json. Skipping main PR pipeline.'); + emitPipeline(pipeline); return; } @@ -401,8 +414,7 @@ const getPipeline = (filename: string, removeSteps = true) => { pipeline.push(getPipeline('.buildkite/pipelines/pull_request/post_build.yml')); - // remove duplicated steps - console.log([...new Set(pipeline)].join('\n')); + emitPipeline(pipeline); } catch (ex) { console.error('Error while generating the pipeline steps: ' + ex.message, ex); process.exit(1);