Skip to content

Commit 9f31dbb

Browse files
author
MaksimZhukov
committed
Support caching for mono repos and repositories with complex structure
1 parent aa759c6 commit 9f31dbb

File tree

7 files changed

+727
-674
lines changed

7 files changed

+727
-674
lines changed

Diff for: .github/workflows/e2e-cache.yml

+2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ jobs:
101101
node-yarn2-depencies-caching:
102102
name: Test yarn 2 (Node ${{ matrix.node-version}}, ${{ matrix.os }})
103103
runs-on: ${{ matrix.os }}
104+
env:
105+
YARN_ENABLE_IMMUTABLE_INSTALLS: false
104106
strategy:
105107
fail-fast: false
106108
matrix:

Diff for: README.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ nvm lts syntax: `lts/erbium`, `lts/fermium`, `lts/*`
4141

4242
### Caching packages dependencies
4343

44-
The action has a built-in functionality for caching and restoring npm/yarn dependencies. Supported package managers are `npm`, `yarn`, `pnpm`. The `cache` input is optional, and caching is turned off by default.
44+
The action has a built-in functionality for caching and restoring npm/yarn dependencies. Supported package managers are `npm`, `yarn`, `pnpm`. The `cache` input is optional, and caching is turned off by default. By default, the action searches for the dependency file in the project root and uses its hash as a part of cache key. Use `cache-dependency-path` to specify custom dependency file path. The field accepts wildcards or an array of files to be cached.
4545

4646
**Caching npm dependencies:**
4747
```yaml
@@ -90,7 +90,7 @@ steps:
9090
- run: pnpm test
9191
```
9292

93-
> At the moment, only `lock` files in the project root are supported.
93+
For more examlpes of caching, please see the [Advanced usage](docs/advanced-usage.md#caching-packages-dependencies) guide.
9494

9595
### Matrix Testing:
9696
```yaml
@@ -114,10 +114,11 @@ jobs:
114114

115115
1. [Check latest version](docs/advanced-usage.md#check-latest-version)
116116
2. [Using different architectures](docs/advanced-usage.md#architecture)
117-
3. [Using multiple operating systems and architectures](docs/advanced-usage.md#multiple-operating-systems-and-architectures)
118-
4. [Publishing to npmjs and GPR with npm](docs/advanced-usage.md#publish-to-npmjs-and-gpr-with-npm)
119-
5. [Publishing to npmjs and GPR with yarn](docs/advanced-usage.md#publish-to-npmjs-and-gpr-with-yarn)
120-
6. [Using private packages](docs/advanced-usage.md#use-private-packages)
117+
3. [Caching packages dependencies](docs/advanced-usage.md#caching-packages-dependencies)
118+
4. [Using multiple operating systems and architectures](docs/advanced-usage.md#multiple-operating-systems-and-architectures)
119+
5. [Publishing to npmjs and GPR with npm](docs/advanced-usage.md#publish-to-npmjs-and-gpr-with-npm)
120+
6. [Publishing to npmjs and GPR with yarn](docs/advanced-usage.md#publish-to-npmjs-and-gpr-with-yarn)
121+
7. [Using private packages](docs/advanced-usage.md#use-private-packages)
121122

122123
# License
123124

Diff for: action.yml

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ inputs:
2121
default: ${{ github.token }}
2222
cache:
2323
description: 'Used to specify a package manager for caching in the default directory. Supported values: npm, yarn, pnpm'
24+
cache-dependency-path:
25+
description: 'Used to specify path to a dependencies file: package-lock.json, yarn.lock, etc. Supports wildcards or an array of file names.'
2426
# TODO: add input to control forcing to pull from cloud or dist.
2527
# escape valve for someone having issues or needing the absolute latest which isn't cached yet
2628
# Deprecated option, do not use. Will not be supported after October 1, 2019

Diff for: dist/setup/index.js

+665-659
Large diffs are not rendered by default.

Diff for: docs/advanced-usage.md

+36-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Advanced usage
22

3-
### Check latest version:
3+
### Check latest version
44

55
The `check-latest` flag defaults to `false`. When set to `false`, the action will first check the local cache for a semver match. If unable to find a specific version in the cache, the action will attempt to download a version of Node.js. It will pull LTS versions from [node-versions releases](https://github.com/actions/node-versions/releases) and on miss or failure will fall back to the previous behavior of downloading directly from [node dist](https://nodejs.org/dist/). Use the default or set `check-latest` to `false` if you prefer stability and if you want to ensure a specific version of Node.js is always used.
66

@@ -19,7 +19,7 @@ steps:
1919
- run: npm test
2020
```
2121
22-
### Architecture:
22+
### Architecture
2323
2424
You can use any of the [supported operating systems](https://docs.github.com/en/actions/reference/virtual-environments-for-github-hosted-runners), and the compatible `architecture` can be selected using `architecture`. Values are `x86`, `x64`, `arm64`, `armv6l`, `armv7l`, `ppc64le`, `s390x` (not all of the architectures are available on all platforms).
2525

@@ -39,7 +39,37 @@ jobs:
3939
- run: npm test
4040
```
4141

42-
### Multiple Operating Systems and Architectures:
42+
### Caching packages dependencies
43+
44+
**Using wildcard patterns to cache dependencies**
45+
```yaml
46+
steps:
47+
- uses: actions/checkout@v2
48+
- uses: actions/setup-node@v2
49+
with:
50+
node-version: '14'
51+
cache: 'npm'
52+
cache-dependency-path: '**/package-lock.json'
53+
- run: npm install
54+
- run: npm test
55+
```
56+
57+
**Using a list of file paths to cache dependencies**
58+
```yaml
59+
steps:
60+
- uses: actions/checkout@v2
61+
- uses: actions/setup-node@v2
62+
with:
63+
node-version: '14'
64+
cache: 'npm'
65+
cache-dependency-path: |
66+
server/app/package-lock.json
67+
frontend/app/package-lock.json
68+
- run: npm install
69+
- run: npm test
70+
```
71+
72+
### Multiple Operating Systems and Architectures
4373

4474
```yaml
4575
jobs:
@@ -74,7 +104,7 @@ jobs:
74104
- run: npm test
75105
```
76106

77-
### Publish to npmjs and GPR with npm:
107+
### Publish to npmjs and GPR with npm
78108
```yaml
79109
steps:
80110
- uses: actions/checkout@v2
@@ -94,7 +124,7 @@ steps:
94124
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
95125
```
96126

97-
### Publish to npmjs and GPR with yarn:
127+
### Publish to npmjs and GPR with yarn
98128
```yaml
99129
steps:
100130
- uses: actions/checkout@v2
@@ -114,7 +144,7 @@ steps:
114144
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
115145
```
116146

117-
### Use private packages:
147+
### Use private packages
118148
```yaml
119149
steps:
120150
- uses: actions/checkout@v2

Diff for: src/cache-restore.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import {
1111
PackageManagerInfo
1212
} from './cache-utils';
1313

14-
export const restoreCache = async (packageManager: string) => {
14+
export const restoreCache = async (
15+
packageManager: string,
16+
cacheDependencyPath?: string
17+
) => {
1518
const packageManagerInfo = await getPackageManagerInfo(packageManager);
1619
if (!packageManagerInfo) {
1720
throw new Error(`Caching for '${packageManager}' is not supported`);
@@ -22,9 +25,17 @@ export const restoreCache = async (packageManager: string) => {
2225
packageManagerInfo,
2326
packageManager
2427
);
25-
const lockFilePath = findLockFile(packageManagerInfo);
28+
const lockFilePath = cacheDependencyPath
29+
? cacheDependencyPath
30+
: findLockFile(packageManagerInfo);
2631
const fileHash = await glob.hashFiles(lockFilePath);
2732

33+
if (!fileHash) {
34+
throw new Error(
35+
'Some specified paths were not resolved, unable to cache dependencies.'
36+
);
37+
}
38+
2839
const primaryKey = `node-cache-${platform}-${packageManager}-${fileHash}`;
2940
core.debug(`primary key is ${primaryKey}`);
3041

Diff for: src/main.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ export async function run() {
5151
if (isGhes()) {
5252
throw new Error('Caching is not supported on GHES');
5353
}
54-
await restoreCache(cache);
54+
const cacheDependencyPath = core.getInput('cache-dependency-path');
55+
await restoreCache(cache, cacheDependencyPath);
5556
}
5657

5758
const matchersPath = path.join(__dirname, '../..', '.github');

0 commit comments

Comments
 (0)