Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: Stabilize CI dependency cache key #13401

Merged
merged 3 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .github/actions/install-dependencies/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ outputs:
runs:
using: "composite"
steps:
# we use a hash of yarn.lock as our cache key, because if it hasn't changed, our dependencies haven't changed,
# so no need to reinstall them
- name: Compute dependency cache key
id: compute_lockfile_hash
run: echo "hash=dependencies-${{ hashFiles('yarn.lock', 'packages/*/package.json', 'dev-packages/*/package.json') }}" >> "$GITHUB_OUTPUT"
run: node ./scripts/dependency-hash-key.js >> "$GITHUB_OUTPUT"
shell: bash

- name: Check dependency cache
Expand Down
71 changes: 71 additions & 0 deletions scripts/dependency-hash-key.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');

/**
* Build a cache key for the dependencies of the monorepo.
* In addition to the content of the yarn.lock file, we also include
* dependencies of all workspace packages in the cache key.
* This ensures that we get a consistent cache key even if a dependency change does not affect
* the yarn.lock file.
*/
function outputDependencyCacheKey() {
const lockfileContent = fs.readFileSync(path.join(process.cwd(), 'yarn.lock'), 'utf8');

const hashParts = [lockfileContent];

const packageJson = require(path.join(process.cwd(), 'package.json'));

const workspacePackages = packageJson.workspaces || [];

// Get the (e.g. @sentry/browser) of all workspace packages
mydea marked this conversation as resolved.
Show resolved Hide resolved
// we want to ignore their version numbers later
const workspacePackageNames = getWorkspacePackageNames(workspacePackages);

// Add the dependencies of the workspace itself
hashParts.push(getNormalizedDependencies(packageJson, workspacePackageNames));

// Now for each workspace package, add the dependencies
workspacePackages.forEach(workspace => {
const packageJsonPath = path.join(process.cwd(), workspace, 'package.json');
const packageJson = require(packageJsonPath);
hashParts.push(getNormalizedDependencies(packageJson, workspacePackageNames));
});

const hash = crypto.createHash('md5').update(hashParts.join('\n')).digest('hex');
// eslint-disable-next-line no-console
console.log(`hash=${hash}`);
}

function getNormalizedDependencies(packageJson, workspacePackageNames) {
const { dependencies, devDependencies } = packageJson;

const mergedDependencies = {
...devDependencies,
...dependencies,
};

const normalizedDependencies = {};

// Sort the keys to ensure a consistent order
Object.keys(mergedDependencies)
.sort()
.forEach(key => {
// If the dependency is a workspace package, ignore the version
// No need to invalidate a cache after every release
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't this mean that we might have outdated internal code in the cache?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the cache key to cache the node_modules folder. So all the workspace packages should not be there at all, so the version should not matter (I think? at least 😅 )

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 yeah I just wasn't sure how these workspace packages are handled within node modules

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, it's a good question 🤔 there are only some symlinks in there, but these should not change I believe between versions. IMHO we can try it and if there are problems we can revisit this particular check - but I'd expect it to be OK. We do capture added internal dependencies, which should be fine I think/hope 😅

const version = workspacePackageNames.includes(key) ? '**workspace**' : mergedDependencies[key];
normalizedDependencies[key] = version;
});

return JSON.stringify(normalizedDependencies);
}

function getWorkspacePackageNames(workspacePackages) {
return workspacePackages.map(workspace => {
const packageJsonPath = path.join(process.cwd(), workspace, 'package.json');
const packageJson = require(packageJsonPath);
return packageJson.name;
});
}

outputDependencyCacheKey();
Loading