From e598c5d087473429bd91bae94f8cb1137cda8476 Mon Sep 17 00:00:00 2001 From: sid sri Date: Thu, 23 Jul 2026 22:08:01 +0530 Subject: [PATCH] fix(cli): terminate github actions command files --- .../src/utilities/githubActions.test.ts | 73 +++++++++++++++++++ .../cli-v3/src/utilities/githubActions.ts | 19 +++-- 2 files changed, 82 insertions(+), 10 deletions(-) create mode 100644 packages/cli-v3/src/utilities/githubActions.test.ts diff --git a/packages/cli-v3/src/utilities/githubActions.test.ts b/packages/cli-v3/src/utilities/githubActions.test.ts new file mode 100644 index 00000000000..6896d1f5de2 --- /dev/null +++ b/packages/cli-v3/src/utilities/githubActions.test.ts @@ -0,0 +1,73 @@ +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, describe, expect, it } from "vitest"; + +import { setGithubActionsOutputAndEnvVars } from "./githubActions.js"; + +describe("setGithubActionsOutputAndEnvVars", () => { + const originalGithubEnv = process.env.GITHUB_ENV; + const originalGithubOutput = process.env.GITHUB_OUTPUT; + let tmpDir: string | undefined; + + afterEach(() => { + if (originalGithubEnv === undefined) { + delete process.env.GITHUB_ENV; + } else { + process.env.GITHUB_ENV = originalGithubEnv; + } + + if (originalGithubOutput === undefined) { + delete process.env.GITHUB_OUTPUT; + } else { + process.env.GITHUB_OUTPUT = originalGithubOutput; + } + + if (tmpDir) { + rmSync(tmpDir, { recursive: true, force: true }); + tmpDir = undefined; + } + }); + + it("terminates GitHub Actions env and output entries with newlines", () => { + tmpDir = mkdtempSync(join(tmpdir(), "trigger-github-actions-")); + const envFile = join(tmpDir, "env"); + const outputFile = join(tmpDir, "output"); + process.env.GITHUB_ENV = envFile; + process.env.GITHUB_OUTPUT = outputFile; + + setGithubActionsOutputAndEnvVars({ + envVars: { + deploymentId: "dep_123", + imageTag: "v1", + }, + outputs: { + deploymentId: "dep_123", + needsPromotion: "true", + }, + }); + + expect(readFileSync(envFile, "utf8")).toMatch(/^deploymentId=dep_123\r?\nimageTag=v1\r?\n$/); + expect(readFileSync(outputFile, "utf8")).toMatch( + /^deploymentId=dep_123\r?\nneedsPromotion=true\r?\n$/ + ); + }); + + it("does not append content when there are no env vars or outputs", () => { + tmpDir = mkdtempSync(join(tmpdir(), "trigger-github-actions-")); + const envFile = join(tmpDir, "env"); + const outputFile = join(tmpDir, "output"); + writeFileSync(envFile, ""); + writeFileSync(outputFile, ""); + process.env.GITHUB_ENV = envFile; + process.env.GITHUB_OUTPUT = outputFile; + + setGithubActionsOutputAndEnvVars({ + envVars: {}, + outputs: {}, + }); + + expect(readFileSync(envFile, "utf8")).toBe(""); + expect(readFileSync(outputFile, "utf8")).toBe(""); + }); +}); diff --git a/packages/cli-v3/src/utilities/githubActions.ts b/packages/cli-v3/src/utilities/githubActions.ts index a4a80e75a1e..c0d9056aa35 100644 --- a/packages/cli-v3/src/utilities/githubActions.ts +++ b/packages/cli-v3/src/utilities/githubActions.ts @@ -1,4 +1,11 @@ import { appendFileSync } from "node:fs"; +import { EOL } from "node:os"; + +function formatGithubActionsCommandFile(entries: Record) { + const lines = Object.entries(entries).map(([key, value]) => `${key}=${value}`); + + return lines.length > 0 ? `${lines.join(EOL)}${EOL}` : ""; +} export function setGithubActionsOutputAndEnvVars({ envVars, @@ -9,19 +16,11 @@ export function setGithubActionsOutputAndEnvVars({ }) { // Set environment variables if (process.env.GITHUB_ENV) { - const contents = Object.entries(envVars) - .map(([key, value]) => `${key}=${value}`) - .join("\n"); - - appendFileSync(process.env.GITHUB_ENV, contents); + appendFileSync(process.env.GITHUB_ENV, formatGithubActionsCommandFile(envVars)); } // Set outputs if (process.env.GITHUB_OUTPUT) { - const contents = Object.entries(outputs) - .map(([key, value]) => `${key}=${value}`) - .join("\n"); - - appendFileSync(process.env.GITHUB_OUTPUT, contents); + appendFileSync(process.env.GITHUB_OUTPUT, formatGithubActionsCommandFile(outputs)); } }