Skip to content

Implement caching #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
28 changes: 26 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,34 @@ jobs:

steps:
- uses: actions/checkout@main
- uses: './'

- name: Build Lua
uses: './'
with:
luaVersion: ${{ matrix.luaVersion }}
buildCache: false

- name: test lua
- name: Test Lua
run: lua -e 'print("hi from lua")'

test-cache:
needs: test

strategy:
fail-fast: false
matrix:
luaVersion: ["5.1.5", "5.2.4", "5.3.5", "5.4.1", "luajit-2.0.5", "luajit-2.1.0-beta3", "luajit-openresty", "5.1", "5.4"]
machineTag: ["ubuntu-latest", "macos-latest"]

runs-on: ${{ matrix.machineTag }}

steps:
- uses: actions/checkout@main

- name: Get Lua from cache
uses: './'
with:
luaVersion: ${{ matrix.luaVersion }}

- name: Test Lua
run: lua -e 'print("hi from lua")'
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ inputs:
luaCompileFlags:
description: "Command line flags passed to make when building Lua"
required: false
buildCache:
description: "Set to 'false' to skip restoring .lua from cache and always compile it"
required: false
default: 'true'

runs:
using: 'node12'
Expand Down
94 changes: 65 additions & 29 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const core = require("@actions/core")
const exec = require("@actions/exec")
const io = require("@actions/io")
const tc = require("@actions/tool-cache")
const ch = require("@actions/cache")
const fsp = require("fs").promises

const path = require("path")

Expand All @@ -19,8 +21,7 @@ const VERSION_ALIASES = {

const isMacOS = () => (process.platform || "").startsWith("darwin")

async function install_luajit_openresty() {
const luaInstallPath = path.join(process.cwd(), LUA_PREFIX)
async function install_luajit_openresty(luaInstallPath) {
const installPath = path.join(process.cwd(), INSTALL_PREFIX)
const luaCompileFlags = core.getInput('luaCompileFlags')

Expand Down Expand Up @@ -48,17 +49,13 @@ async function install_luajit_openresty() {
cwd: path.join(installPath, "luajit2")
})


core.addPath(path.join(luaInstallPath, "bin"));

await exec.exec("ln -s luajit lua", undefined, {
cwd: path.join(luaInstallPath, "bin")
})
}

async function install_luajit(luajitVersion) {
async function install_luajit(luaInstallPath, luajitVersion) {
const luaExtractPath = path.join(process.cwd(), INSTALL_PREFIX, `LuaJIT-${luajitVersion}`)
const luaInstallPath = path.join(process.cwd(), LUA_PREFIX)

const luaCompileFlags = core.getInput('luaCompileFlags')

Expand All @@ -84,33 +81,14 @@ async function install_luajit(luajitVersion) {
cwd: luaExtractPath
})

core.addPath(path.join(luaInstallPath, "bin"));

await exec.exec(`ln -s luajit-${luajitVersion} lua`, undefined, {
cwd: path.join(luaInstallPath, "bin")
})

}

async function main() {
let luaVersion = core.getInput('luaVersion', { required: true })
let luaCompileFlags = core.getInput('luaCompileFlags')

if (VERSION_ALIASES[luaVersion]) {
luaVersion = VERSION_ALIASES[luaVersion]
}

if (luaVersion == "luajit-openresty") {
return await install_luajit_openresty()
}

if (luaVersion.startsWith("luajit-")) {
const luajitVersion = luaVersion.substr("luajit-".length)
return await install_luajit(luajitVersion)
}

async function install_plain_lua(luaInstallPath, luaVersion) {
const luaExtractPath = path.join(process.cwd(), INSTALL_PREFIX, `lua-${luaVersion}`)
const luaInstallPath = path.join(process.cwd(), LUA_PREFIX)
const luaCompileFlags = core.getInput('luaCompileFlags')

const luaSourceTar = await tc.downloadTool(`https://www.lua.org/ftp/lua-${luaVersion}.tar.gz`)
await io.mkdirP(luaExtractPath)
Expand Down Expand Up @@ -144,8 +122,66 @@ async function main() {
await exec.exec(`make -j INSTALL_TOP="${luaInstallPath}" install`, undefined, {
cwd: luaExtractPath
})
}

async function install(luaInstallPath, luaVersion) {
if (luaVersion == "luajit-openresty") {
return await install_luajit_openresty(luaInstallPath)
}

if (luaVersion.startsWith("luajit-")) {
const luajitVersion = luaVersion.substr("luajit-".length)
return await install_luajit(luaInstallPath, luajitVersion)
}

return await install_plain_lua(luaInstallPath, luaVersion)
}

const makeCacheKey = luaVersion=> `setup-lua-${luaVersion}-${process.platform}-${process.arch}`
const exists = (filename, mode) => fsp.access(filename, mode).then(() => true, () => false)

async function main() {
let luaVersion = core.getInput('luaVersion', { required: true })
let luaCompileFlags = core.getInput('luaCompileFlags')

if (VERSION_ALIASES[luaVersion]) {
luaVersion = VERSION_ALIASES[luaVersion]
}

const luaInstallPath = path.join(process.cwd(), LUA_PREFIX)

// The tool cache is mostly useful on self-hosted runners. It doesn't persist across jobs in Github's runners
let toolCacheDir = tc.find('lua', luaVersion)

if (!toolCacheDir) {
const cacheKey = makeCacheKey(luaVersion)
if (core.getInput('buildCache') == 'true') {
await ch.restoreCache([luaInstallPath], cacheKey) // @actions/cache does persist across jobs
}

if (!(await exists(luaInstallPath))) {
await install(luaInstallPath, luaVersion)
try {
await ch.saveCache([luaInstallPath], cacheKey)
} catch (ex) {
// This could happen due to a race condition, in which case it should be safe to ignore saving the cache
if (ex instanceof ch.ReserveCacheError) {
console.log(`Cannot save cache. (${ex.message}) Skipping...`)
} else {
throw ex
}
}
}

toolCacheDir = await tc.cacheDir(luaInstallPath, 'lua', luaVersion)
}

// If .lua doesn't exist, symlink it to the tool cache dir
if (toolCacheDir && !(await exists(luaInstallPath))) {
await fsp.symlink(toolCacheDir, luaInstallPath);
}

core.addPath(path.join(luaInstallPath, "bin"));
core.addPath(path.join(luaInstallPath, "bin"))
}

main().catch(err => {
Expand Down
9 changes: 9 additions & 0 deletions node_modules/@actions/cache/LICENSE.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions node_modules/@actions/cache/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions node_modules/@actions/cache/lib/cache.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading