Skip to content

Commit

Permalink
fix: add dist to run Docker build
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroki0525 committed Feb 10, 2023
1 parent 1faf120 commit f147f53
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
64 changes: 64 additions & 0 deletions dist/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { setTimeout } from "timers/promises";
import * as core from "@actions/core";
import * as github from "@actions/github";
if (github.context.eventName !== "delete") {
throw new Error("This action only supports delete event.");
}
const targetBranch = github.context.payload.ref;
const vercelToken = core.getInput("vercelToken");
const vercelProjectId = core.getInput("vercelProjectId");
const vercelBaseUrl = "https://api.vercel.com";
const vercelGetDeploymentsEndpoint = `${vercelBaseUrl}/v6/deployments`;
const vercelDeleteDeploymentsEndpoint = `${vercelBaseUrl}/v13/deployments`;
core.info(`Target branch is ${targetBranch}.`);
// https://vercel.com/docs/rest-api#endpoints/deployments/list-deployments
// max limit is 100 according to https://vercel.com/docs/rest-api#introduction/api-basics/pagination
const deploymentsRes = await fetch(`${vercelGetDeploymentsEndpoint}?limit=100&projectId=${vercelProjectId}`, {
headers: {
Authorization: `Bearer ${vercelToken}`,
},
method: "get",
});
if (!deploymentsRes.ok) {
core.error(`Status code is ${deploymentsRes.status} when fetched to get deployments list. The response message is as follows.`);
const resText = await deploymentsRes.text();
throw new Error(resText);
}
const { deployments } = (await deploymentsRes.json());
const shouldDeleteDeploymentIds = deployments
.filter(({ state, meta }) => meta?.githubCommitRef === targetBranch &&
["BUILDING", "INITIALIZING", "QUEUED", "READY"].includes(state ?? ""))
.map(({ uid }) => uid);
const shouldDeleteDeploymentCount = shouldDeleteDeploymentIds.length;
core.info(`Matched deployments are ${shouldDeleteDeploymentCount}\nTheir ids are ${shouldDeleteDeploymentIds.join(",")}`);
if (shouldDeleteDeploymentCount === 0) {
throw new Error("There are no deployments which should be deleted.\nPlease check Vercel Console.");
}
const notDeletedDeploymentIds = [];
// forEach can't use async/await
shouldDeleteDeploymentIds.map(async (deploymentId) => {
let deleteDeploymentRes;
try {
await setTimeout(1000);
// https://vercel.com/docs/rest-api#endpoints/deployments/delete-a-deployment
deleteDeploymentRes = await fetch(`${vercelDeleteDeploymentsEndpoint}/${deploymentId}`, {
headers: {
Authorization: `Bearer ${vercelToken}`,
},
method: "delete",
});
if (!deleteDeploymentRes.ok) {
const resText = await deleteDeploymentRes.text();
throw new Error(resText);
}
}
catch (e) {
core.error(`Status code is ${deleteDeploymentRes?.status} when fetched to delete a deployment.`);
core.error(e);
notDeletedDeploymentIds.push(deploymentId);
}
});
if (notDeletedDeploymentIds.length > 0) {
throw new Error(`There are some deployments which couldn't be deleted.\nThe deployments ids are ${notDeletedDeploymentIds.join(",")}`);
}
core.info(`Success! There are all deleted deployments which ids are ${shouldDeleteDeploymentIds.join(",")}`);
1 change: 1 addition & 0 deletions dist/vercelResponseTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};

0 comments on commit f147f53

Please sign in to comment.