UNPKG

@cdwr/deploy-env-action

Version:

The Deploy Env Action will analyze the environment to deploy your Fly.io applications to.

213 lines (197 loc) 7.35 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); // packages/deploy-env-action/src/lib/main.ts var core5 = __toESM(require("@actions/core"), 1); // packages/deploy-env-action/src/lib/fly-environment.ts var core4 = __toESM(require("@actions/core"), 1); var github5 = __toESM(require("@actions/github"), 1); // packages/core/src/lib/actions/add-pull-request-comment.ts var github = __toESM(require("@actions/github"), 1); // packages/core/src/lib/actions/with-github.ts var import_request_error = require("@octokit/request-error"); var import_http_status_codes = require("http-status-codes"); async function withGitHub(operation, option) { try { return await operation(); } catch (error) { if (error instanceof import_request_error.RequestError) { switch (error.status) { case import_http_status_codes.StatusCodes.UNAUTHORIZED: throw new Error( `Authentication failed. Please check your GitHub token. ${error.message}` ); case import_http_status_codes.StatusCodes.FORBIDDEN: throw new Error( `Permission to the operation was denied. Please check your GitHub token. ${error.message}` ); case import_http_status_codes.StatusCodes.NOT_FOUND: if (option === "not-found-returns-null") { return null; } throw new Error( `Requested information was not found. ${error.message}` ); default: throw new Error( `Request failed with ${error.status} - ${(0, import_http_status_codes.getReasonPhrase)(error.status)}. ${error.message}` ); } } throw error; } } // packages/core/src/lib/actions/get-repository-default-branch.ts var github2 = __toESM(require("@actions/github"), 1); var getRepositoryDefaultBranch = async (token) => { const octokit = github2.getOctokit(token); const { data: { default_branch } } = await withGitHub( async () => octokit.rest.repos.get({ ...github2.context.repo }) ); return default_branch; }; // packages/core/src/lib/actions/get-deploy-env.ts var import_zod = require("zod"); var EnvironmentSchema = import_zod.z.enum(["preview", "production"]); var getDeployEnv = (context6, mainBranch) => { const { eventName, ref } = context6; const currentBranch = ref.split("/").at(-1); if (eventName === "pull_request") { return { environment: "preview" }; } if (eventName === "push") { if (currentBranch === mainBranch) { return { environment: "production" }; } return { environment: null, reason: `'${currentBranch}' is not supported for production deployment, only '${mainBranch}' is supported` }; } return { environment: null, reason: `'${eventName}' is not a supported event for deployment, only 'pull_request' and 'push' are supported` }; }; // packages/core/src/lib/actions/get-pull-request.ts var core = __toESM(require("@actions/core"), 1); var github3 = __toESM(require("@actions/github"), 1); // packages/core/src/lib/actions/print-github-context.ts var core2 = __toESM(require("@actions/core"), 1); var github4 = __toESM(require("@actions/github"), 1); var printGitHubContext = () => { core2.info("== Repo =="); core2.info(`- owner: ${github4.context.repo.owner}`); core2.info(`- repo: ${github4.context.repo.repo}`); core2.info("== Misc =="); for (const [key, value] of Object.entries(github4.context)) { if (typeof value === "string") { core2.info(`- ${key}: ${value}`); } } }; // packages/deploy-env-action/src/lib/utils/get-environment-config.ts var core3 = __toESM(require("@actions/core"), 1); // packages/deploy-env-action/src/lib/schemas/environment-config.schema.ts var import_zod2 = require("zod"); var EnvironmentConfigSchema = import_zod2.z.object({ mainBranch: import_zod2.z.string().min(1, "main branch is required"), token: import_zod2.z.string().min(1, "GitHub token is required") }); // packages/deploy-env-action/src/lib/utils/get-environment-config.ts var getEnvironmentConfig = async (inputs) => { const { mainBranch: mainBranchInput, token: tokenInput } = inputs; const token = tokenInput || process.env["GITHUB_TOKEN"]; const mainBranch = mainBranchInput || await getRepositoryDefaultBranch(token); const config = { mainBranch, token }; core3.info(JSON.stringify(config, null, 2)); return EnvironmentConfigSchema.parse(config); }; // packages/deploy-env-action/src/lib/fly-environment.ts async function flyEnvironment(inputs, throwOnError = false) { try { let environment = ""; core4.info("Starting fly environment process"); core4.startGroup("GitHub context details"); printGitHubContext(); core4.endGroup(); core4.startGroup("Get environment configuration"); const config = await getEnvironmentConfig(inputs); core4.endGroup(); core4.startGroup("Analyze event"); const response = getDeployEnv(github5.context, config.mainBranch); if (response.environment) { environment = response.environment; } else { core4.warning(response.reason); } core4.info(`Environment: ${environment}`); core4.endGroup(); return { environment }; } catch (error) { if (error instanceof Error) { core4.setFailed(error.message); if (throwOnError) { throw error.message; } } return {}; } } // packages/deploy-env-action/src/lib/schemas/action-inputs.schema.ts var import_zod3 = require("zod"); var ActionInputsSchema = import_zod3.z.object({ mainBranch: import_zod3.z.string(), token: import_zod3.z.string() }); // packages/deploy-env-action/src/lib/main.ts async function run() { try { const inputs = ActionInputsSchema.parse({ mainBranch: core5.getInput("main-branch"), token: core5.getInput("token") }); core5.debug(`Inputs: ${JSON.stringify(inputs, null, 2)}`); const { environment } = await flyEnvironment(inputs); core5.exportVariable("DEPLOY_ENV", environment); core5.setOutput("environment", environment); } catch (error) { if (error instanceof Error) { core5.setFailed(error.message); } } } // packages/deploy-env-action/src/action.ts run();