Skip to content

Commit 4ea1f58

Browse files
bors[bot]lnicola
andauthored
Merge #11103
11103: internal: Improve `github-release` action r=lnicola a=lnicola Upgrade ``@actions/github`` to get `listReleaseAssets` and retry individual uploads in addition to the whole thing. Also disables `ci`, `metrics` and `rustdoc` on forks to save some CPU time. CC #11056 Co-authored-by: Laurențiu Nicola <[email protected]>
2 parents 45f907e + 45d2262 commit 4ea1f58

File tree

6 files changed

+64
-39
lines changed

6 files changed

+64
-39
lines changed

.github/actions/github-release/main.js

+53-33
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const github = require('@actions/github');
55
const glob = require('glob');
66

77
function sleep(milliseconds) {
8-
return new Promise(resolve => setTimeout(resolve, milliseconds))
8+
return new Promise(resolve => setTimeout(resolve, milliseconds));
99
}
1010

1111
async function runOnce() {
@@ -20,9 +20,8 @@ async function runOnce() {
2020

2121
core.info(`files: ${files}`);
2222
core.info(`name: ${name}`);
23-
core.info(`token: ${token}`);
2423

25-
const octokit = new github.GitHub(token);
24+
const octokit = github.getOctokit(token);
2625

2726
// Delete the previous release since we can't overwrite one. This may happen
2827
// due to retrying an upload or it may happen because we're doing the dev
@@ -34,24 +33,24 @@ async function runOnce() {
3433
}
3534
const release_id = release.id;
3635
core.info(`deleting release ${release_id}`);
37-
await octokit.repos.deleteRelease({ owner, repo, release_id });
36+
await octokit.rest.repos.deleteRelease({ owner, repo, release_id });
3837
}
3938

4039
// We also need to update the `dev` tag while we're at it on the `dev` branch.
4140
if (name == 'nightly') {
4241
try {
4342
core.info(`updating nightly tag`);
44-
await octokit.git.updateRef({
45-
owner,
46-
repo,
47-
ref: 'tags/nightly',
48-
sha,
49-
force: true,
43+
await octokit.rest.git.updateRef({
44+
owner,
45+
repo,
46+
ref: 'tags/nightly',
47+
sha,
48+
force: true,
5049
});
5150
} catch (e) {
52-
console.log("ERROR: ", JSON.stringify(e, null, 2));
51+
core.error(e);
5352
core.info(`creating nightly tag`);
54-
await octokit.git.createTag({
53+
await octokit.rest.git.createTag({
5554
owner,
5655
repo,
5756
tag: 'nightly',
@@ -65,55 +64,76 @@ async function runOnce() {
6564
// Creates an official GitHub release for this `tag`, and if this is `dev`
6665
// then we know that from the previous block this should be a fresh release.
6766
core.info(`creating a release`);
68-
const release = await octokit.repos.createRelease({
67+
const release = await octokit.rest.repos.createRelease({
6968
owner,
7069
repo,
7170
name,
7271
tag_name: name,
7372
target_commitish: sha,
7473
prerelease: name === 'nightly',
7574
});
75+
const release_id = release.data.id;
7676

7777
// Upload all the relevant assets for this release as just general blobs.
7878
for (const file of glob.sync(files)) {
7979
const size = fs.statSync(file).size;
80-
core.info(`upload ${file}`);
81-
await octokit.repos.uploadReleaseAsset({
82-
data: fs.createReadStream(file),
83-
headers: { 'content-length': size, 'content-type': 'application/octet-stream' },
84-
name: path.basename(file),
85-
url: release.data.upload_url,
80+
const name = path.basename(file);
81+
82+
await runWithRetry(async function () {
83+
// We can't overwrite assets, so remove existing ones from a previous try.
84+
let assets = await octokit.rest.repos.listReleaseAssets({
85+
owner,
86+
repo,
87+
release_id
88+
});
89+
for (const asset of assets.data) {
90+
if (asset.name === name) {
91+
core.info(`delete asset ${name}`);
92+
const asset_id = asset.id;
93+
await octokit.rest.repos.deleteReleaseAsset({ owner, repo, asset_id });
94+
}
95+
}
96+
97+
core.info(`upload ${file}`);
98+
const headers = { 'content-length': size, 'content-type': 'application/octet-stream' };
99+
const data = fs.createReadStream(file);
100+
await octokit.rest.repos.uploadReleaseAsset({
101+
data,
102+
headers,
103+
name,
104+
url: release.data.upload_url,
105+
});
86106
});
87107
}
88108
}
89109

90-
async function run() {
110+
async function runWithRetry(f) {
91111
const retries = 10;
112+
const maxDelay = 4000;
113+
let delay = 1000;
114+
92115
for (let i = 0; i < retries; i++) {
93116
try {
94-
await runOnce();
117+
await f();
95118
break;
96119
} catch (e) {
97120
if (i === retries - 1)
98121
throw e;
99-
logError(e);
100-
console.log("RETRYING after 10s");
101-
await sleep(10000)
122+
123+
core.error(e);
124+
const currentDelay = Math.round(Math.random() * delay);
125+
core.info(`sleeping ${currentDelay} ms`);
126+
await sleep(currentDelay);
127+
delay = Math.min(delay * 2, maxDelay);
102128
}
103129
}
104130
}
105131

106-
function logError(e) {
107-
console.log("ERROR: ", e.message);
108-
try {
109-
console.log(JSON.stringify(e, null, 2));
110-
} catch (e) {
111-
// ignore json errors for now
112-
}
113-
console.log(e.stack);
132+
async function run() {
133+
await runWithRetry(runOnce);
114134
}
115135

116136
run().catch(err => {
117-
logError(err);
137+
core.error(err);
118138
core.setFailed(err.message);
119139
});

.github/actions/github-release/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"version": "0.0.0",
44
"main": "main.js",
55
"dependencies": {
6-
"@actions/core": "^1.0.0",
7-
"@actions/github": "^1.0.0",
6+
"@actions/core": "^1.6",
7+
"@actions/github": "^5.0",
88
"glob": "^7.1.5"
99
}
1010
}

.github/workflows/ci.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ env:
1717

1818
jobs:
1919
rust:
20+
if: github.repository == 'rust-analyzer/rust-analyzer'
2021
name: Rust
2122
runs-on: ${{ matrix.os }}
2223
env:
@@ -61,6 +62,7 @@ jobs:
6162

6263
# Weird targets to catch non-portable code
6364
rust-cross:
65+
if: github.repository == 'rust-analyzer/rust-analyzer'
6466
name: Rust Cross
6567
runs-on: ubuntu-latest
6668

@@ -97,6 +99,7 @@ jobs:
9799
done
98100
99101
typescript:
102+
if: github.repository == 'rust-analyzer/rust-analyzer'
100103
name: TypeScript
101104
strategy:
102105
fail-fast: false

.github/workflows/metrics.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ env:
1212

1313
jobs:
1414
metrics:
15+
if: github.repository == 'rust-analyzer/rust-analyzer'
1516
runs-on: ubuntu-latest
1617

1718
steps:

.github/workflows/release.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,12 @@ jobs:
162162
- run: npm ci
163163
working-directory: editors/code
164164

165-
- name: Publish Extension (release)
165+
- name: Package Extension (release)
166166
if: github.ref == 'refs/heads/release'
167167
run: npx vsce package -o "../../dist/rust-analyzer-alpine-x64.vsix" --target alpine-x64
168168
working-directory: editors/code
169169

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

249249
- name: Publish Extension (release)
250-
if: github.ref == 'refs/heads/release'
250+
if: github.ref == 'refs/heads/release' && github.repository == 'rust-analyzer/rust-analyzer'
251251
working-directory: ./editors/code
252252
# token from https://dev.azure.com/rust-analyzer/
253253
run: npx vsce publish --pat ${{ secrets.MARKETPLACE_TOKEN }} --packagePath ../../dist/rust-analyzer-*.vsix
254254

255255
- name: Publish Extension (nightly)
256-
if: github.ref != 'refs/heads/release'
256+
if: github.ref != 'refs/heads/release' && github.repository == 'rust-analyzer/rust-analyzer'
257257
working-directory: ./editors/code
258258
run: npx vsce publish --pat ${{ secrets.MARKETPLACE_TOKEN }} --packagePath ../../dist/rust-analyzer-*.vsix --pre-release

.github/workflows/rustdoc.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ env:
1212

1313
jobs:
1414
rustdoc:
15+
if: github.repository == 'rust-analyzer/rust-analyzer'
1516
runs-on: ubuntu-latest
1617

1718
steps:

0 commit comments

Comments
 (0)