Skip to content

Commit edccdbe

Browse files
committed
Use @actions/cache to accelerate the Action
This opt-out feature uses the cache, so that the files are cached the first time a given artifact from a given buildId is downloaded. Subsequent attempts to download the same artifact with the same buildId should become much quicker as a consequence. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent f6f7847 commit edccdbe

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

__tests__/main.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ if (process.env.RUN_NETWORK_TESTS !== 'true') {
5050
INPUT_FLAVOR: 'minimal',
5151
INPUT_ARCHITECTURE: 'x86_64',
5252
INPUT_PATH: outputDirectory,
53-
INPUT_VERBOSE: '250'
53+
INPUT_VERBOSE: '250',
54+
INPUT_CACHE: 'true'
5455
}
5556
})
5657
).toEqual(0)

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ inputs:
1818
required: false
1919
description: 'Whether to log files as they are extracted'
2020
default: '250'
21+
cache:
22+
required: false
23+
description: 'Use @actions/cache to accelerate this Action'
24+
default: 'true'
2125
runs:
2226
using: 'node12'
2327
main: 'dist/index.js'

main.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as core from '@actions/core'
22
import process from 'process'
33
import {get} from './src/downloader'
4+
import {restoreCache, saveCache} from '@actions/cache'
45

56
async function run(): Promise<void> {
67
try {
@@ -14,15 +15,30 @@ async function run(): Promise<void> {
1415
const architecture = core.getInput('architecture')
1516
const verbose = core.getInput('verbose')
1617

17-
const {artifactName, download} = await get(flavor, architecture)
18+
const {artifactName, download, id} = await get(flavor, architecture)
1819
const outputDirectory = core.getInput('path') || `C:/${artifactName}`
20+
let useCache = core.getInput('cache') === 'true'
21+
22+
try {
23+
if (useCache && (await restoreCache([outputDirectory], id))) {
24+
core.info(`Cached ${id} was successfully restored`)
25+
return
26+
}
27+
} catch (e) {
28+
core.warning(`Cannot use @actions/cache (${e})`)
29+
useCache = false
30+
}
1931

2032
core.info(`Downloading ${artifactName}`)
2133
await download(
2234
outputDirectory,
2335
verbose.match(/^\d+$/) ? parseInt(verbose) : verbose === 'true'
2436
)
2537

38+
if (useCache && !(await saveCache([outputDirectory], id))) {
39+
core.warning(`Failed to cache ${id}`)
40+
}
41+
2642
// Set up PATH so that Git for Windows' SDK's `bash.exe` is found
2743
core.addPath(`${outputDirectory}/usr/bin`)
2844
core.exportVariable(

0 commit comments

Comments
 (0)