Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
setup-go/src/cache-restore.ts
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
65 lines (52 sloc)
2.06 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as cache from '@actions/cache'; | |
import * as core from '@actions/core'; | |
import * as glob from '@actions/glob'; | |
import path from 'path'; | |
import fs from 'fs'; | |
import {State, Outputs} from './constants'; | |
import {PackageManagerInfo} from './package-managers'; | |
import {getCacheDirectoryPath, getPackageManagerInfo} from './cache-utils'; | |
export const restoreCache = async ( | |
versionSpec: string, | |
packageManager: string, | |
cacheDependencyPath?: string | |
) => { | |
const packageManagerInfo = await getPackageManagerInfo(packageManager); | |
const platform = process.env.RUNNER_OS; | |
const cachePaths = await getCacheDirectoryPath(packageManagerInfo); | |
const dependencyFilePath = cacheDependencyPath | |
? cacheDependencyPath | |
: findDependencyFile(packageManagerInfo); | |
const fileHash = await glob.hashFiles(dependencyFilePath); | |
if (!fileHash) { | |
throw new Error( | |
'Some specified paths were not resolved, unable to cache dependencies.' | |
); | |
} | |
const linuxVersion = | |
process.env.RUNNER_OS === 'Linux' ? `${process.env.ImageOS}-` : ''; | |
const primaryKey = `setup-go-${platform}-${linuxVersion}go-${versionSpec}-${fileHash}`; | |
core.debug(`primary key is ${primaryKey}`); | |
core.saveState(State.CachePrimaryKey, primaryKey); | |
const cacheKey = await cache.restoreCache(cachePaths, primaryKey); | |
core.setOutput(Outputs.CacheHit, Boolean(cacheKey)); | |
if (!cacheKey) { | |
core.info(`Cache is not found`); | |
core.setOutput(Outputs.CacheHit, false); | |
return; | |
} | |
core.saveState(State.CacheMatchedKey, cacheKey); | |
core.info(`Cache restored from key: ${cacheKey}`); | |
}; | |
const findDependencyFile = (packageManager: PackageManagerInfo) => { | |
const dependencyFile = packageManager.dependencyFilePattern; | |
const workspace = process.env.GITHUB_WORKSPACE!; | |
const rootContent = fs.readdirSync(workspace); | |
const goSumFileExists = rootContent.includes(dependencyFile); | |
if (!goSumFileExists) { | |
throw new Error( | |
`Dependencies file is not found in ${workspace}. Supported file pattern: ${dependencyFile}` | |
); | |
} | |
return path.join(workspace, dependencyFile); | |
}; |