build: failover to github if gitlab is unavailable

This commit is contained in:
Ming Di Leom 2022-01-25 10:13:27 +00:00
parent 5443e4c918
commit 6424f4fb9a
No known key found for this signature in database
GPG Key ID: 32D3E28E96A695E8
1 changed files with 36 additions and 5 deletions

View File

@ -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/pup-filter/-/jobs/artifacts/main/download?job=pages'
const ghMirror = 'https://nightly.link/curbengh/pup-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()