From 39ab5e8d7c7aa30498693d7161724eca48b51e5c Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Wed, 27 Apr 2022 14:33:24 +0200 Subject: [PATCH] fix(ci): compute the correct hash (#438) --- .github/actions/setup/action.yml | 2 +- .github/workflows/check.yml | 1 + scripts/ci/setRunVariables.ts | 51 ++++++++++++++++++-------------- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/.github/actions/setup/action.yml b/.github/actions/setup/action.yml index 3214e2186b..1fc0bb4cb9 100644 --- a/.github/actions/setup/action.yml +++ b/.github/actions/setup/action.yml @@ -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' diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 6f1626e0b0..ca6c5b50d3 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -370,6 +370,7 @@ jobs: needs: - setup - client_javascript + - client_javascript_algoliasearch - client_java - client_php if: | diff --git a/scripts/ci/setRunVariables.ts b/scripts/ci/setRunVariables.ts index d83a968498..1e5d8784d8 100644 --- a/scripts/ci/setRunVariables.ts +++ b/scripts/ci/setRunVariables.ts @@ -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'; @@ -16,20 +19,10 @@ const JS_CLIENT_FOLDER = 'clients/algoliasearch-client-javascript'; * * The variable will be accessible in the CI via `steps.diff.outputs.`. */ -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', @@ -46,10 +39,6 @@ const VARIABLES_TO_CHECK: Array<{ { name: 'SCRIPTS_CHANGED', path: ['scripts'], - needHash: true, - hashOptions: { - folders: { include: ['scripts'] }, - }, }, { name: 'GENERATORS_CHANGED', @@ -106,6 +95,28 @@ const VARIABLES_TO_CHECK: Array<{ }, ]; +async function computeCommonHash(): Promise { + 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. */ @@ -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}`); }