Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions .github/workflows/algolia.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
name: Algolia
on:
workflow_dispatch:
push:
branches:
- master

jobs:
update_algolia_job:
Expand All @@ -14,7 +11,7 @@ jobs:

- name: Get Algolia config
id: algolia_config
run: echo "::set-output name=config::$(cat algolia_config.json | jq -r tostring)"
run: echo "config=$(cat algolia_config.json | jq -r tostring)" >> "$GITHUB_OUTPUT"

- name: Push indices to Algolia
uses: signcl/docsearch-scraper-action@master
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:
steps:
- uses: actions/checkout@v7

- name: Set Node.js 20.x
- name: Set Node.js
uses: actions/setup-node@v7
with:
node-version: 20.x
node-version-file: .nvmrc

- name: Install Deps
run: yarn install --frozen-lockfile
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20
24.13.0
2 changes: 2 additions & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[[plugins]]
package = "./plugins/trigger-algolia-scraper"
80 changes: 80 additions & 0 deletions plugins/trigger-algolia-scraper/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const MAX_ATTEMPTS = 3;
const RETRY_DELAY_MS = 2_000;

const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

export const onSuccess = async ({ utils }) => {
if (process.env.CONTEXT !== "production") {
console.log("Skipping Algolia scraper trigger — not a production deploy.");
return;
}

const token = process.env.GITHUB_PAT;
if (!token) {
utils.build.failPlugin("GITHUB_PAT environment variable is not set.");
return;
}

console.log("Triggering Algolia scraper workflow...");

for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
let response;
try {
response = await fetch(
"https://api.github.com/repos/absmartly/docs/actions/workflows/algolia.yml/dispatches",
{
method: "POST",
headers: {
Accept: "application/vnd.github+json",
Authorization: `Bearer ${token}`,
"X-GitHub-Api-Version": "2022-11-28",
},
body: JSON.stringify({ ref: "master" }),
signal: AbortSignal.timeout(10_000),
},
);
} catch (error) {
if (attempt < MAX_ATTEMPTS) {
console.warn(
`GitHub API unreachable (attempt ${attempt}/${MAX_ATTEMPTS}): ${error.message}. Retrying...`,
);
await sleep(RETRY_DELAY_MS);
continue;
}
utils.build.failPlugin(
`Could not reach the GitHub API after ${MAX_ATTEMPTS} attempts.`,
{ error },
);
return;
}

if (response.ok) {
console.log("Algolia scraper workflow dispatched successfully.");
utils.status.show({
title: "Algolia scraper triggered",
summary: "The Algolia DocSearch scraper workflow has been dispatched.",
});
return;
}

const body = (await response.text()).slice(0, 200).replace(/\s+/g, " ");
const transient = response.status === 429 || response.status >= 500;
if (transient && attempt < MAX_ATTEMPTS) {
console.warn(
`GitHub API returned ${response.status} (attempt ${attempt}/${MAX_ATTEMPTS}). Retrying...`,
body,
);
await sleep(RETRY_DELAY_MS);
continue;
}

console.error(
`Failed to trigger Algolia scraper: ${response.status} ${response.statusText}`,
body,
);
utils.build.failPlugin(
`GitHub API responded with status ${response.status}. See build log for details.`,
);
return;
}
};
1 change: 1 addition & 0 deletions plugins/trigger-algolia-scraper/manifest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name: trigger-algolia-scraper
3 changes: 3 additions & 0 deletions plugins/trigger-algolia-scraper/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}