electron/script/download-circleci-artifacts.js

111 строки
3.5 KiB
JavaScript
Исходник Обычный вид История

2020-03-20 23:28:31 +03:00
const args = require('minimist')(process.argv.slice(2));
const nugget = require('nugget');
const request = require('request');
async function makeRequest (requestOptions, parseResponse) {
return new Promise((resolve, reject) => {
request(requestOptions, (err, res, body) => {
if (!err && res.statusCode >= 200 && res.statusCode < 300) {
if (parseResponse) {
2020-03-20 23:28:31 +03:00
const build = JSON.parse(body);
resolve(build);
} else {
2020-03-20 23:28:31 +03:00
resolve(body);
}
} else {
if (args.verbose) {
2020-03-20 23:28:31 +03:00
console.error('Error occurred while requesting:', requestOptions.url);
if (parseResponse) {
try {
2020-03-20 23:28:31 +03:00
console.log('Error: ', `(status ${res.statusCode})`, err || JSON.parse(res.body), requestOptions);
} catch (err) {
2020-03-20 23:28:31 +03:00
console.log('Error: ', `(status ${res.statusCode})`, err || res.body, requestOptions);
}
} else {
2020-03-20 23:28:31 +03:00
console.log('Error: ', `(status ${res.statusCode})`, err || res.body, requestOptions);
}
}
2020-03-20 23:28:31 +03:00
reject(err);
}
2020-03-20 23:28:31 +03:00
});
});
}
async function downloadArtifact (name, buildNum, dest) {
2020-03-20 23:28:31 +03:00
const circleArtifactUrl = `https://circleci.com/api/v1.1/project/github/electron/electron/${args.buildNum}/artifacts?circle-token=${process.env.CIRCLE_TOKEN}`;
const artifacts = await makeRequest({
method: 'GET',
url: circleArtifactUrl,
headers: {
'Content-Type': 'application/json',
Accept: 'application/json'
}
}, true).catch(err => {
if (args.verbose) {
2020-03-20 23:28:31 +03:00
console.log('Error calling CircleCI:', err);
} else {
2020-03-20 23:28:31 +03:00
console.error('Error calling CircleCI to get artifact details');
}
2020-03-20 23:28:31 +03:00
});
const artifactToDownload = artifacts.find(artifact => {
2020-03-20 23:28:31 +03:00
return (artifact.path === name);
});
if (!artifactToDownload) {
2020-03-20 23:28:31 +03:00
console.log(`Could not find artifact called ${name} to download for build #${buildNum}.`);
process.exit(1);
} else {
2020-03-20 23:28:31 +03:00
console.log(`Downloading ${artifactToDownload.url}.`);
let downloadError = false;
await downloadWithRetry(artifactToDownload.url, dest).catch(err => {
if (args.verbose) {
2020-03-20 23:28:31 +03:00
console.log(`${artifactToDownload.url} could not be successfully downloaded. Error was:`, err);
} else {
2020-03-20 23:28:31 +03:00
console.log(`${artifactToDownload.url} could not be successfully downloaded.`);
}
2020-03-20 23:28:31 +03:00
downloadError = true;
});
if (!downloadError) {
2020-03-20 23:28:31 +03:00
console.log(`Successfully downloaded ${name}.`);
}
}
}
async function downloadWithRetry (url, directory) {
2020-03-20 23:28:31 +03:00
let lastError;
const downloadURL = `${url}?circle-token=${process.env.CIRCLE_TOKEN}`;
for (let i = 0; i < 5; i++) {
2020-03-20 23:28:31 +03:00
console.log(`Attempting to download ${url} - attempt #${(i + 1)}`);
try {
2020-03-20 23:28:31 +03:00
return await downloadFile(downloadURL, directory);
} catch (err) {
2020-03-20 23:28:31 +03:00
lastError = err;
await new Promise((resolve, reject) => setTimeout(resolve, 30000));
}
}
2020-03-20 23:28:31 +03:00
throw lastError;
}
function downloadFile (url, directory) {
return new Promise((resolve, reject) => {
const nuggetOpts = {
dir: directory,
quiet: args.verbose
2020-03-20 23:28:31 +03:00
};
nugget(url, nuggetOpts, (err) => {
if (err) {
2020-03-20 23:28:31 +03:00
reject(err);
} else {
2020-03-20 23:28:31 +03:00
resolve();
}
2020-03-20 23:28:31 +03:00
});
});
}
if (!args.name || !args.buildNum || !args.dest) {
console.log(`Download CircleCI artifacts.
2020-03-20 23:28:31 +03:00
Usage: download-circleci-artifacts.js [--buildNum=CIRCLE_BUILD_NUMBER] [--name=artifactName] [--dest] [--verbose]`);
process.exit(0);
} else {
2020-03-20 23:28:31 +03:00
downloadArtifact(args.name, args.buildNum, args.dest);
}