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 all 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
1 change: 1 addition & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ jobs:
needs:
- setup
- client_javascript
- client_javascript_algoliasearch
- client_java
- client_php
if: |
Expand Down
51 changes: 29 additions & 22 deletions scripts/ci/setRunVariables.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/* eslint-disable no-console */
import type { HashElementOptions } from 'folder-hash';
import crypto from 'crypto';

import { hashElement } from 'folder-hash';

import { toAbsolutePath } from '../common';

import { getNbGitDiff } from './utils';

const JS_CLIENT_FOLDER = 'clients/algoliasearch-client-javascript';
Expand All @@ -16,20 +19,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 +39,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 +95,28 @@ const VARIABLES_TO_CHECK: Array<{
},
];

async function computeCommonHash(): Promise<string> {
const hashGA = await hashElement(toAbsolutePath('.github'), {
encoding: 'hex',
folders: { exclude: ['ISSUE_TEMPLATE'] },
files: { include: ['*.yml', '.cache_version'] },
});
const hashScripts = await hashElement(toAbsolutePath('scripts'), {
encoding: 'hex',
folders: { exclude: ['docker', '__tests__'] },
});
const hashConfig = await hashElement(toAbsolutePath('.'), {
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 +135,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