diff --git a/index.js b/index.js index 38f958b..de578bc 100644 --- a/index.js +++ b/index.js @@ -21114,12 +21114,12 @@ var __publicField2 = (obj, key, value) => { var Shell = class { constructor(env_passthrough = ["PATH"]) { __publicField(this, "process"); - const env = { PS1: "" }; + const env2 = { PS1: "" }; env_passthrough.forEach((key) => { - env[key] = process.env[key]; + env2[key] = process.env[key]; }); this.process = import_child_process.default.spawn("bash", ["--noprofile", "--norc"], { - env, + env: env2, detached: true }); this.process.stdout.setEncoding("utf8"); @@ -22059,6 +22059,7 @@ var src_default = shellac; // src/index.ts var import_undici = __toESM(require_undici()); +var import_process = require("process"); try { const apiToken = (0, import_core.getInput)("apiToken", { required: true }); const accountId = (0, import_core.getInput)("accountId", { required: true }); @@ -22067,6 +22068,14 @@ try { const gitHubToken = (0, import_core.getInput)("gitHubToken", { required: false }); const branch = (0, import_core.getInput)("branch", { required: false }); const octokit = (0, import_github.getOctokit)(gitHubToken); + const getProject = async () => { + const response = await (0, import_undici.fetch)( + `https://api.cloudflare.com/client/v4/accounts/${accountId}/pages/projects/${projectName}`, + { headers: { Authorization: `Bearer ${apiToken}` } } + ); + const { result } = await response.json(); + return result; + }; const createPagesDeployment = async () => { await src_default` $ export CLOUDFLARE_API_TOKEN="${apiToken}" @@ -22085,14 +22094,16 @@ try { } = await response.json(); return deployment; }; - const createGitHubDeployment = async () => { + const createGitHubDeployment = async (productionEnvironment, environment) => { const deployment = await octokit.rest.repos.createDeployment({ owner: import_github.context.repo.owner, repo: import_github.context.repo.repo, ref: import_github.context.ref, auto_merge: false, description: "Cloudflare Pages", - required_contexts: [] + required_contexts: [], + environment, + production_environment: productionEnvironment }); if (deployment.status === 201) { return deployment.data; @@ -22121,21 +22132,22 @@ try { if (gitHubToken === "") { return; } - const gitHubDeployment = await createGitHubDeployment(); + const project = await getProject(); + const githubBranch = import_process.env.GITHUB_REF_NAME; + const productionEnvironment = githubBranch === project.production_branch; + let environmentName; + if (productionEnvironment) { + environmentName = "Production"; + } else { + environmentName = `Preview (${githubBranch})`; + } + const gitHubDeployment = await createGitHubDeployment(productionEnvironment, environmentName); const pagesDeployment = await createPagesDeployment(); console.log(pagesDeployment); - const productionEnvironment = pagesDeployment.environment === "production"; (0, import_core.setOutput)("id", pagesDeployment.id); (0, import_core.setOutput)("url", pagesDeployment.url); (0, import_core.setOutput)("environment", pagesDeployment.environment); (0, import_core.setOutput)("alias", productionEnvironment ? pagesDeployment.url : pagesDeployment.aliases[0]); - let environmentName; - if (productionEnvironment) { - environmentName = "Production"; - } else { - const url = new URL(pagesDeployment.aliases[0]); - environmentName = `Preview (${url.hostname.split(".")[0]})`; - } if (gitHubDeployment) { await createGitHubDeploymentStatus({ id: gitHubDeployment.id, diff --git a/package-lock.json b/package-lock.json index c97fc39..e353168 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,7 @@ "undici": "^5.11.0" }, "devDependencies": { + "@types/node": "^18.11.3", "esbuild": "^0.15.12", "husky": "^8.0.1", "prettier": "^2.6.2", @@ -212,6 +213,12 @@ "@octokit/openapi-types": "^12.11.0" } }, + "node_modules/@types/node": { + "version": "18.11.3", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.3.tgz", + "integrity": "sha512-fNjDQzzOsZeKZu5NATgXUPsaFaTxeRgFXoosrHivTl8RGeV733OLawXsGfEk9a8/tySyZUyiZ6E8LcjPFZ2y1A==", + "dev": true + }, "node_modules/before-after-hook": { "version": "2.2.3", "resolved": "https://registry.npmjs.org/before-after-hook/-/before-after-hook-2.2.3.tgz", @@ -961,6 +968,12 @@ "@octokit/openapi-types": "^12.11.0" } }, + "@types/node": { + "version": "18.11.3", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.3.tgz", + "integrity": "sha512-fNjDQzzOsZeKZu5NATgXUPsaFaTxeRgFXoosrHivTl8RGeV733OLawXsGfEk9a8/tySyZUyiZ6E8LcjPFZ2y1A==", + "dev": true + }, "before-after-hook": { "version": "2.2.3", "resolved": "https://registry.npmjs.org/before-after-hook/-/before-after-hook-2.2.3.tgz", diff --git a/package.json b/package.json index 1d62356..0112d55 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "undici": "^5.11.0" }, "devDependencies": { + "@types/node": "^18.11.3", "esbuild": "^0.15.12", "husky": "^8.0.1", "prettier": "^2.6.2", diff --git a/src/index.ts b/src/index.ts index 5c4c6e6..fb5858b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,8 +2,15 @@ import { getInput, setOutput, setFailed } from "@actions/core"; import { context, getOctokit } from "@actions/github"; import shellac from "shellac"; import { fetch } from "undici"; +import { env } from "process"; import type { Deployment } from '@cloudflare/types'; +// TODO: Add Project to @cloudflare/types +interface Project { + name: string; + production_branch: string; +} + try { const apiToken = getInput("apiToken", { required: true }); const accountId = getInput("accountId", { required: true }); @@ -14,6 +21,15 @@ try { const octokit = getOctokit(gitHubToken); + const getProject = async () => { + const response = await fetch( + `https://api.cloudflare.com/client/v4/accounts/${accountId}/pages/projects/${projectName}`, + { headers: { Authorization: `Bearer ${apiToken}` } } + ); + const { result } = await response.json() as { result: Project }; + return result; + } + const createPagesDeployment = async () => { // TODO: Replace this with an API call to wrangler so we can get back a full deployment response object await shellac` @@ -36,7 +52,7 @@ try { return deployment; }; - const createGitHubDeployment = async () => { + const createGitHubDeployment = async (productionEnvironment: boolean, environment: string) => { const deployment = await octokit.rest.repos.createDeployment({ owner: context.repo.owner, repo: context.repo.repo, @@ -44,6 +60,8 @@ try { auto_merge: false, description: "Cloudflare Pages", required_contexts: [], + environment, + production_environment: productionEnvironment, }); if (deployment.status === 201) { @@ -83,29 +101,31 @@ try { return; } - const gitHubDeployment = await createGitHubDeployment(); + const project = await getProject(); + + const githubBranch = env.GITHUB_REF_NAME; + const productionEnvironment = githubBranch === project.production_branch; + + let environmentName: string; + if (productionEnvironment) { + environmentName = "Production" + } else { + // Use the branch name + environmentName = `Preview (${githubBranch})`; + } + + const gitHubDeployment = await createGitHubDeployment(productionEnvironment, environmentName); const pagesDeployment = await createPagesDeployment(); // la la debug console.log(pagesDeployment) - const productionEnvironment = pagesDeployment.environment === "production"; - setOutput("id", pagesDeployment.id); setOutput("url", pagesDeployment.url); setOutput("environment", pagesDeployment.environment); setOutput("alias", productionEnvironment ? pagesDeployment.url : pagesDeployment.aliases[0]); - let environmentName: string; - if (productionEnvironment) { - environmentName = "Production" - } else { - const url = new URL(pagesDeployment.aliases[0]); - // Use the branch alias (staging/walshy-fix-bug) - environmentName = `Preview (${url.hostname.split(".")[0]})`; - } - if (gitHubDeployment) { await createGitHubDeploymentStatus({ id: gitHubDeployment.id,