|
|
@ -1,9 +1,11 @@
|
|
|
|
import { getInput, setOutput, setFailed } from "@actions/core";
|
|
|
|
import { getInput, setOutput, setFailed } from "@actions/core";
|
|
|
|
|
|
|
|
import type { Project, Deployment } from '@cloudflare/types';
|
|
|
|
import { context, getOctokit } from "@actions/github";
|
|
|
|
import { context, getOctokit } from "@actions/github";
|
|
|
|
import shellac from "shellac";
|
|
|
|
import shellac from "shellac";
|
|
|
|
import { fetch } from "undici";
|
|
|
|
import { fetch } from "undici";
|
|
|
|
import { env } from "process";
|
|
|
|
import { env } from "process";
|
|
|
|
import type { Project, Deployment } from '@cloudflare/types';
|
|
|
|
|
|
|
|
|
|
|
|
type Octokit = ReturnType<typeof getOctokit>;
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const apiToken = getInput("apiToken", { required: true });
|
|
|
|
const apiToken = getInput("apiToken", { required: true });
|
|
|
@ -13,14 +15,12 @@ try {
|
|
|
|
const gitHubToken = getInput("gitHubToken", { required: false });
|
|
|
|
const gitHubToken = getInput("gitHubToken", { required: false });
|
|
|
|
const branch = getInput("branch", { required: false });
|
|
|
|
const branch = getInput("branch", { required: false });
|
|
|
|
|
|
|
|
|
|
|
|
let octokit;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getProject = async () => {
|
|
|
|
const getProject = async () => {
|
|
|
|
const response = await fetch(
|
|
|
|
const response = await fetch(
|
|
|
|
`https://api.cloudflare.com/client/v4/accounts/${accountId}/pages/projects/${projectName}`,
|
|
|
|
`https://api.cloudflare.com/client/v4/accounts/${accountId}/pages/projects/${projectName}`,
|
|
|
|
{ headers: { Authorization: `Bearer ${apiToken}` } }
|
|
|
|
{ headers: { Authorization: `Bearer ${apiToken}` } }
|
|
|
|
);
|
|
|
|
);
|
|
|
|
const { result } = await response.json() as { result: Project };
|
|
|
|
const { result } = await response.json() as { result: Project | null };
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -46,7 +46,7 @@ try {
|
|
|
|
return deployment;
|
|
|
|
return deployment;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const createGitHubDeployment = async (productionEnvironment: boolean, environment: string) => {
|
|
|
|
const createGitHubDeployment = async (octokit: Octokit, productionEnvironment: boolean, environment: string) => {
|
|
|
|
const deployment = await octokit.rest.repos.createDeployment({
|
|
|
|
const deployment = await octokit.rest.repos.createDeployment({
|
|
|
|
owner: context.repo.owner,
|
|
|
|
owner: context.repo.owner,
|
|
|
|
repo: context.repo.repo,
|
|
|
|
repo: context.repo.repo,
|
|
|
@ -69,7 +69,9 @@ try {
|
|
|
|
deploymentId,
|
|
|
|
deploymentId,
|
|
|
|
environmentName,
|
|
|
|
environmentName,
|
|
|
|
productionEnvironment,
|
|
|
|
productionEnvironment,
|
|
|
|
|
|
|
|
octokit,
|
|
|
|
}: {
|
|
|
|
}: {
|
|
|
|
|
|
|
|
octokit: Octokit,
|
|
|
|
id: number;
|
|
|
|
id: number;
|
|
|
|
url: string;
|
|
|
|
url: string;
|
|
|
|
deploymentId: string;
|
|
|
|
deploymentId: string;
|
|
|
@ -91,28 +93,21 @@ try {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
(async () => {
|
|
|
|
if (gitHubToken === "") {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const project = await getProject();
|
|
|
|
const project = await getProject();
|
|
|
|
octokit = getOctokit(gitHubToken);
|
|
|
|
if (!project) throw new Error('Unable to find pages project')
|
|
|
|
|
|
|
|
|
|
|
|
const githubBranch = env.GITHUB_REF_NAME;
|
|
|
|
const githubBranch = env.GITHUB_REF_NAME;
|
|
|
|
const productionEnvironment = githubBranch === project.production_branch;
|
|
|
|
const productionEnvironment = githubBranch === project.production_branch;
|
|
|
|
|
|
|
|
const environmentName = productionEnvironment ? "Production" : `Preview: (${githubBranch})`;
|
|
|
|
|
|
|
|
|
|
|
|
let environmentName: string;
|
|
|
|
let gitHubDeployment: Awaited<ReturnType<typeof createGitHubDeployment>>;
|
|
|
|
if (productionEnvironment) {
|
|
|
|
|
|
|
|
environmentName = "Production"
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Use the branch name
|
|
|
|
|
|
|
|
environmentName = `Preview (${githubBranch})`;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const gitHubDeployment = await createGitHubDeployment(productionEnvironment, environmentName);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (gitHubToken && gitHubToken.length) {
|
|
|
|
|
|
|
|
const octokit = getOctokit(gitHubToken);
|
|
|
|
|
|
|
|
gitHubDeployment = await createGitHubDeployment(octokit, productionEnvironment, environmentName);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const pagesDeployment = await createPagesDeployment();
|
|
|
|
const pagesDeployment = await createPagesDeployment();
|
|
|
|
|
|
|
|
|
|
|
|
setOutput("id", pagesDeployment.id);
|
|
|
|
setOutput("id", pagesDeployment.id);
|
|
|
|
setOutput("url", pagesDeployment.url);
|
|
|
|
setOutput("url", pagesDeployment.url);
|
|
|
|
setOutput("environment", pagesDeployment.environment);
|
|
|
|
setOutput("environment", pagesDeployment.environment);
|
|
|
@ -124,12 +119,15 @@ try {
|
|
|
|
setOutput("alias", alias);
|
|
|
|
setOutput("alias", alias);
|
|
|
|
|
|
|
|
|
|
|
|
if (gitHubDeployment) {
|
|
|
|
if (gitHubDeployment) {
|
|
|
|
|
|
|
|
const octokit = getOctokit(gitHubToken);
|
|
|
|
|
|
|
|
|
|
|
|
await createGitHubDeploymentStatus({
|
|
|
|
await createGitHubDeploymentStatus({
|
|
|
|
id: gitHubDeployment.id,
|
|
|
|
id: gitHubDeployment.id,
|
|
|
|
url: pagesDeployment.url,
|
|
|
|
url: pagesDeployment.url,
|
|
|
|
deploymentId: pagesDeployment.id,
|
|
|
|
deploymentId: pagesDeployment.id,
|
|
|
|
environmentName,
|
|
|
|
environmentName,
|
|
|
|
productionEnvironment,
|
|
|
|
productionEnvironment,
|
|
|
|
|
|
|
|
octokit
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
})();
|
|
|
|