Permalink
Cannot retrieve contributors at this time
50 lines (40 sloc)
1.6 KB
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-dotnet/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.
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 {readdir} from 'node:fs/promises'; | |
import {join} from 'node:path'; | |
import * as cache from '@actions/cache'; | |
import * as core from '@actions/core'; | |
import * as glob from '@actions/glob'; | |
import {getNuGetFolderPath} from './cache-utils'; | |
import {lockFilePatterns, State, Outputs} from './constants'; | |
export const restoreCache = async (cacheDependencyPath?: string) => { | |
const lockFilePath = cacheDependencyPath || (await findLockFile()); | |
const fileHash = await glob.hashFiles(lockFilePath); | |
if (!fileHash) { | |
throw new Error( | |
'Some specified paths were not resolved, unable to cache dependencies.' | |
); | |
} | |
const platform = process.env.RUNNER_OS; | |
const primaryKey = `dotnet-cache-${platform}-${fileHash}`; | |
core.debug(`primary key is ${primaryKey}`); | |
core.saveState(State.CachePrimaryKey, primaryKey); | |
const {'global-packages': cachePath} = await getNuGetFolderPath(); | |
const cacheKey = await cache.restoreCache([cachePath], primaryKey); | |
core.setOutput(Outputs.CacheHit, Boolean(cacheKey)); | |
if (!cacheKey) { | |
core.info('Dotnet cache is not found'); | |
return; | |
} | |
core.saveState(State.CacheMatchedKey, cacheKey); | |
core.info(`Cache restored from key: ${cacheKey}`); | |
}; | |
const findLockFile = async () => { | |
const workspace = process.env.GITHUB_WORKSPACE!; | |
const rootContent = await readdir(workspace); | |
const lockFile = lockFilePatterns.find(item => rootContent.includes(item)); | |
if (!lockFile) { | |
throw new Error( | |
`Dependencies lock file is not found in ${workspace}. Supported file patterns: ${lockFilePatterns.toString()}` | |
); | |
} | |
return join(workspace, lockFile); | |
}; |