mirror of
https://github.com/gradle/gradle-build-action.git
synced 2024-12-25 15:06:19 +08:00
Extract cache key generation into common function
This commit is contained in:
parent
0ecbac99f3
commit
d7ed6d7e8d
@ -4,11 +4,10 @@ import os from 'os'
|
|||||||
|
|
||||||
import * as core from '@actions/core'
|
import * as core from '@actions/core'
|
||||||
import * as cache from '@actions/cache'
|
import * as cache from '@actions/cache'
|
||||||
import * as github from '@actions/github'
|
|
||||||
import {
|
import {
|
||||||
|
generateCacheKey,
|
||||||
isCacheReadEnabled,
|
isCacheReadEnabled,
|
||||||
isCacheSaveEnabled,
|
isCacheSaveEnabled
|
||||||
truncateArgs
|
|
||||||
} from './cache-utils'
|
} from './cache-utils'
|
||||||
|
|
||||||
const CACHE_NAME = 'gradle-user-home'
|
const CACHE_NAME = 'gradle-user-home'
|
||||||
@ -28,21 +27,15 @@ export async function restore(): Promise<void> {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const cacheKeySeed = process.env[`CACHE_KEY_SEED`] || ''
|
const cacheKey = generateCacheKey('gradle')
|
||||||
const runnerOs = process.env[`RUNNER_OS`] || ''
|
|
||||||
const cacheKeyPrefix = `${cacheKeySeed}${runnerOs}|gradle|`
|
|
||||||
|
|
||||||
const args = truncateArgs(core.getInput('arguments'))
|
core.saveState(CACHE_KEY, cacheKey.key)
|
||||||
const cacheKeyWithArgs = `${cacheKeyPrefix}${args}|`
|
|
||||||
|
|
||||||
const cacheKey = `${cacheKeyWithArgs}${github.context.sha}`
|
const cacheResult = await cache.restoreCache(
|
||||||
|
CACHE_PATH,
|
||||||
core.saveState(CACHE_KEY, cacheKey)
|
cacheKey.key,
|
||||||
|
cacheKey.restoreKeys
|
||||||
const cacheResult = await cache.restoreCache(CACHE_PATH, cacheKey, [
|
)
|
||||||
cacheKeyWithArgs,
|
|
||||||
cacheKeyPrefix
|
|
||||||
])
|
|
||||||
|
|
||||||
if (!cacheResult) {
|
if (!cacheResult) {
|
||||||
core.info(
|
core.info(
|
||||||
|
@ -3,11 +3,10 @@ import fs from 'fs'
|
|||||||
|
|
||||||
import * as core from '@actions/core'
|
import * as core from '@actions/core'
|
||||||
import * as cache from '@actions/cache'
|
import * as cache from '@actions/cache'
|
||||||
import * as github from '@actions/github'
|
|
||||||
import {
|
import {
|
||||||
|
generateCacheKey,
|
||||||
isCacheReadEnabled,
|
isCacheReadEnabled,
|
||||||
isCacheSaveEnabled,
|
isCacheSaveEnabled
|
||||||
truncateArgs
|
|
||||||
} from './cache-utils'
|
} from './cache-utils'
|
||||||
|
|
||||||
const CACHE_NAME = 'project-dot-gradle'
|
const CACHE_NAME = 'project-dot-gradle'
|
||||||
@ -27,21 +26,14 @@ export async function restore(rootDir: string): Promise<void> {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const cacheKeySeed = process.env[`CACHE_KEY_SEED`] || ''
|
const cacheKey = generateCacheKey('project')
|
||||||
const runnerOs = process.env[`RUNNER_OS`] || ''
|
|
||||||
const cacheKeyPrefix = `${cacheKeySeed}${runnerOs}|project|`
|
|
||||||
|
|
||||||
const args = truncateArgs(core.getInput('arguments'))
|
core.saveState(CACHE_KEY, cacheKey.key)
|
||||||
const cacheKeyWithArgs = `${cacheKeyPrefix}${args}|`
|
|
||||||
|
|
||||||
const cacheKey = `${cacheKeyWithArgs}${github.context.sha}`
|
|
||||||
|
|
||||||
core.saveState(CACHE_KEY, cacheKey)
|
|
||||||
|
|
||||||
const cacheResult = await cache.restoreCache(
|
const cacheResult = await cache.restoreCache(
|
||||||
getCachePath(rootDir),
|
getCachePath(rootDir),
|
||||||
cacheKey,
|
cacheKey.key,
|
||||||
[cacheKeyWithArgs, cacheKeyPrefix]
|
cacheKey.restoreKeys
|
||||||
)
|
)
|
||||||
|
|
||||||
if (!cacheResult) {
|
if (!cacheResult) {
|
||||||
|
@ -1,8 +1,5 @@
|
|||||||
import * as core from '@actions/core'
|
import * as core from '@actions/core'
|
||||||
|
import * as github from '@actions/github'
|
||||||
export function truncateArgs(args: string): string {
|
|
||||||
return args.trim().replace(/\s+/g, ' ').substr(0, 400)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function isCacheReadEnabled(cacheName: string): boolean {
|
export function isCacheReadEnabled(cacheName: string): boolean {
|
||||||
const configValue = getCacheEnabledValue(cacheName)
|
const configValue = getCacheEnabledValue(cacheName)
|
||||||
@ -26,3 +23,29 @@ function getCacheEnabledValue(cacheName: string): string {
|
|||||||
`Invalid cache-enabled parameter '${configValue}'. Valid values are ['true', 'false', 'read-only']`
|
`Invalid cache-enabled parameter '${configValue}'. Valid values are ['true', 'false', 'read-only']`
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function generateCacheKey(cacheName: string): CacheKey {
|
||||||
|
const cacheKeySeed = process.env[`CACHE_KEY_SEED`] || ''
|
||||||
|
const runnerOs = process.env[`RUNNER_OS`] || ''
|
||||||
|
const cacheKeyPrefix = `${cacheKeySeed}${runnerOs}|${cacheName}|`
|
||||||
|
|
||||||
|
const args = truncateArgs(core.getInput('arguments'))
|
||||||
|
const cacheKeyWithArgs = `${cacheKeyPrefix}${args}|`
|
||||||
|
|
||||||
|
const cacheKey = `${cacheKeyWithArgs}${github.context.sha}`
|
||||||
|
return new CacheKey(cacheKey, [cacheKeyWithArgs, cacheKeyPrefix])
|
||||||
|
}
|
||||||
|
|
||||||
|
function truncateArgs(args: string): string {
|
||||||
|
return args.trim().replace(/\s+/g, ' ').substr(0, 400)
|
||||||
|
}
|
||||||
|
|
||||||
|
export class CacheKey {
|
||||||
|
key: string
|
||||||
|
restoreKeys: string[]
|
||||||
|
|
||||||
|
constructor(key: string, restoreKeys: string[]) {
|
||||||
|
this.key = key
|
||||||
|
this.restoreKeys = restoreKeys
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user