diff --git a/.github/workflows/e2e-cache.yml b/.github/workflows/e2e-cache.yml index 8f4e1500..084cc235 100644 --- a/.github/workflows/e2e-cache.yml +++ b/.github/workflows/e2e-cache.yml @@ -10,7 +10,9 @@ on: - releases/* paths-ignore: - '**.md' - + +permissions: + contents: read jobs: python-pip-depencies-caching: name: Test pip (Python ${{ matrix.python-version}}, ${{ matrix.os }}) @@ -30,7 +32,7 @@ jobs: - name: Install dependencies run: pip install numpy pandas requests - python-pipenv-depencies-caching: + python-pipenv-dependencies-caching: name: Test pipenv (Python ${{ matrix.python-version}}, ${{ matrix.os }}) runs-on: ${{ matrix.os }} strategy: @@ -50,7 +52,7 @@ jobs: - name: Install dependencies run: pipenv install flake8 - python-pip-depencies-caching-path: + python-pip-dependencies-caching-path: name: Test pip (Python ${{ matrix.python-version}}, ${{ matrix.os }}) runs-on: ${{ matrix.os }} strategy: @@ -69,7 +71,7 @@ jobs: - name: Install dependencies run: pip install numpy pandas requests - python-pipenv-depencies-caching-path: + python-pipenv-dependencies-caching-path: name: Test pipenv (Python ${{ matrix.python-version}}, ${{ matrix.os }}) runs-on: ${{ matrix.os }} strategy: diff --git a/README.md b/README.md index 71c4de65..8ee9c783 100644 --- a/README.md +++ b/README.md @@ -209,12 +209,16 @@ pypy-3.7-nightly # Python 3.7 and nightly PyPy # Caching packages dependencies -The action has a built-in functionality for caching and restoring dependencies. It uses [actions/cache](https://github.com/actions/cache) under hood for caching dependencies but requires less configuration settings. Supported package managers are `pip` and `pipenv`. The `cache` input is optional, and caching is turned off by default. +The action has built-in functionality for caching and restoring dependencies. It uses [actions/cache](https://github.com/actions/toolkit/tree/main/packages/cache) under the hood for caching dependencies but requires less configuration settings. Supported package managers are `pip` and `pipenv`. The `cache` input is optional, and caching is turned off by default. -The action defaults to search for the dependency file (`requirements.txt` for pip or `Pipfile.lock` for pipenv) in the repository, and uses its hash as a part of the cache key. Use `cache-dependency-path` for cases when multiple dependency files are used, they are located in different subdirectories or different files for hash want to be used. +The action defaults to searching for a dependency file (`requirements.txt` for pip or `Pipfile.lock` for pipenv) in the repository, and uses its hash as a part of the cache key. Use `cache-dependency-path` for cases where multiple dependency files are used, they are located in different subdirectories or different files for the hash want to be used. - - For pip action will cache global cache ditrectory - - For pipenv action will cache virtuenv directory + - For pip, action will cache global cache directory + - For pipenv, action will cache virtuenv directory + +**Please Note:** Restored cache will not be used if the requirements.txt file is not updated for a long time and a newer version of the dependency is available that can lead to an increase in total build time. + +The requirements file format allows to specify dependency versions using logical operators (for example chardet>=3.0.4) or specify dependencies without any versions. In this case the pip install -r requirements.txt command will always try to install the latest available package version. To be sure that the cache will be used, please stick to a specific dependency version and update it manually if necessary. **Caching pip dependencies:** diff --git a/__tests__/cache-restore.test.ts b/__tests__/cache-restore.test.ts index aac8aa6f..f3c5b01d 100644 --- a/__tests__/cache-restore.test.ts +++ b/__tests__/cache-restore.test.ts @@ -26,7 +26,7 @@ describe('restore-cache', () => { beforeEach(() => { process.env['RUNNER_OS'] = process.env['RUNNER_OS'] ?? 'linux'; - // process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data'); + infoSpy = jest.spyOn(core, 'info'); infoSpy.mockImplementation(input => undefined); diff --git a/__tests__/cache-save.test.ts b/__tests__/cache-save.test.ts index c1e700c2..61367428 100644 --- a/__tests__/cache-save.test.ts +++ b/__tests__/cache-save.test.ts @@ -31,7 +31,7 @@ describe('run', () => { beforeEach(() => { process.env['RUNNER_OS'] = process.env['RUNNER_OS'] ?? 'linux'; - // process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data'); + infoSpy = jest.spyOn(core, 'info'); infoSpy.mockImplementation(input => undefined); diff --git a/action.yml b/action.yml index 892f1bf8..a1dd5b77 100644 --- a/action.yml +++ b/action.yml @@ -15,7 +15,7 @@ inputs: description: Used to pull python distributions from actions/python-versions. Since there's a default, this is typically not supplied by the user. default: ${{ github.token }} cache-dependency-path: - description: 'Used to specify the path to a dependency files. Supports wildcards or a list of file names for caching multiple dependencies.' + description: 'Used to specify the path to dependency files. Supports wildcards or a list of file names for caching multiple dependencies.' outputs: python-version: description: "The installed python version. Useful when given a version range as input." diff --git a/dist/setup/index.js b/dist/setup/index.js index f936ee50..5e8afc9f 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -6633,17 +6633,14 @@ const utils_1 = __webpack_require__(163); function isPyPyVersion(versionSpec) { return versionSpec.startsWith('pypy-'); } -function cacheDepencies(pythonVersion) { +function cacheDepencies(cache, pythonVersion) { return __awaiter(this, void 0, void 0, function* () { - const cache = core.getInput('cache'); - if (cache) { - if (utils_1.isGhes()) { - throw new Error('Caching is not supported on GHES'); - } - const cacheDependencyPath = core.getInput('cache-dependency-path') || undefined; - const cacheDistributor = cache_factory_1.getCacheDistributor(cache, pythonVersion, cacheDependencyPath); - yield cacheDistributor.restoreCache(); + if (utils_1.isGhes()) { + throw new Error('Caching is not supported on GHES'); } + const cacheDependencyPath = core.getInput('cache-dependency-path') || undefined; + const cacheDistributor = cache_factory_1.getCacheDistributor(cache, pythonVersion, cacheDependencyPath); + yield cacheDistributor.restoreCache(); }); } function run() { @@ -6663,7 +6660,10 @@ function run() { pythonVersion = installed.version; core.info(`Successfully setup ${installed.impl} (${pythonVersion})`); } - yield cacheDepencies(pythonVersion); + const cache = core.getInput('cache'); + if (cache) { + yield cacheDepencies(cache, pythonVersion); + } } const matchersPath = path.join(__dirname, '../..', '.github'); core.info(`##[add-matcher]${path.join(matchersPath, 'python.json')}`); diff --git a/src/setup-python.ts b/src/setup-python.ts index 2d5bd057..f793ee1d 100644 --- a/src/setup-python.ts +++ b/src/setup-python.ts @@ -10,21 +10,18 @@ function isPyPyVersion(versionSpec: string) { return versionSpec.startsWith('pypy-'); } -async function cacheDepencies(pythonVersion: string) { - const cache = core.getInput('cache'); - if (cache) { - if (isGhes()) { - throw new Error('Caching is not supported on GHES'); - } - const cacheDependencyPath = - core.getInput('cache-dependency-path') || undefined; - const cacheDistributor = getCacheDistributor( - cache, - pythonVersion, - cacheDependencyPath - ); - await cacheDistributor.restoreCache(); +async function cacheDepencies(cache: string, pythonVersion: string) { + if (isGhes()) { + throw new Error('Caching is not supported on GHES'); } + const cacheDependencyPath = + core.getInput('cache-dependency-path') || undefined; + const cacheDistributor = getCacheDistributor( + cache, + pythonVersion, + cacheDependencyPath + ); + await cacheDistributor.restoreCache(); } async function run() { @@ -45,7 +42,10 @@ async function run() { core.info(`Successfully setup ${installed.impl} (${pythonVersion})`); } - await cacheDepencies(pythonVersion); + const cache = core.getInput('cache'); + if (cache) { + await cacheDepencies(cache, pythonVersion); + } } const matchersPath = path.join(__dirname, '../..', '.github'); core.info(`##[add-matcher]${path.join(matchersPath, 'python.json')}`);