-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yml
More file actions
210 lines (180 loc) · 8.08 KB
/
action.yml
File metadata and controls
210 lines (180 loc) · 8.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
name: "Dependencies cache"
description: |
Action to setup dependencies cache managment.
### Supported caches
- NX (`node_modules/.cache/nx`): `nx`
- Prettier (`node_modules/.cache/prettier`): `prettier`
- Docusaurus (`.docusaurus`, `node_modules/.cache/webpack`): `docusaurus`
- Vitest (`node_modules/.vite/vitest`): `vitest`
- Astro (`node_modules/.astro`): `astro`
- Gatsby (`.cache`, `public`): `gatsby`
- Storybook (`node_modules/.cache/storybook`): `storybook`
- Jest (auto-detected `cacheDirectory`): `jest`
author: hoverkraft
branding:
icon: archive
color: blue
inputs:
dependencies:
description: "List of dependencies for which the cache should be managed."
required: true
working-directory:
description: |
Working directory where the dependencies are installed.
Can be absolute or relative to the repository root.
required: false
default: "."
runs:
using: "composite"
steps:
- shell: bash
# FIXME: workaround until will be merged: https://github.com/actions/runner/pull/1684
run: mkdir -p ./self-dependencies-cache-action/ && cp -r $GITHUB_ACTION_PATH/../* ./self-dependencies-cache-action/
- id: has-installed-dependencies
if: inputs.dependencies != ''
uses: ./self-dependencies-cache-action/has-installed-dependencies
with:
dependencies: ${{ inputs.dependencies }}
working-directory: ${{ inputs.working-directory }}
- id: get-package-manager
if: inputs.package-manager == ''
uses: ./self-dependencies-cache-action/get-package-manager
with:
working-directory: ${{ inputs.working-directory }}
- name: ♻️ NX cache
if: fromJson(steps.has-installed-dependencies.outputs.installed-dependencies).nx == true
uses: actions/cache@a7833574556fa59680c1b7cb190c1735db73ebf0 # v5.0.0
with:
path: ${{ inputs.working-directory }}/node_modules/.cache/nx
key: ${{ runner.os }}-cache-nx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-cache-nx-
- name: ♻️ Prettier cache
if: fromJson(steps.has-installed-dependencies.outputs.installed-dependencies).prettier == true
uses: actions/cache@a7833574556fa59680c1b7cb190c1735db73ebf0 # v5.0.0
with:
path: ${{ inputs.working-directory }}/node_modules/.cache/prettier
key: ${{ runner.os }}-cache-prettier-${{ github.sha }}
restore-keys: |
${{ runner.os }}-cache-prettier-
- name: ♻️ Docusaurus cache
if: fromJson(steps.has-installed-dependencies.outputs.installed-dependencies).docusaurus == true
uses: actions/cache@a7833574556fa59680c1b7cb190c1735db73ebf0 # v5.0.0
with:
path: |
${{ inputs.working-directory }}/.docusaurus
${{ inputs.working-directory }}/node_modules/.cache/webpack
key: ${{ runner.os }}-cache-docusaurus-${{ github.sha }}
restore-keys: |
${{ runner.os }}-cache-docusaurus-
- name: ♻️ Vitest cache
if: fromJson(steps.has-installed-dependencies.outputs.installed-dependencies).vitest == true
uses: actions/cache@a7833574556fa59680c1b7cb190c1735db73ebf0 # v5.0.0
with:
path: ${{ inputs.working-directory }}/node_modules/.vite/vitest
key: ${{ runner.os }}-cache-vitest-${{ github.sha }}
restore-keys: |
${{ runner.os }}-cache-vitest-
- name: ♻️ Astro cache
if: fromJson(steps.has-installed-dependencies.outputs.installed-dependencies).astro == true
uses: actions/cache@a7833574556fa59680c1b7cb190c1735db73ebf0 # v5.0.0
with:
path: ${{ inputs.working-directory }}/node_modules/.astro
key: ${{ runner.os }}-cache-astro-${{ github.sha }}
restore-keys: |
${{ runner.os }}-cache-astro-
- name: ♻️ Gatsby cache
if: fromJson(steps.has-installed-dependencies.outputs.installed-dependencies).gatsby == true
uses: actions/cache@a7833574556fa59680c1b7cb190c1735db73ebf0 # v5.0.0
with:
path: |
${{ inputs.working-directory }}/.cache
${{ inputs.working-directory }}/public
key: ${{ runner.os }}-cache-gatsby-${{ github.sha }}
restore-keys: |
${{ runner.os }}-cache-gatsby-
- name: ♻️ Storybook cache
if: fromJson(steps.has-installed-dependencies.outputs.installed-dependencies).storybook == true
uses: actions/cache@a7833574556fa59680c1b7cb190c1735db73ebf0 # v5.0.0
with:
path: ${{ inputs.working-directory }}/node_modules/.cache/storybook
key: ${{ runner.os }}-cache-storybook-${{ github.sha }}
restore-keys: |
${{ runner.os }}-cache-storybook-
- name: ♻️ Get Jest cache dir
id: jest-cache-dir-path
if: fromJson(steps.has-installed-dependencies.outputs.installed-dependencies).jest == true
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
env:
WORKING_DIRECTORY: ${{ inputs.working-directory }}
PACKAGE_MANAGER: ${{ steps.get-package-manager.outputs.package-manager }}
with:
# jscpd:ignore-start
script: |
const fs = require('node:fs');
const path = require('node:path');
let workingDirectory = process.env.WORKING_DIRECTORY || '.';
if (!path.isAbsolute(workingDirectory)) {
workingDirectory = path.join(process.env.GITHUB_WORKSPACE, workingDirectory);
}
if (!fs.existsSync(workingDirectory)) {
core.setFailed(`The specified working directory does not exist: ${workingDirectory}`);
return;
}
workingDirectory = path.resolve(workingDirectory);
core.debug(`Running in working directory: ${workingDirectory}`);
process.chdir(workingDirectory);
const packageManager = process.env.PACKAGE_MANAGER;
if (!packageManager) {
core.setFailed('Unable to determine package manager');
return;
}
core.debug(`Using package manager: ${packageManager}`);
const commandArgs = packageManager === 'npm'
? ['exec', 'jest', '--', '--showConfig']
: ['jest', '--showConfig'];
let execResult;
try {
execResult = await exec.getExecOutput(packageManager, commandArgs, { cwd: workingDirectory, silent: true });
} catch (error) {
core.setFailed(`Unable to get Jest config: ${error.message}`);
return;
}
if (execResult.exitCode !== 0) {
const errorMessage = execResult.stderr?.trim() || execResult.stdout?.trim();
core.setFailed(`Unable to get Jest config (exit code ${execResult.exitCode}): ${errorMessage}`);
return;
}
const jestConfigRaw = execResult.stdout.trim();
if (!jestConfigRaw) {
core.setFailed('Unable to get Jest config');
return;
}
core.debug(`Jest config: ${jestConfigRaw}`);
// Find cacheDirectory in the config with regex
const cacheDirMatch = jestConfigRaw.match(/"cacheDirectory"\s*:\s*"([^"]+)"/);
if (!cacheDirMatch || cacheDirMatch.length < 2) {
core.setFailed('Unable to find cacheDirectory in Jest config');
return;
}
let jestCacheDir = cacheDirMatch[1];
if (!path.isAbsolute(jestCacheDir)) {
jestCacheDir = path.join(workingDirectory, jestCacheDir);
}
jestCacheDir = path.resolve(jestCacheDir);
core.debug(`Jest cache directory: ${jestCacheDir}`);
core.setOutput('dir', jestCacheDir);
# jscpd:ignore-end
- name: ♻️ Test cache
if: steps.jest-cache-dir-path.outputs.dir
uses: actions/cache@a7833574556fa59680c1b7cb190c1735db73ebf0 # v5.0.0
with:
path: ${{ steps.jest-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-test-${{ github.sha }}
restore-keys: |
${{ runner.os }}-test-
# FIXME: workaround until will be merged: https://github.com/actions/runner/pull/1684
- shell: bash
if: always()
run: |
rm -fr ./self-dependencies-cache-action