From 53df33a21607bb40bbe3b7247434573f23498d51 Mon Sep 17 00:00:00 2001 From: Ming Di Leom <2809763-curben@users.noreply.gitlab.com> Date: Tue, 25 Jan 2022 10:17:28 +0000 Subject: [PATCH] build: failover to github if gitlab is unavailable --- src/build.js | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/src/build.js b/src/build.js index 1c92915..6072ab3 100644 --- a/src/build.js +++ b/src/build.js @@ -11,20 +11,51 @@ const { pipeline } = require('stream/promises') const rootPath = join(__dirname, '..') const tmpPath = join(rootPath, 'tmp') +const publicPath = join(rootPath, 'public') const zipPath = join(tmpPath, 'artifacts.zip') const artifactsUrl = 'https://gitlab.com/curben/tracking-filter/-/jobs/artifacts/main/download?job=pages' +const ghMirror = 'https://nightly.link/curbengh/tracking-filter/workflows/pages/main/public.zip' const f = async () => { + let isMirror = false + await mkdir(tmpPath, { recursive: true }) console.log(`Downloading artifacts.zip from "${artifactsUrl}"`) - await pipeline( - gotStream(artifactsUrl), - createWriteStream(zipPath) - ) + try { + await pipeline( + gotStream(artifactsUrl), + createWriteStream(zipPath) + ) + } catch ({ message }) { + console.error(JSON.stringify({ + error: message, + link: artifactsUrl + })) + + console.log(`Downloading artifacts.zip from "${ghMirror}"`) + isMirror = true + + try { + await pipeline( + gotStream(ghMirror), + createWriteStream(zipPath) + ) + } catch ({ message }) { + throw new Error(JSON.stringify({ + error: message, + link: ghMirror + })) + } + } console.log('Extracting artifacts.zip...') - await unzip(zipPath, { dir: rootPath }) + if (isMirror === false) { + await unzip(zipPath, { dir: rootPath }) + } else { + await mkdir(publicPath, { recursive: true }) + await unzip(zipPath, { dir: publicPath }) + } } f()