add retries to cf pages deploy (#2)

* add retries to cf pages deploy

* fix cloudflare account id setup

* fix export statement again

* fix multiline cmd

* exit 0 on success

* use exits block

* remove husky lines
pull/132/head
Ben Miner 10 months ago committed by GitHub
parent bb92368361
commit f0a512895f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1,4 +1 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
npm run build && git add index.js

2572
index.js

File diff suppressed because one or more lines are too long

1072
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -22,17 +22,17 @@
},
"homepage": "https://github.com/cloudflare/pages-action#readme",
"dependencies": {
"@actions/core": "^1.10.0",
"@actions/core": "^1.10.1",
"@actions/github": "^5.1.1",
"@cloudflare/types": "^6.18.16",
"shellac": "^0.7.2",
"undici": "^5.28.4"
"@cloudflare/types": "^6.29.1",
"shellac": "^0.8.0",
"undici": "^5.11.0"
},
"devDependencies": {
"@types/node": "^20.14.8",
"esbuild": "^0.15.12",
"husky": "^8.0.1",
"prettier": "^2.8.1",
"esbuild": "^0.23.1",
"husky": "^9.1.6",
"prettier": "^3.3.3",
"typescript": "^4.6.4"
}
}

@ -16,6 +16,7 @@ try {
const gitHubToken = getInput("gitHubToken", { required: false });
const branch = getInput("branch", { required: false });
const workingDirectory = getInput("workingDirectory", { required: false });
const maxRetries = getInput("maxRetries", { required: false }) || 5;
const wranglerVersion = getInput("wranglerVersion", { required: false });
const getProject = async () => {
@ -39,14 +40,39 @@ try {
};
const createPagesDeployment = async () => {
const command = `
retries=0
max_retries=${maxRetries}
delay=10
while [ $retries -lt $max_retries ]; do
if npx wrangler@${wranglerVersion} pages publish "${directory}" --project-name="${projectName}" --branch="${branch}"; then
echo "Deploy successful"
exit 0
else
retries=$((retries + 1))
echo "Attempt $retries/$max_retries failed, retrying in $delay seconds..."
sleep $delay
delay=$((delay * 2)) # Exponential backoff
fi
if [ $retries -eq $max_retries ]; then
echo "Max retries reached. Deploy failed."
exit 1
fi
done
`;
// TODO: Replace this with an API call to wrangler so we can get back a full deployment response object
await shellac.in(path.join(process.cwd(), workingDirectory))`
$ export CLOUDFLARE_API_TOKEN="${apiToken}"
if ${accountId} {
$ export CLOUDFLARE_ACCOUNT_ID="${accountId}"
}
$$ npx wrangler@${wranglerVersion} pages publish "${directory}" --project-name="${projectName}" --branch="${branch}"
$ export CLOUDFLARE_API_TOKEN="${apiToken}"
if ${accountId} {
$ export CLOUDFLARE_ACCOUNT_ID="${accountId}"
}
exits {
$$ ${command}
}
`;
const response = await fetch(
@ -143,7 +169,7 @@ try {
let gitHubDeployment: Awaited<ReturnType<typeof createGitHubDeployment>>;
if (gitHubToken && gitHubToken.length) {
if (gitHubToken?.length) {
const octokit = getOctokit(gitHubToken);
gitHubDeployment = await createGitHubDeployment(octokit, productionEnvironment, environmentName);
}

Loading…
Cancel
Save