From 4a08748ace346be6b06cec8872f8f47006efc238 Mon Sep 17 00:00:00 2001 From: Trim21 Date: Mon, 8 Jun 2020 22:56:46 +0800 Subject: [PATCH] embedding cache steps by using @actions/cache --- README.md | 6 ----- index.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++ package-lock.json | 43 ++++++++++++++++++++++++++++++----- package.json | 1 + 4 files changed, 95 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 7adec68..57be008 100644 --- a/README.md +++ b/README.md @@ -24,12 +24,6 @@ jobs: steps: - uses: actions/checkout@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 ``` diff --git a/index.js b/index.js index fb87234..4c9b569 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,58 @@ +const os = require('os'); +const fs = require('fs'); +const path = require('path'); +const crypto = require('crypto'); + const core = require('@actions/core'); const exec = require('@actions/exec'); const github = require('@actions/github'); const tr = require('@actions/exec/lib/toolrunner'); +const cache = require('@actions/cache'); + +const paths = [path.join(os.homedir(), '.cache', 'pre-commit')]; + +async function getPythonVersion() { + let myOutput = ''; + const options = { + silent: true, + listeners: { + stdout: (data) => { + myOutput += data.toString(); + } + } + }; + + await exec.exec('python', ['-VV'], options); + await exec.exec('which', ['python'], options); + return myOutput; +} + +function hashFile(filePath) { + return hashString(fs.readFileSync(filePath).toString()); + +} + +function hashString(content) { + const sha256 = crypto.createHash('sha256'); + return sha256.update(content).digest('hex'); +} + +async function setupCache(cacheKey) { + try { + await cache.saveCache(paths, cacheKey); + } catch (e) { + if (e.toString().includes('reserveCache failed')) { + // race condition + core.info(e.message); + return; + } + throw e; + } +} + +async function restoreCache(cacheKey) { + return !!(await cache.restoreCache(paths, cacheKey)); +} function addToken(url, token) { return url.replace(/^https:\/\//, `https://x-access-token:${token}@`); @@ -22,7 +73,13 @@ async function main() { const token = core.getInput('token'); const pr = github.context.payload.pull_request; const push = !!token && !!pr; + + const pyVer = await getPythonVersion(); + const cacheKey = `pre-commit-1-${hashString(pyVer)}-${hashFile('.pre-commit-config.yaml')}`; + await restoreCache(cacheKey); const ret = await exec.exec('pre-commit', args, {ignoreReturnCode: push}); + await setupCache(cacheKey); + if (ret && push) { // actions do not run on pushes made by actions. // need to make absolute sure things are good before pushing diff --git a/package-lock.json b/package-lock.json index cdda01a..124f022 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2,6 +2,27 @@ "requires": true, "lockfileVersion": 1, "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": { "version": "1.2.4", "resolved": "https://registry.npmjs.org/@actions/core/-/core-1.2.4.tgz", @@ -26,6 +47,15 @@ "@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": { "version": "1.0.8", "resolved": "https://registry.npmjs.org/@actions/http-client/-/http-client-1.0.8.tgz", @@ -496,8 +526,7 @@ "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", - "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", - "dev": true + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" }, "base": { "version": "0.11.2", @@ -600,7 +629,6 @@ "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", - "dev": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -1019,8 +1047,7 @@ "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", - "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", - "dev": true + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" }, "concat-stream": { "version": "1.6.2", @@ -2326,7 +2353,6 @@ "version": "3.0.4", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", - "dev": true, "requires": { "brace-expansion": "^1.1.7" } @@ -3722,6 +3748,11 @@ "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=", "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": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/v8-compile-cache/-/v8-compile-cache-2.0.3.tgz", diff --git a/package.json b/package.json index 6b87caa..2625d22 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,7 @@ { "private": true, "dependencies": { + "@actions/cache": "*", "@actions/core": "*", "@actions/exec": "*", "@actions/github": "*"