-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement built-in cache support (#8)
Resolves #2. Using [@actions/toolkit](https://github.com/actions/toolkit) Scarb cache saving/restoring is now supported out of the box. Cache key is based on sha256 has of Scarb.toml file which is retreived by path returned from `scarb manifest-path`. Right now cache directories paths are hardcoded into the action but in the future there is a possibility to make a command in Scarb that will return cache directory as other package managers such as npm or yarn are doing.
- Loading branch information
Showing
16 changed files
with
122,957 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import * as core from "@actions/core"; | ||
import * as cache from "@actions/cache"; | ||
|
||
import fs from "fs/promises"; | ||
|
||
import { getCacheDirectory, getCacheKey, State } from "./cache-utils"; | ||
|
||
export async function restoreCache() { | ||
const cacheDir = getCacheDirectory(); | ||
await fs.mkdir(cacheDir, { recursive: true }); | ||
|
||
core.info(`Restoring Scarb cache into ${cacheDir}`); | ||
|
||
const primaryKey = await getCacheKey(); | ||
core.info(`Cache primary key is ${primaryKey}`); | ||
core.saveState(State.CachePrimaryKey, primaryKey); | ||
|
||
const matchedKey = await cache.restoreCache([cacheDir], primaryKey); | ||
if (!matchedKey) { | ||
core.info(`Cache entry not found.`); | ||
return; | ||
} | ||
|
||
core.saveState(State.CacheMatchedKey, matchedKey); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import * as core from "@actions/core"; | ||
import * as cache from "@actions/cache"; | ||
|
||
import { getCacheDirectory, State } from "./cache-utils"; | ||
|
||
async function saveCache() { | ||
try { | ||
const primaryKey = core.getState(State.CachePrimaryKey); | ||
const matchedKey = core.getState(State.CacheMatchedKey); | ||
|
||
if (primaryKey !== matchedKey) { | ||
await cache.saveCache([getCacheDirectory()], primaryKey); | ||
} else { | ||
core.info(`Cache hit occurred, not saving cache.`); | ||
} | ||
} catch (e) { | ||
core.error(e); | ||
} | ||
} | ||
|
||
saveCache(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import * as exec from "@actions/exec"; | ||
import * as glob from "@actions/glob"; | ||
|
||
import os from "os"; | ||
import path from "path"; | ||
|
||
export const State = { | ||
CachePrimaryKey: "primary_key", | ||
CacheMatchedKey: "matched_key", | ||
}; | ||
|
||
export function getCacheDirectory() { | ||
const platform = os.platform(); | ||
const home = process.env.HOME; | ||
|
||
switch (platform) { | ||
case "linux": | ||
return path.join(home, ".cache/scarb"); | ||
case "darwin": | ||
return path.join(home, `Library/Caches/com.swmansion.Scarb`); | ||
case "win32": | ||
return path.join(process.env.APPDATA, "swmansion/Scarb/config"); | ||
default: | ||
throw new Error(`Caching not available for ${platform} platform.`); | ||
} | ||
} | ||
|
||
export async function getCacheKey() { | ||
const platform = process.env.RUNNER_OS; | ||
const fileHash = await glob.hashFiles(await getScarbManifestPath()); | ||
|
||
if (!fileHash) { | ||
throw new Error( | ||
"Unable to hash Scarb.toml file, cannot cache dependencies.", | ||
); | ||
} | ||
|
||
return `scarb-cache-${platform}-${fileHash}`.toLowerCase(); | ||
} | ||
|
||
async function getScarbManifestPath() { | ||
const { stdout, exitCode } = await exec.getExecOutput("scarb manifest-path"); | ||
|
||
if (exitCode > 0) { | ||
throw new Error("Unable to resolve Scarb.toml path."); | ||
} | ||
|
||
return stdout.trim(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.