Skip to content

Commit

Permalink
Allows separate caches for cargo.lock and platform. still deduplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
0o-de-lally committed Sep 13, 2023
1 parent 6513834 commit c98204a
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 27 deletions.
20 changes: 17 additions & 3 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ jobs:
run: npm run format-check
- name: ESLint Check
run: npm run lint
- name: Build & Test
run: npm run test
- name: Builds
run: npm run build

test:
name: Test version
Expand Down Expand Up @@ -70,4 +70,18 @@ jobs:

- name: Run sccache for check
shell: bash
run: ${SCCACHE_PATH} --start-server
run: ${SCCACHE_PATH} --show-stats

- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: 1.70.0
override: true

- name: install any cargo
shell: bash
working-directory: ./test-src
run: cargo build



2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
**/target

node_modules/
lib/
# Cache file of eslint
Expand Down
4 changes: 2 additions & 2 deletions dist/post_run/index.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/setup/index.js

Large diffs are not rendered by default.

35 changes: 27 additions & 8 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@actions/core": "^1.10.1",
"@actions/exec": "^1.1.1",
"@actions/github": "^5.1.1",
"@actions/glob": "^0.4.0",
"@actions/io": "^1.1.3",
"@actions/tool-cache": "^2.0.1"
},
Expand Down
64 changes: 55 additions & 9 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,77 @@
import {saveCache, restoreCache} from '@actions/cache';
import * as core from '@actions/core';
import * as gh from '@actions/github';
import * as glob from '@actions/glob';
import fs from 'fs';
import * as crypto from 'crypto';

const key = `sccache-${process.platform}`;

const cargoLockHash = async (): Promise<string> => {
// TODO: have an input for lockfile
// let lockfile = core.getInput('glob-to-lock');
const glob_match = await globFiles(`**/Cargo.lock`);
// console.log(glob_match);
const fileBuffer = await fs.promises.readFile(glob_match[0]);
const hash = crypto.createHash('sha256');
hash.update(fileBuffer);
const hash_string = hash.digest('hex').slice(0, 5).trim();
console.log(hash_string);
return hash_string;
};

async function globFiles(pattern: string): Promise<string[]> {
const globber = await glob.create(pattern, {
followSymbolicLinks: false
});
// fs.statSync resolve the symbolic link and returns stat for the
// file it pointed to, so isFile would make sure the resolved
// file is actually a regular file.
return (await globber.glob()).filter(file => fs.statSync(file).isFile());
}

const makeKey = async (): Promise<string> => {
const hash = await cargoLockHash();
return `${key}-${hash}`;
};

const key = 'sccache';
export const pleaseSave = async () => {
const path = process.env.SCCACHE_CACHE_DIR;
console.log(path);
if (!path) {
console.log(`no sccache dir found in SCCACHE_CACHE_DIR ${path}`);
return;
}
await saveCache([path], key);
await saveCache([path], await makeKey());
};

export const pleaseRestore = async () => {
console.log('restore sccache files');
const path = process.env.SCCACHE_CACHE_DIR;
console.log(path);
console.log(`restoring to: ${path}`);
if (!path) {
console.log(`no sccache dir found in SCCACHE_CACHE_DIR ${path}`);
return;
}
await restoreCache([path], key).then(r => {
if (!r) {
console.log(`no cache matching "${path}" to restore`);
}
});

const exact_restore = await makeKey();
const alt_restore = [key];
console.log(
`searching for exact cache: ${exact_restore}, or alternate matching: ${key}`
);

// restores anything that matches `sccache` if the exact hash is not found
await restoreCache([path], exact_restore, alt_restore)
.then(r => {
if (!r) {
console.log(
`no exising cache matching ${exact_restore} nor "${alt_restore}"`
);
} else {
console.log(`restoring cache: ${r}`);
}
})
.catch(e => console.log(`err: ${e}`));
};

export const deduplicate = async () => {
Expand All @@ -37,7 +83,7 @@ export const deduplicate = async () => {
.deleteActionsCacheByKey({
owner: gh.context.repo.owner,
repo: gh.context.repo.repo,
key
key: await makeKey()
})
.then(() => {
// TODO: more info
Expand Down
6 changes: 4 additions & 2 deletions src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,10 @@ async function setup() {
});
const json = JSON.parse(myOutput);
console.log(`\n${json.cache_location}`);
let cache_path = json.cache_location.split(':')[1].trim().slice(1, -1);

const cache_path = json.cache_location
.split('Local disk: ')[1]
.trim()
.slice(1, -1); // remove quotes
core.exportVariable('SCCACHE_CACHE_DIR', cache_path);

await pleaseRestore();
Expand Down
55 changes: 55 additions & 0 deletions test-src/Cargo.lock

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

9 changes: 9 additions & 0 deletions test-src/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "test-src"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
hashbrown = "0.14.0"
3 changes: 3 additions & 0 deletions test-src/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

0 comments on commit c98204a

Please sign in to comment.