Improve github-release action

This commit is contained in:
Laurențiu Nicola 2021-12-22 19:56:54 +02:00
parent 45f907ea6f
commit bcf5f407fb
3 changed files with 57 additions and 37 deletions

View file

@ -5,7 +5,7 @@ const github = require('@actions/github');
const glob = require('glob'); const glob = require('glob');
function sleep(milliseconds) { function sleep(milliseconds) {
return new Promise(resolve => setTimeout(resolve, milliseconds)) return new Promise(resolve => setTimeout(resolve, milliseconds));
} }
async function runOnce() { async function runOnce() {
@ -20,9 +20,8 @@ async function runOnce() {
core.info(`files: ${files}`); core.info(`files: ${files}`);
core.info(`name: ${name}`); core.info(`name: ${name}`);
core.info(`token: ${token}`);
const octokit = new github.GitHub(token); const octokit = github.getOctokit(token);
// Delete the previous release since we can't overwrite one. This may happen // Delete the previous release since we can't overwrite one. This may happen
// due to retrying an upload or it may happen because we're doing the dev // due to retrying an upload or it may happen because we're doing the dev
@ -34,24 +33,24 @@ async function runOnce() {
} }
const release_id = release.id; const release_id = release.id;
core.info(`deleting release ${release_id}`); core.info(`deleting release ${release_id}`);
await octokit.repos.deleteRelease({ owner, repo, release_id }); await octokit.rest.repos.deleteRelease({ owner, repo, release_id });
} }
// We also need to update the `dev` tag while we're at it on the `dev` branch. // We also need to update the `dev` tag while we're at it on the `dev` branch.
if (name == 'nightly') { if (name == 'nightly') {
try { try {
core.info(`updating nightly tag`); core.info(`updating nightly tag`);
await octokit.git.updateRef({ await octokit.rest.git.updateRef({
owner, owner,
repo, repo,
ref: 'tags/nightly', ref: 'tags/nightly',
sha, sha,
force: true, force: true,
}); });
} catch (e) { } catch (e) {
console.log("ERROR: ", JSON.stringify(e, null, 2)); core.error(e);
core.info(`creating nightly tag`); core.info(`creating nightly tag`);
await octokit.git.createTag({ await octokit.rest.git.createTag({
owner, owner,
repo, repo,
tag: 'nightly', tag: 'nightly',
@ -65,7 +64,7 @@ async function runOnce() {
// Creates an official GitHub release for this `tag`, and if this is `dev` // Creates an official GitHub release for this `tag`, and if this is `dev`
// then we know that from the previous block this should be a fresh release. // then we know that from the previous block this should be a fresh release.
core.info(`creating a release`); core.info(`creating a release`);
const release = await octokit.repos.createRelease({ const release = await octokit.rest.repos.createRelease({
owner, owner,
repo, repo,
name, name,
@ -73,47 +72,68 @@ async function runOnce() {
target_commitish: sha, target_commitish: sha,
prerelease: name === 'nightly', prerelease: name === 'nightly',
}); });
const release_id = release.data.id;
// Upload all the relevant assets for this release as just general blobs. // Upload all the relevant assets for this release as just general blobs.
for (const file of glob.sync(files)) { for (const file of glob.sync(files)) {
const size = fs.statSync(file).size; const size = fs.statSync(file).size;
core.info(`upload ${file}`); const name = path.basename(file);
await octokit.repos.uploadReleaseAsset({
data: fs.createReadStream(file), await runWithRetry(async function () {
headers: { 'content-length': size, 'content-type': 'application/octet-stream' }, // We can't overwrite assets, so remove existing ones from a previous try.
name: path.basename(file), let assets = await octokit.rest.repos.listReleaseAssets({
url: release.data.upload_url, owner,
repo,
release_id
});
for (const asset of assets.data) {
if (asset.name === name) {
core.info(`delete asset ${name}`);
const asset_id = asset.id;
await octokit.rest.repos.deleteReleaseAsset({ owner, repo, asset_id });
}
}
core.info(`upload ${file}`);
const headers = { 'content-length': size, 'content-type': 'application/octet-stream' };
const data = fs.createReadStream(file);
await octokit.rest.repos.uploadReleaseAsset({
data,
headers,
name,
url: release.data.upload_url,
});
}); });
} }
} }
async function run() { async function runWithRetry(f) {
const retries = 10; const retries = 10;
const maxDelay = 4000;
let delay = 1000;
for (let i = 0; i < retries; i++) { for (let i = 0; i < retries; i++) {
try { try {
await runOnce(); await f();
break; break;
} catch (e) { } catch (e) {
if (i === retries - 1) if (i === retries - 1)
throw e; throw e;
logError(e);
console.log("RETRYING after 10s"); core.error(e);
await sleep(10000) const currentDelay = Math.round(Math.random() * delay);
core.info(`sleeping ${currentDelay} ms`);
await sleep(currentDelay);
delay = Math.min(delay * 2, maxDelay);
} }
} }
} }
function logError(e) { async function run() {
console.log("ERROR: ", e.message); await runWithRetry(runOnce);
try {
console.log(JSON.stringify(e, null, 2));
} catch (e) {
// ignore json errors for now
}
console.log(e.stack);
} }
run().catch(err => { run().catch(err => {
logError(err); core.error(err);
core.setFailed(err.message); core.setFailed(err.message);
}); });

View file

@ -3,8 +3,8 @@
"version": "0.0.0", "version": "0.0.0",
"main": "main.js", "main": "main.js",
"dependencies": { "dependencies": {
"@actions/core": "^1.0.0", "@actions/core": "^1.6",
"@actions/github": "^1.0.0", "@actions/github": "^5.0",
"glob": "^7.1.5" "glob": "^7.1.5"
} }
} }

View file

@ -162,12 +162,12 @@ jobs:
- run: npm ci - run: npm ci
working-directory: editors/code working-directory: editors/code
- name: Publish Extension (release) - name: Package Extension (release)
if: github.ref == 'refs/heads/release' if: github.ref == 'refs/heads/release'
run: npx vsce package -o "../../dist/rust-analyzer-alpine-x64.vsix" --target alpine-x64 run: npx vsce package -o "../../dist/rust-analyzer-alpine-x64.vsix" --target alpine-x64
working-directory: editors/code working-directory: editors/code
- name: Publish Extension (nightly) - name: Package Extension (nightly)
if: github.ref != 'refs/heads/release' if: github.ref != 'refs/heads/release'
run: npx vsce package -o "../../dist/rust-analyzer-alpine-x64.vsix" --target alpine-x64 --pre-release run: npx vsce package -o "../../dist/rust-analyzer-alpine-x64.vsix" --target alpine-x64 --pre-release
working-directory: editors/code working-directory: editors/code