| | | 1 | | import { randomUUID } from 'node:crypto'; |
| | | 2 | | import { appendFile } from 'node:fs/promises'; |
| | | 3 | | import { EOL } from 'node:os'; |
| | | 4 | | import { InvalidArgumentError } from '../../domain/errors/DomainErrors.js'; |
| | | 5 | | import type { IEnvironmentExporter } from '../../domain/ports/IEnvironmentExporter.js'; |
| | | 6 | | |
| | | 7 | | /** |
| | | 8 | | * Names the GitHub runner refuses to set from `GITHUB_ENV`: `NODE_OPTIONS` |
| | | 9 | | * and the documented default variables, which cannot be overwritten. Only |
| | | 10 | | * exact names: other `GITHUB_*` and `RUNNER_*` names export normally. `CI` |
| | | 11 | | * is left out because GitHub still lets it be overwritten. |
| | | 12 | | * https://docs.github.com/en/actions/reference/workflows-and-actions/variables |
| | | 13 | | */ |
| | 4 | 14 | | const RUNNER_BLOCKED_NAMES = new Set([ |
| | | 15 | | 'NODE_OPTIONS', |
| | | 16 | | 'GITHUB_ACTION', |
| | | 17 | | 'GITHUB_ACTION_PATH', |
| | | 18 | | 'GITHUB_ACTION_REPOSITORY', |
| | | 19 | | 'GITHUB_ACTIONS', |
| | | 20 | | 'GITHUB_ACTOR', |
| | | 21 | | 'GITHUB_ACTOR_ID', |
| | | 22 | | 'GITHUB_API_URL', |
| | | 23 | | 'GITHUB_ARTIFACTS', |
| | | 24 | | 'GITHUB_ARTIFACTS_LIST', |
| | | 25 | | 'GITHUB_BASE_REF', |
| | | 26 | | 'GITHUB_ENV', |
| | | 27 | | 'GITHUB_EVENT_NAME', |
| | | 28 | | 'GITHUB_EVENT_PATH', |
| | | 29 | | 'GITHUB_GRAPHQL_URL', |
| | | 30 | | 'GITHUB_HEAD_REF', |
| | | 31 | | 'GITHUB_JOB', |
| | | 32 | | 'GITHUB_OUTPUT', |
| | | 33 | | 'GITHUB_PATH', |
| | | 34 | | 'GITHUB_REF', |
| | | 35 | | 'GITHUB_REF_NAME', |
| | | 36 | | 'GITHUB_REF_PROTECTED', |
| | | 37 | | 'GITHUB_REF_TYPE', |
| | | 38 | | 'GITHUB_REPOSITORY', |
| | | 39 | | 'GITHUB_REPOSITORY_ID', |
| | | 40 | | 'GITHUB_REPOSITORY_OWNER', |
| | | 41 | | 'GITHUB_REPOSITORY_OWNER_ID', |
| | | 42 | | 'GITHUB_RETENTION_DAYS', |
| | | 43 | | 'GITHUB_RUN_ATTEMPT', |
| | | 44 | | 'GITHUB_RUN_ID', |
| | | 45 | | 'GITHUB_RUN_NUMBER', |
| | | 46 | | 'GITHUB_SERVER_URL', |
| | | 47 | | 'GITHUB_SHA', |
| | | 48 | | 'GITHUB_STEP_SUMMARY', |
| | | 49 | | 'GITHUB_TRIGGERING_ACTOR', |
| | | 50 | | 'GITHUB_WORKFLOW', |
| | | 51 | | 'GITHUB_WORKFLOW_REF', |
| | | 52 | | 'GITHUB_WORKFLOW_SHA', |
| | | 53 | | 'GITHUB_WORKSPACE', |
| | | 54 | | 'RUNNER_ARCH', |
| | | 55 | | 'RUNNER_DEBUG', |
| | | 56 | | 'RUNNER_ENVIRONMENT', |
| | | 57 | | 'RUNNER_NAME', |
| | | 58 | | 'RUNNER_OS', |
| | | 59 | | 'RUNNER_TEMP', |
| | | 60 | | 'RUNNER_TOOL_CACHE', |
| | | 61 | | ]); |
| | | 62 | | |
| | | 63 | | /** |
| | | 64 | | * Exports variables to the later steps of a GitHub Actions job by appending |
| | | 65 | | * them to the file named by `GITHUB_ENV`, which the runner reads when the |
| | | 66 | | * current step ends. |
| | | 67 | | * |
| | | 68 | | * Every variable uses the multiline `NAME<<DELIMITER` form with a random |
| | | 69 | | * delimiter, so a value containing newlines cannot close its own block and |
| | | 70 | | * declare extra variables. All variables are appended in a single write. |
| | | 71 | | */ |
| | | 72 | | export class GitHubEnvFileExporter implements IEnvironmentExporter { |
| | | 73 | | /** |
| | | 74 | | * @param options.filePath - File to append to (default: `GITHUB_ENV`, |
| | | 75 | | * read when exporting). |
| | | 76 | | */ |
| | 34 | 77 | | constructor(private readonly options: { filePath?: string } = {}) {} |
| | | 78 | | |
| | | 79 | | async export(variables: Readonly<Record<string, string>>): Promise<void> { |
| | 8 | 80 | | const filePath = this.options.filePath ?? process.env.GITHUB_ENV; |
| | 8 | 81 | | if (!filePath) { |
| | 1 | 82 | | throw new InvalidArgumentError( |
| | | 83 | | 'GITHUB_ENV is not set: exporting secrets to the job environment only works inside a GitHub Actions job. Set env |
| | | 84 | | ); |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | // The runner skips these names but applies the rest, so the step would |
| | | 88 | | // report a variable later steps never get. Compared case-insensitively: |
| | | 89 | | // on Windows runners `node_options` is the same variable. |
| | 7 | 90 | | const blocked = Object.keys(variables).filter((name) => |
| | 11 | 91 | | RUNNER_BLOCKED_NAMES.has(name.toUpperCase()), |
| | | 92 | | ); |
| | 7 | 93 | | if (blocked.length > 0) { |
| | 2 | 94 | | throw new InvalidArgumentError( |
| | | 95 | | `${blocked.join(', ')} cannot be exported to the job environment: GitHub Actions does not let GITHUB_ENV set NOD |
| | | 96 | | ); |
| | | 97 | | } |
| | | 98 | | |
| | 5 | 99 | | const blocks = Object.entries(variables).map(([name, value]) => |
| | 6 | 100 | | GitHubEnvFileExporter.block(name, value), |
| | | 101 | | ); |
| | 5 | 102 | | if (blocks.length === 0) { |
| | 0 | 103 | | return; |
| | | 104 | | } |
| | 5 | 105 | | await appendFile(filePath, blocks.join(''), { encoding: 'utf8' }); |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | private static block(name: string, value: string): string { |
| | 6 | 109 | | const delimiter = `ghadelimiter_${randomUUID()}`; |
| | 6 | 110 | | if (name.includes(delimiter) || value.includes(delimiter)) { |
| | 0 | 111 | | throw new InvalidArgumentError( |
| | | 112 | | `Unexpected delimiter collision while exporting ${name}.`, |
| | | 113 | | ); |
| | | 114 | | } |
| | 6 | 115 | | return `${name}<<${delimiter}${EOL}${value}${EOL}${delimiter}${EOL}`; |
| | | 116 | | } |
| | | 117 | | } |