Created
February 19, 2025 08:59
-
-
Save viazure/6750aba76e92fcfe0e25d74f229700b1 to your computer and use it in GitHub Desktop.
Trigger the GitHub Actions for the running-page project manually via webhook to update the data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @typedef {Object} Env | |
*/ | |
export default { | |
/** | |
* @param {Request} request | |
* @param {Env} env | |
* @param {ExecutionContext} ctx | |
* @returns {Promise<Response>} | |
*/ | |
async fetch(request, env, ctx) { | |
const url = new URL(request.url); | |
const sync_token = env.SYNC_TOKEN; | |
if (url.pathname !== `/${sync_token}`) { | |
return new Response(":(", { | |
headers: { "content-type": "text/plain" }, | |
status: 401, | |
}); | |
} | |
const github_owener = env.GITHUB_OWENER_NAME; | |
const github_repo = env.GITHUB_REPO_NAME; | |
const github_token = env.GITHUB_TOKEN; | |
const github_workflow_id = env.GITHUB_WORKFLOW_ID; | |
const ghBody = { | |
ref: "master", | |
}; | |
const ghReq = { | |
headers: { | |
"Content-Type": "application/json", | |
"User-Agent": `Cloudflare - ${github_owener}`, | |
Authorization: `Bearer ${github_token}`, | |
}, | |
method: "POST", | |
body: JSON.stringify(ghBody), | |
}; | |
const ghUrl = `https://api.github.com/repos/${github_owener}/${github_repo}/actions/workflows/${github_workflow_id}/dispatches`; | |
const ghResp = await fetch(ghUrl, ghReq); | |
if (ghResp.status !== 204) { | |
console.log("Request: ", ghReq); | |
console.log("Response: ", ghResp); | |
return new Response(":(", { | |
headers: { "content-type": "text/plain" }, | |
status: ghResp.status, | |
}); | |
} | |
return new Response(":)", { | |
headers: { "content-type": "text/plain" }, | |
status: 200, | |
}); | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment