Replace forked cache by official npm (#21)

* Replace forked cache by official npm

* Follow the same validation as @action/cache

* Remove debug log
This commit is contained in:
Tam Mach 2020-05-23 19:25:16 +10:00 committed by GitHub
parent 04eca20383
commit 3395f777a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 2289 additions and 1581 deletions

View File

@ -10,7 +10,7 @@ jobs:
build: # make sure build/ci work properly build: # make sure build/ci work properly
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v1 - uses: actions/checkout@v2
- run: | - run: |
npm install npm install
npm run prepare-deps npm run prepare-deps
@ -18,7 +18,7 @@ jobs:
test: # make sure the action works on a clean machine without building test: # make sure the action works on a clean machine without building
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v1 - uses: actions/checkout@v2
- uses: ./ - uses: ./
with: with:
version: v1.26 version: v1.26

View File

@ -58,7 +58,7 @@ The restrictions of annotations are the following:
The action was implemented with performance in mind: The action was implemented with performance in mind:
1. We cache data by [@actions/cache](https://github.com/actions/cache) between builds: Go build cache, Go modules cache, golangci-lint analysis cache. 1. We cache data by [@actions/cache](https://github.com/actions/toolkit/tree/master/packages/cache) between builds: Go build cache, Go modules cache, golangci-lint analysis cache.
2. We don't use Docker because image pulling is slow. 2. We don't use Docker because image pulling is slow.
3. We do as much as we can in parallel, e.g. we download cache, go and golangci-lint binary in parallel. 3. We do as much as we can in parallel, e.g. we download cache, go and golangci-lint binary in parallel.
@ -74,7 +74,7 @@ For example, in a repository of [golangci-lint](https://github.com/golangci/gola
We use JavaScript-based action. We don't use Docker-based action because: We use JavaScript-based action. We don't use Docker-based action because:
1. docker pulling is slow currently 1. docker pulling is slow currently
2. it's easier to use caching from [@actions/cache](https://github.com/actions/cache) until GitHub team hasn't supported reusing actions from actions 2. it's easier to use caching from [@actions/cache](https://github.com/actions/toolkit/tree/master/packages/cache)
Inside our action we perform 3 steps: Inside our action we perform 3 steps:

1842
dist/post_run/index.js vendored

File diff suppressed because it is too large Load Diff

1842
dist/run/index.js vendored

File diff suppressed because it is too large Load Diff

26
package-lock.json generated
View File

@ -4,6 +4,20 @@
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"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"
}
},
"@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",
@ -446,18 +460,6 @@
"resolved": "https://registry.npmjs.org/btoa-lite/-/btoa-lite-1.0.0.tgz", "resolved": "https://registry.npmjs.org/btoa-lite/-/btoa-lite-1.0.0.tgz",
"integrity": "sha1-M3dm2hWAEhD92VbCLpxokaudAzc=" "integrity": "sha1-M3dm2hWAEhD92VbCLpxokaudAzc="
}, },
"cache": {
"version": "git+https://github.com/golangci/cache.git#3df9f001c2d6c950a3deb6f2c1f88ddc2a609d25",
"from": "git+https://github.com/golangci/cache.git",
"requires": {
"@actions/core": "^1.2.0",
"@actions/exec": "^1.0.1",
"@actions/glob": "^0.1.0",
"@actions/http-client": "^1.0.8",
"@actions/io": "^1.0.1",
"uuid": "^3.3.3"
}
},
"callsites": { "callsites": {
"version": "3.1.0", "version": "3.1.0",
"resolved": "https://registry.npmjs.org/callsites/-/callsites-3.1.0.tgz", "resolved": "https://registry.npmjs.org/callsites/-/callsites-3.1.0.tgz",

View File

@ -6,8 +6,7 @@
"main": "dist/main.js", "main": "dist/main.js",
"scripts": { "scripts": {
"prepare-setup-go": "cd node_modules/setup-go && npm run build", "prepare-setup-go": "cd node_modules/setup-go && npm run build",
"prepare-cache": "cd node_modules/cache && npm run build", "prepare-deps": "npm run prepare-setup-go",
"prepare-deps": "npm run prepare-setup-go && npm run prepare-cache",
"build": "tsc && ncc build -o dist/run/ src/main.ts && ncc build -o dist/post_run/ src/post_main.ts", "build": "tsc && ncc build -o dist/run/ src/main.ts && ncc build -o dist/post_run/ src/post_main.ts",
"watched_build_main": "tsc && ncc build -w -o dist/run/ src/main.ts", "watched_build_main": "tsc && ncc build -w -o dist/run/ src/main.ts",
"watched_build_post_main": "tsc && ncc build -w -o dist/post_run/ src/post_main.ts", "watched_build_post_main": "tsc && ncc build -w -o dist/post_run/ src/post_main.ts",
@ -30,9 +29,9 @@
"@actions/exec": "^1.0.1", "@actions/exec": "^1.0.1",
"@actions/github": "^2.1.1", "@actions/github": "^2.1.1",
"@actions/tool-cache": "^1.3.4", "@actions/tool-cache": "^1.3.4",
"@actions/cache": "^0.2.1",
"@types/semver": "^7.1.0", "@types/semver": "^7.1.0",
"@types/tmp": "^0.2.0", "@types/tmp": "^0.2.0",
"cache": "git+https://github.com/golangci/cache.git",
"setup-go": "git+https://github.com/actions/setup-go.git", "setup-go": "git+https://github.com/actions/setup-go.git",
"tmp": "^0.2.1" "tmp": "^0.2.1"
}, },

View File

@ -1,9 +1,11 @@
import * as cache from "@actions/cache"
import * as core from "@actions/core" import * as core from "@actions/core"
import restore from "cache/lib/restore"
import save from "cache/lib/save"
import * as crypto from "crypto" import * as crypto from "crypto"
import * as fs from "fs" import * as fs from "fs"
import { Events, State } from "./constants"
import * as utils from "./utils/actionUtils"
function checksumFile(hashName: string, path: string): Promise<string> { function checksumFile(hashName: string, path: string): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const hash = crypto.createHash(hashName) const hash = crypto.createHash(hashName)
@ -52,30 +54,82 @@ async function buildCacheKeys(): Promise<string[]> {
} }
export async function restoreCache(): Promise<void> { export async function restoreCache(): Promise<void> {
if (!utils.isValidEvent()) {
utils.logWarning(
`Event Validation Error: The event type ${process.env[Events.Key]} is not supported because it's not tied to a branch or tag ref.`
)
return
}
const startedAt = Date.now() const startedAt = Date.now()
const keys = await buildCacheKeys() const keys = await buildCacheKeys()
const primaryKey = keys.pop() const primaryKey = keys.pop()
const restoreKeys = keys.reverse() const restoreKeys = keys.reverse()
core.info(`Primary analysis cache key is '${primaryKey}', restore keys are '${restoreKeys.join(` | `)}'`)
process.env[`INPUT_RESTORE-KEYS`] = restoreKeys.join(`\n`)
process.env.INPUT_KEY = primaryKey
process.env.INPUT_PATH = getCacheDirs().join(`\n`)
// Tell golangci-lint to use our cache directory. // Tell golangci-lint to use our cache directory.
process.env.GOLANGCI_LINT_CACHE = getLintCacheDir() process.env.GOLANGCI_LINT_CACHE = getLintCacheDir()
await restore() if (!primaryKey) {
core.info(`Restored cache for golangci-lint from key '${primaryKey}' in ${Date.now() - startedAt}ms`) utils.logWarning(`Invalid primary key`)
return
}
core.saveState(State.CachePrimaryKey, primaryKey)
try {
const cacheKey = await cache.restoreCache(getCacheDirs(), primaryKey, restoreKeys)
if (!cacheKey) {
core.info(`Cache not found for input keys: ${[primaryKey, ...restoreKeys].join(", ")}`)
return
}
// Store the matched cache key
utils.setCacheState(cacheKey)
const isExactKeyMatch = utils.isExactKeyMatch(primaryKey, cacheKey)
utils.setCacheHitOutput(isExactKeyMatch)
core.info(`Restored cache for golangci-lint from key '${primaryKey}' in ${Date.now() - startedAt}ms`)
} catch (error) {
if (error.name === cache.ValidationError.name) {
throw error
} else {
core.warning(error.message)
}
}
} }
export async function saveCache(): Promise<void> { export async function saveCache(): Promise<void> {
// Validate inputs, this can cause task failure
if (!utils.isValidEvent()) {
utils.logWarning(
`Event Validation Error: The event type ${process.env[Events.Key]} is not supported because it's not tied to a branch or tag ref.`
)
return
}
const startedAt = Date.now() const startedAt = Date.now()
const cacheDirs = getCacheDirs() const cacheDirs = getCacheDirs()
process.env.INPUT_PATH = cacheDirs.join(`\n`) const primaryKey = core.getState(State.CachePrimaryKey)
if (!primaryKey) {
utils.logWarning(`Error retrieving key from state.`)
return
}
await save() const state = utils.getCacheState()
core.info(`Saved cache for golangci-lint from paths '${cacheDirs.join(`, `)}' in ${Date.now() - startedAt}ms`)
if (utils.isExactKeyMatch(primaryKey, state)) {
core.info(`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`)
return
}
try {
await cache.saveCache(cacheDirs, primaryKey)
core.info(`Saved cache for golangci-lint from paths '${cacheDirs.join(`, `)}' in ${Date.now() - startedAt}ms`)
} catch (error) {
if (error.name === cache.ValidationError.name) {
throw error
} else if (error.name === cache.ReserveCacheError.name) {
core.info(error.message)
} else {
core.info(`[warning] ${error.message}`)
}
}
} }

22
src/constants.ts Normal file
View File

@ -0,0 +1,22 @@
export enum Inputs {
Key = "key",
Path = "path",
RestoreKeys = "restore-keys",
}
export enum Outputs {
CacheHit = "cache-hit",
}
export enum State {
CachePrimaryKey = "CACHE_KEY",
CacheMatchedKey = "CACHE_RESULT",
}
export enum Events {
Key = "GITHUB_EVENT_NAME",
Push = "push",
PullRequest = "pull_request",
}
export const RefKey = "GITHUB_REF"

47
src/utils/actionUtils.ts Normal file
View File

@ -0,0 +1,47 @@
import * as core from "@actions/core"
import { Outputs, RefKey, State } from "../constants"
export function isExactKeyMatch(key: string, cacheKey?: string): boolean {
return !!(
cacheKey &&
cacheKey.localeCompare(key, undefined, {
sensitivity: "accent",
}) === 0
)
}
export function setCacheState(state: string): void {
core.saveState(State.CacheMatchedKey, state)
}
export function setCacheHitOutput(isCacheHit: boolean): void {
core.setOutput(Outputs.CacheHit, isCacheHit.toString())
}
export function setOutputAndState(key: string, cacheKey?: string): void {
setCacheHitOutput(isExactKeyMatch(key, cacheKey))
// Store the matched cache key if it exists
cacheKey && setCacheState(cacheKey)
}
export function getCacheState(): string | undefined {
const cacheKey = core.getState(State.CacheMatchedKey)
if (cacheKey) {
core.debug(`Cache state/key: ${cacheKey}`)
return cacheKey
}
return undefined
}
export function logWarning(message: string): void {
const warningPrefix = "[warning]"
core.info(`${warningPrefix}${message}`)
}
// Cache token authorized for all events that are tied to a ref
// See GitHub Context https://help.github.com/actions/automating-your-workflow-with-github-actions/contexts-and-expression-syntax-for-github-actions#github-context
export function isValidEvent(): boolean {
return RefKey in process.env && Boolean(process.env[RefKey])
}