Skip to content

Commit

Permalink
try hashing cache
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 acd8c11
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 20 deletions.
21 changes: 18 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,19 @@ 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
run: |
echo $RUSTC_WRAPPER
cargo install hashbrown
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
35 changes: 32 additions & 3 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
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';

const cargoLockHash = async (): Promise<string> => {
const file_to_hash = await globFiles('**/*.lock');
console.log(file_to_hash);
const fileBuffer = await fs.promises.readFile(file_to_hash[0]);
const hash = crypto.createHash('sha256');
hash.update(fileBuffer);
return hash.digest('hex');
};

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}`;
};

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 () => {
Expand All @@ -21,7 +49,8 @@ export const pleaseRestore = async () => {
console.log(`no sccache dir found in SCCACHE_CACHE_DIR ${path}`);
return;
}
await restoreCache([path], key).then(r => {
// restores anything that matches `sccache` if the exact hash is not found
await restoreCache([path], await makeKey(), [key]).then(r => {
if (!r) {
console.log(`no cache matching "${path}" to restore`);
}
Expand All @@ -37,7 +66,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
2 changes: 1 addition & 1 deletion src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ 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(':')[1].trim().slice(1, -1);

core.exportVariable('SCCACHE_CACHE_DIR', cache_path);

Expand Down

0 comments on commit acd8c11

Please sign in to comment.