Skip to content

Commit

Permalink
feat: cache the bin
Browse files Browse the repository at this point in the history
Signed-off-by: Dwi Siswanto <me@dw1.io>
  • Loading branch information
dwisiswant0 committed Mar 21, 2024
1 parent 51063f6 commit 251b9d6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 33 deletions.
1 change: 1 addition & 0 deletions src/const.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const cache = require('@actions/cache');
export const core = require('@actions/core');
export const fs = require('fs');
export const https = require('https');
Expand Down
89 changes: 56 additions & 33 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
core, fs, https, io, os,
path, shell, TMP_DIR
cache, core, fs, https, io,
os, path, shell, TMP_DIR
} from './const';

const inputOverwrite = core.getBooleanInput('overwrite', {required: false});
Expand Down Expand Up @@ -46,7 +46,7 @@ async function get_slim() {
VER = inputVersion;
}
} catch {
throw new Error('ERROR! Could not retrieve the current Slim version number.');
throw new Error(`ERROR! Could not get the Slim version ${VER}.`);
}

URL = `https://downloads.dockerslim.com/releases/${VER}`;
Expand Down Expand Up @@ -84,40 +84,63 @@ async function get_slim() {
// Derive the filename
FILENAME = `dist_${DIST}.${EXT}`;

const file = fs.createWriteStream(path.join(TMP_DIR, FILENAME));
await new Promise((resolve, reject) => {
https.get(`${URL}/${FILENAME}`, (response) => {
response.pipe(file);
file.on('finish', () => {
file.close();
resolve(file);
// Get the bin from cache
const cachePrefix = `slim-${VER}`;
const cacheKey = `${cachePrefix}-${DIST}`;
const cachePath = path.join('/tmp', cacheKey);

try {
const cacheResult = await cache.restoreCache(
[ cachePath ], cacheKey, [ `${cachePrefix}-` ]
);

if (typeof cacheResult === 'undefined') {
throw new Error(`ERROR! Cache miss: ${cacheKey} was not found in the cache.`)
}

core.debug(`${cacheKey} cache was restored.`)

SLIM_PATH = cachePath;
} catch (e) {
const file = fs.createWriteStream(path.join(TMP_DIR, FILENAME));
await new Promise((resolve, reject) => {
https.get(`${URL}/${FILENAME}`, (response) => {
response.pipe(file);
file.on('finish', () => {
file.close();
resolve(file);
});
}).on('error', (error) => {
fs.unlinkSync(path.join(TMP_DIR, FILENAME));
reject(error);
});
}).on('error', (error) => {
fs.unlinkSync(path.join(TMP_DIR, FILENAME));
reject(error);
});
});

core.debug(`Unpacking ${path.join(TMP_DIR, FILENAME)}`);
if (EXT === 'zip') {
const extract = require('extract-zip');
await extract(path.join(TMP_DIR, FILENAME), {
dir: TMP_DIR
});
} else if (EXT === 'tar.gz') {
const tar = require('tar');
await tar.x({
file: path.join(TMP_DIR, FILENAME),
cwd: TMP_DIR
});
} else {
throw new Error('ERROR! Unexpected file extension.');
}
core.debug(`Unpacking ${path.join(TMP_DIR, FILENAME)}`);
if (EXT === 'zip') {
const extract = require('extract-zip');
await extract(path.join(TMP_DIR, FILENAME), {
dir: TMP_DIR
});
} else if (EXT === 'tar.gz') {
const tar = require('tar');
await tar.x({
file: path.join(TMP_DIR, FILENAME),
cwd: TMP_DIR
});
} else {
throw new Error('ERROR! Unexpected file extension.');
}

SLIM_PATH = path.join(TMP_DIR, `dist_${DIST}`);
core.addPath(SLIM_PATH);
SLIM_PATH = path.join(TMP_DIR, `dist_${DIST}`);

core.info(`Using slim version ${VER}`);
const cacheId = await cache.saveCache([ cachePath ], cacheKey);

core.debug(`${cacheKey} cache saved (ID: ${cacheId})`)
} finally {
core.addPath(SLIM_PATH);
core.info(`Using slim version ${VER}`);
}
}

async function run() {
Expand All @@ -143,7 +166,7 @@ async function run() {
throw new Error('ERROR! Cannot build over target');
}

const [image, tag] = report.target_reference.split(':')
const [image, tag] = report.target_reference.split(':');

if (inputOverwrite && tag) {
core.info(`Overwriting ${image}:${tag} with slimmed version`);
Expand Down

0 comments on commit 251b9d6

Please sign in to comment.