68 lines
1.6 KiB
JavaScript
68 lines
1.6 KiB
JavaScript
import { getInput, getMultilineInput, info } from '@actions/core'
|
|
|
|
const crypto = require('crypto')
|
|
const fs = require('fs')
|
|
|
|
function getFileHash256(fileName) {
|
|
return new Promise((resolve, reject) => {
|
|
const hash = crypto.createHash('sha256')
|
|
|
|
var reader = fs.createReadStream(fileName)
|
|
|
|
reader.pipe(hash)
|
|
|
|
reader.on('end', function () {
|
|
resolve(hash.digest('hex'))
|
|
})
|
|
|
|
reader.on('error', function (error) {
|
|
reject(error)
|
|
})
|
|
})
|
|
|
|
}
|
|
|
|
|
|
export function isExactKeyMatch(key, cacheKey) {
|
|
return !!(
|
|
cacheKey &&
|
|
cacheKey.localeCompare(key, undefined, {
|
|
sensitivity: "accent"
|
|
}) === 0
|
|
)
|
|
}
|
|
|
|
export const mounts = getMultilineInput('mount', { require: true, trimWhitespace: true })
|
|
|
|
export const { primaryKey, restoreKeys } = await async function () {
|
|
|
|
const key = getInput('key', { required: true, trimWhitespace: true }).replace('/', '-')
|
|
|
|
const os = process.env.RUNNER_OS
|
|
|
|
let primaryKey, restoreKeys = []
|
|
|
|
const hashFiles = getMultilineInput('hash_files', { trimWhitespace: true })
|
|
|
|
info('hashFiles=' + hashFiles.join(','))
|
|
|
|
const tasks = hashFiles.filter(s => fs.existsSync(s))
|
|
.map(item => getFileHash256(item))
|
|
|
|
const hash256 = await Promise.all(tasks).then(res => {
|
|
return crypto.createHash('sha256')
|
|
.update(res.join(' ') + '|cache')
|
|
.digest('hex')
|
|
})
|
|
|
|
primaryKey = `${os}-${key}-${hash256}`
|
|
restoreKeys = [
|
|
`${os}-${key}-`,
|
|
]
|
|
|
|
return {
|
|
primaryKey,
|
|
restoreKeys,
|
|
}
|
|
}()
|