Skip to content

Commit

Permalink
Include Python version in pip cache key
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Dec 17, 2021
1 parent 2b732b8 commit 45cf4cf
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
6 changes: 1 addition & 5 deletions __tests__/cache-restore.test.ts
Expand Up @@ -92,13 +92,9 @@ describe('restore-cache', () => {
dependencyFile
);
await cacheDistributor.restoreCache();
let pythonKey = '';
if (packageManager === 'pipenv') {
pythonKey = `python-${pythonVersion}-`;
}

expect(infoSpy).toHaveBeenCalledWith(
`Cache restored from key: setup-python-${process.env['RUNNER_OS']}-${pythonKey}${packageManager}-${fileHash}`
`Cache restored from key: setup-python-${process.env['RUNNER_OS']}-python-${pythonVersion}-${packageManager}-${fileHash}`
);
}
);
Expand Down
2 changes: 1 addition & 1 deletion src/cache-distributions/cache-factory.ts
Expand Up @@ -13,7 +13,7 @@ export function getCacheDistributor(
) {
switch (packageManager) {
case PackageManagers.Pip:
return new PipCache(cacheDependencyPath);
return new PipCache(pythonVersion, cacheDependencyPath);
case PackageManagers.Pipenv:
return new PipenvCache(pythonVersion, cacheDependencyPath);
default:
Expand Down
9 changes: 6 additions & 3 deletions src/cache-distributions/pip-cache.ts
Expand Up @@ -8,7 +8,10 @@ import os from 'os';
import CacheDistributor from './cache-distributor';

class PipCache extends CacheDistributor {
constructor(cacheDependencyPath: string = '**/requirements.txt') {
constructor(
private pythonVersion: string,
cacheDependencyPath: string = '**/requirements.txt'
) {
super('pip', cacheDependencyPath);
}

Expand Down Expand Up @@ -36,8 +39,8 @@ class PipCache extends CacheDistributor {

protected async computeKeys() {
const hash = await glob.hashFiles(this.cacheDependencyPath);
const primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${this.packageManager}-${hash}`;
const restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${this.packageManager}`;
const primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
const restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}`;

return {
primaryKey,
Expand Down

0 comments on commit 45cf4cf

Please sign in to comment.