Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions packages/cli-v3/src/utilities/githubActions.test.ts
Original file line number Diff line number Diff line change
@@ -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("");
});
});
19 changes: 9 additions & 10 deletions packages/cli-v3/src/utilities/githubActions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { appendFileSync } from "node:fs";
import { EOL } from "node:os";

function formatGithubActionsCommandFile(entries: Record<string, string>) {
const lines = Object.entries(entries).map(([key, value]) => `${key}=${value}`);

return lines.length > 0 ? `${lines.join(EOL)}${EOL}` : "";
}

export function setGithubActionsOutputAndEnvVars({
envVars,
Expand All @@ -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));
}
}