UNPKG

@cdwr/deploy-env-action

Version:

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

204 lines (190 loc) 6.24 kB
// packages/deploy-env-action/src/lib/fly-environment.ts import * as core4 from "@actions/core"; import * as github5 from "@actions/github"; // packages/core/src/lib/actions/add-pull-request-comment.ts import * as github from "@actions/github"; // packages/core/src/lib/actions/with-github.ts import { RequestError } from "@octokit/request-error"; import { StatusCodes, getReasonPhrase } from "http-status-codes"; async function withGitHub(operation, option) { try { return await operation(); } catch (error) { if (error instanceof RequestError) { switch (error.status) { case StatusCodes.UNAUTHORIZED: throw new Error( `Authentication failed. Please check your GitHub token. ${error.message}` ); case StatusCodes.FORBIDDEN: throw new Error( `Permission to the operation was denied. Please check your GitHub token. ${error.message}` ); case 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} - ${getReasonPhrase(error.status)}. ${error.message}` ); } } throw error; } } // packages/core/src/lib/actions/get-repository-default-branch.ts import * as github2 from "@actions/github"; 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 import { z } from "zod"; var EnvironmentSchema = z.enum(["preview", "production"]); var getDeployEnv = (context6, mainBranch) => { try { 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` }; } catch (error) { return { environment: null, reason: `Failed to determine deploy environment: ${error instanceof Error ? error.message : String(error)}` }; } }; // packages/core/src/lib/actions/get-pull-request.ts import * as core from "@actions/core"; import * as github3 from "@actions/github"; // packages/core/src/lib/actions/print-github-context.ts import * as core2 from "@actions/core"; import * as github4 from "@actions/github"; var printGitHubContext = () => { try { 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}`); } } } catch (error) { core2.info( `GitHub context details unavailable: ${error instanceof Error ? error.message : String(error)}` ); } }; // packages/deploy-env-action/src/lib/utils/get-environment-config.ts import * as core3 from "@actions/core"; // packages/deploy-env-action/src/lib/schemas/environment-config.schema.ts import { z as z2 } from "zod"; var EnvironmentConfigSchema = z2.object({ mainBranch: z2.string().min(1, "main branch is required"), token: z2.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/main.ts import * as core5 from "@actions/core"; // packages/deploy-env-action/src/lib/schemas/action-inputs.schema.ts import { z as z3 } from "zod"; var ActionInputsSchema = z3.object({ mainBranch: z3.string(), token: z3.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); } } } export { flyEnvironment, run };