5
0
Fork 0
mirror of https://github.com/pre-commit/action.git synced 2025-11-07 10:46:56 +00:00

Merge pull request #31 from Trim21/master

embedding cache steps by using @actions/cache
This commit is contained in:
Anthony Sottile 2020-06-13 12:48:50 -07:00 committed by GitHub
commit 5c843d930a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 12 deletions

View file

@ -24,12 +24,6 @@ jobs:
steps: steps:
- uses: actions/checkout@v1 - uses: actions/checkout@v1
- uses: actions/setup-python@v1 - uses: actions/setup-python@v1
- name: set PY
run: echo "::set-env name=PY::$(python -c 'import hashlib, sys;print(hashlib.sha256(sys.version.encode()+sys.executable.encode()).hexdigest())')"
- uses: actions/cache@v1
with:
path: ~/.cache/pre-commit
key: pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
- uses: pre-commit/action@v1.1.1 - uses: pre-commit/action@v1.1.1
``` ```

View file

@ -1,8 +1,33 @@
const child_process = require('child_process');
const crypto = require('crypto');
const fs = require('fs');
const os = require('os');
const path = require('path');
const cache = require('@actions/cache');
const core = require('@actions/core'); const core = require('@actions/core');
const exec = require('@actions/exec'); const exec = require('@actions/exec');
const github = require('@actions/github'); const github = require('@actions/github');
const tr = require('@actions/exec/lib/toolrunner'); const tr = require('@actions/exec/lib/toolrunner');
function hashString(content) {
const sha256 = crypto.createHash('sha256');
return sha256.update(content).digest('hex');
}
function getPythonVersion() {
const args = ['-c', 'import sys;print(sys.executable+"\\n"+sys.version)'];
const res = child_process.spawnSync('python', args);
if (res.status !== 0) {
throw 'python version check failed';
}
return res.stdout.toString();
}
function hashFile(filePath) {
return hashString(fs.readFileSync(filePath).toString());
}
function addToken(url, token) { function addToken(url, token) {
return url.replace(/^https:\/\//, `https://x-access-token:${token}@`); return url.replace(/^https:\/\//, `https://x-access-token:${token}@`);
} }
@ -22,7 +47,14 @@ async function main() {
const token = core.getInput('token'); const token = core.getInput('token');
const pr = github.context.payload.pull_request; const pr = github.context.payload.pull_request;
const push = !!token && !!pr; const push = !!token && !!pr;
const cachePaths = [path.join(os.homedir(), '.cache', 'pre-commit')];
const py = getPythonVersion();
const cacheKey = `pre-commit-2-${hashString(py)}-${hashFile('.pre-commit-config.yaml')}`;
await cache.restoreCache(cachePaths, cacheKey);
const ret = await exec.exec('pre-commit', args, {ignoreReturnCode: push}); const ret = await exec.exec('pre-commit', args, {ignoreReturnCode: push});
await cache.saveCache(cachePaths, cacheKey);
if (ret && push) { if (ret && push) {
// actions do not run on pushes made by actions. // actions do not run on pushes made by actions.
// need to make absolute sure things are good before pushing // need to make absolute sure things are good before pushing

43
package-lock.json generated
View file

@ -2,6 +2,27 @@
"requires": true, "requires": true,
"lockfileVersion": 1, "lockfileVersion": 1,
"dependencies": { "dependencies": {
"@actions/cache": {
"version": "0.2.1",
"resolved": "https://registry.npmjs.org/@actions/cache/-/cache-0.2.1.tgz",
"integrity": "sha512-CV2D9zp+d+nL7o6XK/I7vVh350JglPMp/jHi9ppRUkdBHPUeP0UHVUfygZaAs8WmxhhWY1MI0gWah+t0QYu3Fg==",
"requires": {
"@actions/core": "^1.2.4",
"@actions/exec": "^1.0.1",
"@actions/glob": "^0.1.0",
"@actions/http-client": "^1.0.8",
"@actions/io": "^1.0.1",
"semver": "^6.1.0",
"uuid": "^3.3.3"
},
"dependencies": {
"semver": {
"version": "6.3.0",
"resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz",
"integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw=="
}
}
},
"@actions/core": { "@actions/core": {
"version": "1.2.4", "version": "1.2.4",
"resolved": "https://registry.npmjs.org/@actions/core/-/core-1.2.4.tgz", "resolved": "https://registry.npmjs.org/@actions/core/-/core-1.2.4.tgz",
@ -26,6 +47,15 @@
"@octokit/plugin-rest-endpoint-methods": "^3.10.0" "@octokit/plugin-rest-endpoint-methods": "^3.10.0"
} }
}, },
"@actions/glob": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/@actions/glob/-/glob-0.1.0.tgz",
"integrity": "sha512-lx8SzyQ2FE9+UUvjqY1f28QbTJv+w8qP7kHHbfQRhphrlcx0Mdmm1tZdGJzfxv1jxREa/sLW4Oy8CbGQKCJySA==",
"requires": {
"@actions/core": "^1.2.0",
"minimatch": "^3.0.4"
}
},
"@actions/http-client": { "@actions/http-client": {
"version": "1.0.8", "version": "1.0.8",
"resolved": "https://registry.npmjs.org/@actions/http-client/-/http-client-1.0.8.tgz", "resolved": "https://registry.npmjs.org/@actions/http-client/-/http-client-1.0.8.tgz",
@ -496,8 +526,7 @@
"balanced-match": { "balanced-match": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
"integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c="
"dev": true
}, },
"base": { "base": {
"version": "0.11.2", "version": "0.11.2",
@ -600,7 +629,6 @@
"version": "1.1.11", "version": "1.1.11",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
"integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==",
"dev": true,
"requires": { "requires": {
"balanced-match": "^1.0.0", "balanced-match": "^1.0.0",
"concat-map": "0.0.1" "concat-map": "0.0.1"
@ -1019,8 +1047,7 @@
"concat-map": { "concat-map": {
"version": "0.0.1", "version": "0.0.1",
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
"integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s="
"dev": true
}, },
"concat-stream": { "concat-stream": {
"version": "1.6.2", "version": "1.6.2",
@ -2326,7 +2353,6 @@
"version": "3.0.4", "version": "3.0.4",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz",
"integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==",
"dev": true,
"requires": { "requires": {
"brace-expansion": "^1.1.7" "brace-expansion": "^1.1.7"
} }
@ -3722,6 +3748,11 @@
"integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=", "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=",
"dev": true "dev": true
}, },
"uuid": {
"version": "3.4.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz",
"integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A=="
},
"v8-compile-cache": { "v8-compile-cache": {
"version": "2.0.3", "version": "2.0.3",
"resolved": "https://registry.npmjs.org/v8-compile-cache/-/v8-compile-cache-2.0.3.tgz", "resolved": "https://registry.npmjs.org/v8-compile-cache/-/v8-compile-cache-2.0.3.tgz",

View file

@ -1,6 +1,7 @@
{ {
"private": true, "private": true,
"dependencies": { "dependencies": {
"@actions/cache": "*",
"@actions/core": "*", "@actions/core": "*",
"@actions/exec": "*", "@actions/exec": "*",
"@actions/github": "*" "@actions/github": "*"