Skip to content

Commit b6e638b

Browse files
authored
new: Include proto/moon version in cache key. (#9)
1 parent 58d4b7f commit b6e638b

File tree

7 files changed

+285
-226
lines changed

7 files changed

+285
-226
lines changed

.github/workflows/ci.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,19 @@ jobs:
1414
- run: npm install -g pnpm
1515
- run: pnpm install
1616
- run: pnpm run check
17+
action-default:
18+
name: 'Action'
19+
runs-on: ${{ matrix.os }}
20+
strategy:
21+
matrix:
22+
os: [ubuntu-latest, macos-latest, windows-latest]
23+
fail-fast: false
24+
steps:
25+
- uses: actions/checkout@v4
26+
- uses: actions/setup-node@v3
27+
- run: npm install -g pnpm
28+
- run: pnpm install
29+
- run: pnpm run build
30+
- uses: ./ # self
31+
env:
32+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 0.3.0
2+
3+
- Now includes the moon and proto versions in the cache key.
4+
- Updated dependencies.
5+
16
# 0.2.1
27

38
- Support proto v0.24 changes.

helpers.ts

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import crypto from 'node:crypto';
12
import fs from 'node:fs';
23
import os from 'node:os';
34
import path from 'node:path';
@@ -37,24 +38,35 @@ export function getToolsDir() {
3738
return path.join(getProtoHome(), 'tools');
3839
}
3940

41+
export function getUidFile() {
42+
return path.join(getProtoHome(), 'id');
43+
}
44+
4045
export function getWorkingDir() {
4146
return process.env.GITHUB_WORKSPACE ?? process.cwd();
4247
}
4348

44-
export function isCacheEnabled(): boolean {
49+
export function isCacheEnabled() {
4550
return core.getBooleanInput('cache') && cache.isFeatureAvailable();
4651
}
4752

4853
export function isUsingMoon() {
4954
return fs.existsSync(path.join(getWorkingDir(), core.getInput('workspace-root'), '.moon'));
5055
}
5156

57+
export function extractMajorMinor(version: string) {
58+
const [major, minor] = version.split('.');
59+
60+
return `${major}.${minor}`;
61+
}
62+
5263
export function getCacheKeyPrefix() {
5364
// v1 - Before proto v0.24 changes
5465
return 'moonrepo-toolchain-v2';
5566
}
5667

5768
export async function getToolchainCacheKey() {
69+
const hasher = crypto.createHash('sha1');
5870
const files = ['.prototools'];
5971

6072
if (isUsingMoon()) {
@@ -67,9 +79,27 @@ export async function getToolchainCacheKey() {
6779
}
6880
}
6981

70-
const toolchainHash = await glob.hashFiles(files.join('\n'));
82+
core.debug(`Hashing files: ${files.join(', ')}`);
7183

72-
return `${getCacheKeyPrefix()}-${process.platform}-${toolchainHash}`;
84+
hasher.update(await glob.hashFiles(files.join('\n')));
85+
86+
const protoVersion = process.env.PROTO_CLI_VERSION;
87+
88+
if (protoVersion) {
89+
core.debug(`Hashing proto version: ${protoVersion}`);
90+
91+
hasher.update(extractMajorMinor(protoVersion));
92+
}
93+
94+
const moonVersion = process.env.MOON_CLI_VERSION;
95+
96+
if (moonVersion) {
97+
core.debug(`Hashing moon version: ${moonVersion}`);
98+
99+
hasher.update(extractMajorMinor(moonVersion));
100+
}
101+
102+
return `${getCacheKeyPrefix()}-${process.platform}-${hasher.digest('hex')}`;
73103
}
74104

75105
export async function installBin(bin: string) {
@@ -98,20 +128,28 @@ export async function installBin(bin: string) {
98128

99129
const binDir = getBinDir();
100130
const binPath = path.join(binDir, WINDOWS ? `${bin}.exe` : bin);
131+
const envPrefix = bin.toUpperCase();
101132

102133
await execa(script, version === 'latest' ? [] : [version], {
103134
env: {
104-
[`${bin.toUpperCase()}_INSTALL_DIR`]: binDir,
135+
[`${envPrefix}_INSTALL_DIR`]: binDir,
105136
},
106-
stdio: core.isDebug() || !!process.env[`${bin.toUpperCase()}_DEBUG`] ? 'inherit' : 'pipe',
137+
stdio: core.isDebug() || !!process.env[`${envPrefix}_DEBUG`] ? 'inherit' : 'pipe',
107138
});
108139

109140
core.info(`Installed binary to ${binPath}`);
110141

111142
core.info('Checking version');
112143

113144
try {
114-
await execa(binPath, ['--version'], { stdio: 'inherit' });
145+
const result = await execa(binPath, ['--version'], { stdio: 'pipe' });
146+
147+
if (result.stdout) {
148+
// eslint-disable-next-line require-atomic-updates
149+
process.env[`${envPrefix}_CLI_VERSION`] = result.stdout.replace(bin, '').trim();
150+
151+
core.info(result.stdout);
152+
}
115153
} catch (error) {
116154
core.error(String(error));
117155
}

index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
getShimsDir,
99
getToolchainCacheKey,
1010
getToolsDir,
11+
getUidFile,
1112
installBin,
1213
isCacheEnabled,
1314
isUsingMoon,
@@ -22,12 +23,11 @@ async function restoreCache() {
2223

2324
const primaryKey = await getToolchainCacheKey();
2425
const cachePrefix = getCacheKeyPrefix();
26+
2527
const cacheKey = await cache.restoreCache(
26-
[getPluginsDir(), getToolsDir()],
28+
[getPluginsDir(), getToolsDir(), getUidFile()],
2729
primaryKey,
2830
[`${cachePrefix}-${process.platform}`, cachePrefix],
29-
{},
30-
false,
3131
);
3232

3333
if (cacheKey) {
@@ -50,14 +50,14 @@ async function run() {
5050
core.addPath(binDir);
5151
core.addPath(shimsDir);
5252

53-
await restoreCache();
54-
5553
await installBin('proto');
5654

5755
if (isUsingMoon()) {
5856
await installBin('moon');
5957
}
6058

59+
await restoreCache();
60+
6161
if (core.getBooleanInput('auto-install')) {
6262
core.info('Auto-installing tools');
6363

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@moonrepo/setup-toolchain",
3-
"version": "0.2.1",
3+
"version": "0.3.0",
44
"description": "A GitHub action to setup and cache the proto and moon toolchains.",
55
"main": "dist/index.js",
66
"scripts": {
@@ -26,15 +26,15 @@
2626
"execa": "^5.1.1"
2727
},
2828
"devDependencies": {
29-
"@types/node": "^20.9.4",
29+
"@types/node": "^20.10.4",
3030
"@vercel/ncc": "^0.38.1",
31-
"eslint": "^8.54.0",
32-
"eslint-config-moon": "^2.0.12",
33-
"prettier": "^3.1.0",
31+
"eslint": "^8.56.0",
32+
"eslint-config-moon": "^2.0.13",
33+
"prettier": "^3.1.1",
3434
"prettier-config-moon": "^1.1.2",
35-
"ts-node": "^10.9.1",
35+
"ts-node": "^10.9.2",
3636
"tsconfig-moon": "^1.3.0",
37-
"typescript": "^5.3.2"
37+
"typescript": "^5.3.3"
3838
},
3939
"engines": {
4040
"node": ">=16.0.0"

0 commit comments

Comments
 (0)