Use github cache (#53)

* Cache the zig compiler locally

* logging

* npm update

* verboser

* os.arch

* debug

* log signal

* address https://github.com/actions/toolkit/issues/687

* correct path

* add a cache: false option

* share size

* zigpath

* path.join skull
This commit is contained in:
Renée 2023-06-25 17:44:29 +02:00 committed by GitHub
parent e24fd328aa
commit bc506fbfd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 57880 additions and 1851 deletions

View File

@ -48,6 +48,18 @@ If you are running Zig on Windows machines, you need to make sure that your .zig
*.zig text eol=lf *.zig text eol=lf
``` ```
This action caches the downloaded compilers in your repository's Actions cache by default,
to reduce the load on the Zig Foundation's servers. Cached compilers are only about 60MB
each per version/OS/architecture.
If this is really bad for you for some reason you can disable the caching.
```yaml
- uses: goto-bus-stop/setup-zig@v2
with:
cache: false
```
## License ## License
[Apache-2.0](LICENSE.md) [Apache-2.0](LICENSE.md)

View File

@ -9,6 +9,10 @@ inputs:
description: 'Version of the zig compiler to use (must be 0.3.0 or up)' description: 'Version of the zig compiler to use (must be 0.3.0 or up)'
required: true required: true
default: 'master' default: 'master'
cache:
description: 'Cache downloaded compilers for faster action runs. Strongly recommended.'
required: false
default: 'true'
runs: runs:
using: 'node16' using: 'node16'
main: 'dist/index.js' main: 'dist/index.js'

59673
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@ -4,7 +4,8 @@ const os = require('os')
const path = require('path') const path = require('path')
const semver = require('semver') const semver = require('semver')
const actions = require('@actions/core') const actions = require('@actions/core')
const cache = require('@actions/tool-cache') const cache = require('@actions/cache')
const toolCache = require('@actions/tool-cache')
const { const {
extForPlatform, extForPlatform,
resolveCommit, resolveCommit,
@ -13,40 +14,60 @@ const {
const TOOL_NAME = 'zig' const TOOL_NAME = 'zig'
async function downloadZig (platform, version) { async function downloadZig (platform, version, useCache = true) {
const ext = extForPlatform(platform) const ext = extForPlatform(platform)
const { downloadUrl, variantName, version: useVersion } = version.includes('+') const { downloadUrl, variantName, version: useVersion } = version.includes('+')
? resolveCommit(platform, version) ? resolveCommit(platform, version)
: await resolveVersion(platform, version) : await resolveVersion(platform, version)
const cachedPath = cache.find(TOOL_NAME, useVersion) const cachedPath = toolCache.find(TOOL_NAME, useVersion)
if (cachedPath) { if (cachedPath) {
actions.info(`using cached zig install: ${cachedPath}`) actions.info(`using cached zig install: ${cachedPath}`)
return cachedPath return cachedPath
} }
const cacheKey = `${TOOL_NAME}-${variantName}`
if (useCache) {
const restorePath = path.join(process.env.RUNNER_TOOL_CACHE, TOOL_NAME, useVersion, os.arch())
actions.info(`attempting restore of ${cacheKey} to ${restorePath}`)
const restoredKey = await cache.restoreCache([restorePath], cacheKey)
if (restoredKey) {
actions.info(`using cached zig install: ${restorePath}`)
return restorePath
}
}
actions.info(`no cached version found. downloading zig ${variantName}`) actions.info(`no cached version found. downloading zig ${variantName}`)
const downloadPath = await cache.downloadTool(downloadUrl) const downloadPath = await toolCache.downloadTool(downloadUrl)
const zigPath = ext === 'zip' const zigPath = ext === 'zip'
? await cache.extractZip(downloadPath) ? await toolCache.extractZip(downloadPath)
: await cache.extractTar(downloadPath, undefined, 'x') : await toolCache.extractTar(downloadPath, undefined, 'x')
const binPath = path.join(zigPath, variantName) const binPath = path.join(zigPath, variantName)
const cachePath = await cache.cacheDir(binPath, TOOL_NAME, useVersion) const cachePath = await toolCache.cacheDir(binPath, TOOL_NAME, useVersion)
actions.info(`added zig ${useVersion} to the tool cache`)
if (useCache) {
actions.info(`adding zig ${useVersion} at ${cachePath} to local cache ${cacheKey}`)
await cache.saveCache([cachePath], cacheKey)
}
return cachePath return cachePath
} }
async function main () { async function main () {
const version = actions.getInput('version') || 'master' const version = actions.getInput('version') || 'master'
const useCache = actions.getInput('cache') || 'true'
if (semver.valid(version) && semver.lt(version, '0.3.0')) { if (semver.valid(version) && semver.lt(version, '0.3.0')) {
actions.setFailed('This action does not work with Zig 0.1.0 and Zig 0.2.0') actions.setFailed('This action does not work with Zig 0.1.0 and Zig 0.2.0')
return return
} }
if (useCache !== 'false' && useCache !== 'true') {
actions.setFailed('`with.cache` must be "true" or "false"')
return
}
const zigPath = await downloadZig(os.platform(), version) const zigPath = await downloadZig(os.platform(), version, Boolean(useCache))
// Add the `zig` binary to the $PATH // Add the `zig` binary to the $PATH
actions.addPath(zigPath) actions.addPath(zigPath)

View File

@ -7,13 +7,14 @@
"url": "https://github.com/goto-bus-stop/setup-zig/issues" "url": "https://github.com/goto-bus-stop/setup-zig/issues"
}, },
"dependencies": { "dependencies": {
"@actions/cache": "^3.2.1",
"@actions/core": "^1.2.2", "@actions/core": "^1.2.2",
"@actions/tool-cache": "^2.0.1", "@actions/tool-cache": "^2.0.1",
"semver": "^7.1.3", "semver": "^7.1.3",
"simple-get": "^4.0.0" "simple-get": "^4.0.0"
}, },
"devDependencies": { "devDependencies": {
"esbuild": "^0.17.19", "esbuild": "^0.18.8",
"standard": "^17.0.0" "standard": "^17.0.0"
}, },
"homepage": "https://github.com/goto-bus-stop/setup-zig", "homepage": "https://github.com/goto-bus-stop/setup-zig",
@ -30,7 +31,7 @@
"url": "https://github.com/goto-bus-stop/setup-zig.git" "url": "https://github.com/goto-bus-stop/setup-zig.git"
}, },
"scripts": { "scripts": {
"prepare": "esbuild index.js --outdir=dist --bundle --platform=node", "prepare": "esbuild index.js --outdir=dist --keep-names --bundle --platform=node --target=node16",
"test": "standard && node test" "test": "standard && node test"
}, },
"standard": { "standard": {