@equinor/fusion-framework-cli
Version:
[](./LICENSE)
71 lines • 3.79 kB
JavaScript
import { readFileSync } from 'node:fs';
/**
* Resolves GitHub Actions-specific annotation variables from environment variables.
*
* This function extracts relevant workflow and repository information from GitHub Actions environment variables.
* It constructs a set of annotation variables that can be used for reporting or logging purposes in CI/CD pipelines.
*
* @returns {Record<string, string>} An object containing GitHub Actions annotation variables such as eventName, actor,
* payload, runId, repository, and runUrl. If a variable is not found, its value defaults to 'unknown'.
*
* Environment variables used by this function (see: https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables):
* - GITHUB_EVENT_NAME: Name of the event that triggered the workflow
* - GITHUB_ACTOR: Username of the person or app that initiated the workflow
* - GITHUB_RUN_ID: Unique identifier for the workflow run
* - GITHUB_REPOSITORY: Repository in the format owner/repo
* - GITHUB_SERVER_URL: Base URL of the GitHub server (defaults to https://github.com)
* - GITHUB_EVENT_PATH: Path to the event payload file (JSON)
* - GITHUB_WORKFLOW: Name of the workflow
*
* Notes for maintainers:
* - The event payload can be large; consider truncating or parsing if only specific fields are needed.
* - If reading the payload fails, the function returns a fallback string. Adjust error handling as needed for your use case.
* - If you need additional GitHub Actions context, refer to the official documentation and add new variables here.
* - The payload and other fields may contain sensitive information. Use caution when logging or exposing these values.
*
* Extending this function:
* - To add more annotations, extract additional environment variables as needed and include them in the returned object.
* - For custom workflows or self-hosted runners, verify that all required environment variables are available.
*/
export const resolveGithubAnnotations = () => {
// Extract event name from environment
const eventName = process.env.GITHUB_EVENT_NAME || 'unknown';
// Extract actor (user who triggered the workflow)
const actor = process.env.GITHUB_ACTOR || 'unknown';
// Extract run ID
const runId = process.env.GITHUB_RUN_ID || 'unknown';
// Extract repository in the format owner/repo
const repository = process.env.GITHUB_REPOSITORY || 'unknown';
// Extract workflow name
const workflow = process.env.GITHUB_WORKFLOW || 'unknown';
// Extract server URL (defaults to public GitHub if not set)
const serverUrl = process.env.GITHUB_SERVER_URL || 'https://github.com';
// Construct a direct URL to the workflow run if all required parts are available
let runUrl = 'unknown';
if (serverUrl && repository !== 'unknown' && runId !== 'unknown') {
// Remove trailing slash from serverUrl if present, then build the run results URL
runUrl = `${serverUrl.replace(/\/$/, '')}/${repository}/actions/runs/${runId}`;
}
// Aggregate all annotation variables into a single object for easy consumption
const annotations = {
eventName,
actor,
runId,
runUrl,
workflow,
};
// Apply event payload to annotations
if (process.env.GITHUB_EVENT_PATH) {
try {
const rawPayload = readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8');
Object.assign(annotations, JSON.parse(rawPayload));
}
catch {
console.error('Failed to parse GitHub event payload');
}
}
// Return the resolved annotation variables
return annotations;
};
export default resolveGithubAnnotations;
//# sourceMappingURL=resolve-github-annotations.js.map