Skip to content

Commit 90c992c

Browse files
committed
Added support for uncompressed artifacts and selective formats.
1 parent 5f6f489 commit 90c992c

File tree

1 file changed

+35
-31
lines changed

1 file changed

+35
-31
lines changed

bin/save-to-github-cache.js

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
'use strict';
44

5+
const {EOL} = require('os');
56
const {promises: fsp} = require('fs');
67
const path = require('path');
78
const zlib = require('zlib');
@@ -91,7 +92,12 @@ const withParams = (url, params) => {
9192

9293
const artifactPath = getParam('artifact'),
9394
prefix = getParam('prefix'),
94-
suffix = getParam('suffix');
95+
suffix = getParam('suffix'),
96+
format = getParam('format', 'br'),
97+
requestedFormats = new Set(...format.toLowerCase().split(/\s*,\s*/)),
98+
skipBrotli = !zlib.brotliCompress || !requestedFormats.has('br'),
99+
skipGzip = !zlib.gzip || !requestedFormats.has('gz'),
100+
skipUncompressed = !requestedFormats.has('none');
95101

96102
const main = async () => {
97103
const [OWNER, REPO] = process.env.GITHUB_REPOSITORY.split('/'),
@@ -119,54 +125,52 @@ const main = async () => {
119125
})
120126
]);
121127

128+
const postArtifact = (name, label, data, contentType = 'application/octet-stream') =>
129+
post(
130+
withParams(uploadUrl, {name, label}),
131+
{
132+
auth: TOKEN ? OWNER + ':' + TOKEN : null,
133+
headers: {
134+
Accept: 'application/vnd.github.v3+json',
135+
'Content-Type': contentType,
136+
'Content-Length': data.length,
137+
'User-Agent': 'uhop/install-artifact-from-github',
138+
Authorization: !TOKEN && PERSONAL_TOKEN ? 'Bearer ' + PERSONAL_TOKEN : null
139+
}
140+
},
141+
data
142+
);
143+
122144
console.log('Compressing and uploading ...');
123145

124146
await Promise.all([
125147
(async () => {
126-
if (!zlib.brotliCompress) return null;
148+
if (skipBrotli) return null;
127149
const compressed = await promisify(zlib.brotliCompress)(data, {params: {[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY}}),
128150
name = fileName + '.br',
129151
label = `Binary artifact: ${artifactPath} (${platform}, ${process.arch}, ${process.versions.modules}, brotli).`;
130-
return post(
131-
withParams(uploadUrl, {name, label}),
132-
{
133-
auth: TOKEN ? OWNER + ':' + TOKEN : null,
134-
headers: {
135-
Accept: 'application/vnd.github.v3+json',
136-
'Content-Type': 'application/brotli',
137-
'Content-Length': compressed.length,
138-
'User-Agent': 'uhop/install-artifact-from-github',
139-
Authorization: !TOKEN && PERSONAL_TOKEN ? 'Bearer ' + PERSONAL_TOKEN : null
140-
}
141-
},
142-
compressed
143-
)
152+
return postArtifact(name, label, compressed, 'application/brotli')
144153
.then(({res}) => console.log('Uploaded BR:', res.statusCode))
145154
.catch(error => console.error('BR has failed to upload:', error));
146155
})(),
147156
(async () => {
148-
if (!zlib.gzip) return null;
157+
if (skipGzip) return null;
149158
const compressed = await promisify(zlib.gzip)(data, {level: zlib.constants.Z_BEST_COMPRESSION}),
150159
name = fileName + '.gz',
151160
label = `Binary artifact: ${artifactPath} (${platform}, ${process.arch}, ${process.versions.modules}, gzip).`;
152-
return post(
153-
withParams(uploadUrl, {name, label}),
154-
{
155-
auth: TOKEN ? OWNER + ':' + TOKEN : null,
156-
headers: {
157-
Accept: 'application/vnd.github.v3+json',
158-
'Content-Type': 'application/gzip',
159-
'Content-Length': compressed.length,
160-
'User-Agent': 'uhop/install-artifact-from-github',
161-
Authorization: !TOKEN && PERSONAL_TOKEN ? 'Bearer ' + PERSONAL_TOKEN : null
162-
}
163-
},
164-
compressed
165-
)
161+
return postArtifact(name, label, compressed, 'application/gzip')
166162
.then(({res}) => console.log('Uploaded GZ:', res.statusCode))
167163
.catch(error => console.error('GZ has failed to upload:', error));
164+
})(),
165+
(async () => {
166+
if (skipUncompressed) return null;
167+
const label = `Binary artifact: ${artifactPath} (${platform}, ${process.arch}, ${process.versions.modules}, uncompressed).`;
168+
return postArtifact(fileName, label, data)
169+
.then(({res}) => console.log('Uploaded Uncompressed:', res.statusCode))
170+
.catch(error => console.error('Uncompressed has failed to upload:', error));
168171
})()
169172
]);
173+
if (process.env.GITHUB_ENV) await fsp.appendFile(process.env.GITHUB_ENV, 'CREATED_ASSET_NAME=' + fileName + EOL);
170174
console.log('Done.');
171175
};
172176

0 commit comments

Comments
 (0)