Skip to content

Commit 29d543f

Browse files
Copilotneilime
andcommitted
feat: use symlink to maintain caching for external actions
Co-authored-by: neilime <[email protected]>
1 parent 5b62d9d commit 29d543f

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

actions/get-package-manager/action.yml

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,26 +91,43 @@ runs:
9191
}
9292
9393
let relativeWorkingDirectory = path.relative(process.env.GITHUB_WORKSPACE, workingDirectory) || '.';
94-
let isOutsideWorkspace = false;
94+
let cacheDependencyPathPrefix = relativeWorkingDirectory;
9595
9696
if (relativeWorkingDirectory.startsWith('../')) {
97-
core.warning(`Working directory "${workingDirectory}" is outside GITHUB_WORKSPACE. Caching will be disabled for setup-node.`);
98-
isOutsideWorkspace = true;
97+
// Working directory is outside GITHUB_WORKSPACE
98+
// Create a symlink inside GITHUB_WORKSPACE to enable caching
99+
const symlinkName = '.github-actions-cache';
100+
const symlinkPath = path.join(process.env.GITHUB_WORKSPACE, symlinkName);
101+
102+
try {
103+
// Remove existing symlink if it exists
104+
if (fs.existsSync(symlinkPath)) {
105+
fs.unlinkSync(symlinkPath);
106+
}
107+
108+
// Create symlink pointing to the working directory
109+
fs.symlinkSync(workingDirectory, symlinkPath, 'dir');
110+
core.info(`Created symlink at "${symlinkPath}" pointing to "${workingDirectory}" to enable caching.`);
111+
cacheDependencyPathPrefix = symlinkName;
112+
} catch (error) {
113+
core.warning(`Failed to create symlink for caching: ${error.message}. Caching will be disabled.`);
114+
cacheDependencyPathPrefix = '';
115+
}
99116
}
100117
101118
const packageManagerConfig = {
102119
yarn: {
103-
cacheDependencyPath: isOutsideWorkspace ? '' : `${relativeWorkingDirectory}/**/yarn.lock`,
120+
cacheDependencyPath: cacheDependencyPathPrefix ? `${cacheDependencyPathPrefix}/**/yarn.lock` : '',
104121
installCommand: 'yarn install --frozen-lockfile',
105122
runScriptCommand: 'yarn',
106123
},
107124
pnpm: {
108-
cacheDependencyPath: isOutsideWorkspace ? '' : `${relativeWorkingDirectory}/**/pnpm-lock.yaml`,
125+
cacheDependencyPath: cacheDependencyPathPrefix ? `${cacheDependencyPathPrefix}/**/pnpm-lock.yaml` : '',
109126
installCommand: 'pnpm install --frozen-lockfile',
110127
runScriptCommand: 'pnpm',
111128
},
112129
npm: {
113-
cacheDependencyPath: isOutsideWorkspace ? '' : `${relativeWorkingDirectory}/**/package-lock.json`,
130+
cacheDependencyPath: cacheDependencyPathPrefix ? `${cacheDependencyPathPrefix}/**/package-lock.json` : '',
114131
installCommand: 'npm ci',
115132
runScriptCommand: 'npm run',
116133
},

0 commit comments

Comments
 (0)