Skip to content

Commit 5c843d9

Browse files
authored
Merge pull request #31 from Trim21/master
embedding cache steps by using @actions/cache
2 parents 7d1e41a + 5a37a08 commit 5c843d9

File tree

4 files changed

+70
-12
lines changed

4 files changed

+70
-12
lines changed

README.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@ jobs:
2424
steps:
2525
- uses: actions/checkout@v1
2626
- uses: actions/setup-python@v1
27-
- name: set PY
28-
run: echo "::set-env name=PY::$(python -c 'import hashlib, sys;print(hashlib.sha256(sys.version.encode()+sys.executable.encode()).hexdigest())')"
29-
- uses: actions/cache@v1
30-
with:
31-
path: ~/.cache/pre-commit
32-
key: pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
3327
- uses: pre-commit/[email protected]
3428
```
3529

index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
1+
const child_process = require('child_process');
2+
const crypto = require('crypto');
3+
const fs = require('fs');
4+
const os = require('os');
5+
const path = require('path');
6+
7+
const cache = require('@actions/cache');
18
const core = require('@actions/core');
29
const exec = require('@actions/exec');
310
const github = require('@actions/github');
411
const tr = require('@actions/exec/lib/toolrunner');
512

13+
function hashString(content) {
14+
const sha256 = crypto.createHash('sha256');
15+
return sha256.update(content).digest('hex');
16+
}
17+
18+
function getPythonVersion() {
19+
const args = ['-c', 'import sys;print(sys.executable+"\\n"+sys.version)'];
20+
const res = child_process.spawnSync('python', args);
21+
if (res.status !== 0) {
22+
throw 'python version check failed';
23+
}
24+
return res.stdout.toString();
25+
}
26+
27+
function hashFile(filePath) {
28+
return hashString(fs.readFileSync(filePath).toString());
29+
}
30+
631
function addToken(url, token) {
732
return url.replace(/^https:\/\//, `https://x-access-token:${token}@`);
833
}
@@ -22,7 +47,14 @@ async function main() {
2247
const token = core.getInput('token');
2348
const pr = github.context.payload.pull_request;
2449
const push = !!token && !!pr;
50+
51+
const cachePaths = [path.join(os.homedir(), '.cache', 'pre-commit')];
52+
const py = getPythonVersion();
53+
const cacheKey = `pre-commit-2-${hashString(py)}-${hashFile('.pre-commit-config.yaml')}`;
54+
await cache.restoreCache(cachePaths, cacheKey);
2555
const ret = await exec.exec('pre-commit', args, {ignoreReturnCode: push});
56+
await cache.saveCache(cachePaths, cacheKey);
57+
2658
if (ret && push) {
2759
// actions do not run on pushes made by actions.
2860
// need to make absolute sure things are good before pushing

package-lock.json

Lines changed: 37 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"private": true,
33
"dependencies": {
4+
"@actions/cache": "*",
45
"@actions/core": "*",
56
"@actions/exec": "*",
67
"@actions/github": "*"

0 commit comments

Comments
 (0)