Skip to content

fix(ci): compute the correct hash #438

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

Merged
merged 7 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
2 changes: 1 addition & 1 deletion .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ runs:
- name: Compute cache common hash
if: inputs.type != 'minimal'
shell: bash
run: echo "CACHE_COMMON_HASH=${{ steps.diff.outputs.GITHUB_ACTIONS_CHANGED_HASH }}-${{ steps.diff.outputs.SCRIPTS_CHANGED_HASH }}" >> $GITHUB_ENV
run: echo "CACHE_COMMON_HASH=${{ steps.diff.outputs.COMMON_HASH }}" >> $GITHUB_ENV

- name: Compute specs matrix
if: inputs.type != 'minimal'
Expand Down
49 changes: 27 additions & 22 deletions scripts/ci/setRunVariables.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable no-console */
import type { HashElementOptions } from 'folder-hash';
import crypto from 'crypto';

import { hashElement } from 'folder-hash';

import { getNbGitDiff } from './utils';
Expand All @@ -16,20 +17,10 @@ const JS_CLIENT_FOLDER = 'clients/algoliasearch-client-javascript';
*
* The variable will be accessible in the CI via `steps.diff.outputs.<name>`.
*/
const VARIABLES_TO_CHECK: Array<{
name: string;
path: string[];
needHash?: boolean;
hashOptions?: HashElementOptions;
}> = [
const VARIABLES_TO_CHECK = [
{
name: 'GITHUB_ACTIONS_CHANGED',
path: ['.github/actions', '.github/workflows', '.github/.cache_version'],
needHash: true,
hashOptions: {
folders: { include: ['.github/actions', '.github/workflows'] },
files: { include: ['.github/.cache_version'] },
},
},
{
name: 'SPECS_CHANGED',
Expand All @@ -46,10 +37,6 @@ const VARIABLES_TO_CHECK: Array<{
{
name: 'SCRIPTS_CHANGED',
path: ['scripts'],
needHash: true,
hashOptions: {
folders: { include: ['scripts'] },
},
},
{
name: 'GENERATORS_CHANGED',
Expand Down Expand Up @@ -106,6 +93,28 @@ const VARIABLES_TO_CHECK: Array<{
},
];

async function computeCommonHash(): Promise<string> {
const hashGA = await hashElement('../.github', {
Copy link
Member

Choose a reason for hiding this comment

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

Ok last comment I swear: do we maybe want to use the toAbsolutePath fn here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

if you want ahah, the CI is flaky anyway there will be some merge later

encoding: 'hex',
folders: { exclude: ['ISSUE_TEMPLATE'] },
files: { include: ['*.yml', '.cache_version'] },
});
const hashScripts = await hashElement('../scripts', {
encoding: 'hex',
folders: { exclude: ['docker', '__tests__'] },
});
const hashConfig = await hashElement('../', {
encoding: 'hex',
folders: { include: ['config'] },
files: { include: ['openapitools.json', 'clients.config.json'] },
});

return crypto
.createHash('sha256')
.update(`${hashGA.hash}-${hashScripts.hash}-${hashConfig.hash}`)
.digest('hex');
}

/**
* Outputs variables used in the CI to determine if a job should run.
*/
Expand All @@ -124,14 +133,10 @@ async function setRunVariables({

console.log(`Found ${diff} changes for '${check.name}'`);
console.log(`::set-output name=${check.name}::${diff}`);
if (diff && check.needHash) {
const hash = (
await hashElement('.', { encoding: 'hex', ...check.hashOptions })
).hash;
console.log(`::set-output name=${check.name}_HASH::${hash}`);
}
}

console.log(`::set-output name=COMMON_HASH::${await computeCommonHash()}`);

console.log(`::set-output name=ORIGIN_BRANCH::${originBranch}`);
}

Expand Down