Skip to content

Commit

Permalink
Chore: download dist from GitLab if GitHub dist repo is down
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Sep 4, 2024
1 parent affaa13 commit 386f243
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 42 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ jobs:
run: |
mkdir ./deploy-git
cd ./deploy-git
git init
git config --global init.defaultBranch master
git init
git config --global push.default matching
git config --global user.email "${GITLAB_EMAIL}"
git config --global user.name "${GITLAB_USER}"
Expand All @@ -95,6 +95,7 @@ jobs:
GITLAB_TOKEN: ${{ secrets.GITLAB_TOKEN }}
- name: Upload Dist to GitHub
uses: peaceiris/actions-gh-pages@v4
continue-on-error: true
with:
personal_token: ${{ secrets.GIT_TOKEN }}
user_name: ${{ secrets.GIT_USER }}
Expand Down
94 changes: 53 additions & 41 deletions Build/download-previous-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,54 +49,66 @@ export const downloadPreviousBuild = task(require.main === module, __filename)(a

const filesList = buildOutputList.map(f => path.join('ruleset.skk.moe-master', f));

return span
.traceChild('download & extract previoud build')
.traceAsyncFn(async () => {
const resp = await fetchWithRetry('https://codeload.github.com/sukkalab/ruleset.skk.moe/tar.gz/master', {
...defaultRequestInit,
retry: {
retryOnNon2xx: false
}
});

if (resp.status !== 200) {
console.warn('Download previous build failed! Status:', resp.status);
if (resp.status === 404) {
return;
}
const tarGzUrl = await span.traceChildAsync('get tar.gz url', async () => {
const resp = await fetchWithRetry('https://codeload.github.com/sukkalab/ruleset.skk.moe/tar.gz/master', {
...defaultRequestInit,
method: 'HEAD',
retry: {
retryOnNon2xx: false
}

if (!resp.body) {
throw new Error('Download previous build failed! No body found');
});
if (resp.status !== 200) {
return 'https://gitlab.com/SukkaW/ruleset.skk.moe/-/archive/master/ruleset.skk.moe-master.tar.gz';
}
return 'https://codeload.github.com/sukkalab/ruleset.skk.moe/tar.gz/master';
});

return span.traceChildAsync('download & extract previoud build', async () => {
const resp = await fetchWithRetry(tarGzUrl, {
...defaultRequestInit,
retry: {
retryOnNon2xx: false
}
});

const gunzip = zlib.createGunzip();
const extract = tarStream.extract();
if (resp.status !== 200) {
console.warn('Download previous build failed! Status:', resp.status);
if (resp.status === 404) {
return;
}
}

pipeline(
Readable.fromWeb(resp.body),
gunzip,
extract
);
if (!resp.body) {
throw new Error('Download previous build failed! No body found');
}

const pathPrefix = `ruleset.skk.moe-master${path.sep}`;
const gunzip = zlib.createGunzip();
const extract = tarStream.extract();

for await (const entry of extract) {
if (entry.header.type !== 'file') {
entry.resume(); // Drain the entry
continue;
}
// filter entry
if (!filesList.some(f => entry.header.name.startsWith(f))) {
entry.resume(); // Drain the entry
continue;
}
pipeline(
Readable.fromWeb(resp.body),
gunzip,
extract
);

const relativeEntryPath = entry.header.name.replace(pathPrefix, '');
const targetPath = path.join(__dirname, '..', relativeEntryPath);
const pathPrefix = 'ruleset.skk.moe-master/';

await mkdir(path.dirname(targetPath), { recursive: true });
await pipeline(entry, createWriteStream(targetPath));
for await (const entry of extract) {
if (entry.header.type !== 'file') {
entry.resume(); // Drain the entry
continue;
}
});
// filter entry
if (!filesList.some(f => entry.header.name.startsWith(f))) {
entry.resume(); // Drain the entry
continue;
}

const relativeEntryPath = entry.header.name.replace(pathPrefix, '');
const targetPath = path.join(__dirname, '..', relativeEntryPath);

await mkdir(path.dirname(targetPath), { recursive: true });
await pipeline(entry, createWriteStream(targetPath));
}
});
});

0 comments on commit 386f243

Please sign in to comment.