From acca4d2e7280df6a3eb92ee8e9e6b1501cafc235 Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Wed, 27 Apr 2022 10:58:10 +0200 Subject: [PATCH] chore(ci): include common changes to cache version (#433) --- .github/.cache_version | 2 +- .github/actions/cache/action.yml | 9 +++++++-- .github/actions/setup/action.yml | 8 ++++++-- .github/workflows/check.yml | 24 ++++++++++++++++++++++-- scripts/ci/setRunVariables.ts | 25 ++++++++++++++++++++++++- 5 files changed, 60 insertions(+), 8 deletions(-) diff --git a/.github/.cache_version b/.github/.cache_version index b93692a052..36eef14295 100644 --- a/.github/.cache_version +++ b/.github/.cache_version @@ -1 +1 @@ -8.0.10.0.8 +8.0.11 diff --git a/.github/actions/cache/action.yml b/.github/actions/cache/action.yml index 54ff04f120..2ab9fd6d2f 100644 --- a/.github/actions/cache/action.yml +++ b/.github/actions/cache/action.yml @@ -9,13 +9,18 @@ inputs: language: description: The language to retrieve dependencies. required: false + cache_hash: + description: The cache hash from github actions and scripts + required: false runs: using: composite steps: - name: Read current GitHub Actions cache version shell: bash - run: echo "CACHE_VERSION=$(< .github/.cache_version)" >> $GITHUB_ENV + run: | + echo "CACHE_VERSION_YARN=$(< .github/.cache_version)" >> $GITHUB_ENV + echo "CACHE_VERSION=$(< .github/.cache_version)-${{ inputs.cache_hash }}" >> $GITHUB_ENV # JavaScript setup - name: Get yarn cache directory path @@ -27,7 +32,7 @@ runs: uses: actions/cache@v2 with: path: ${{ steps.yarn-cache-dir.outputs.dir || '.yarn/cache' }} - key: node-cache-${{ env.CACHE_VERSION }}-yarn-${{ hashFiles('yarn.lock') }} + key: node-cache-${{ env.CACHE_VERSION_YARN }}-yarn-${{ hashFiles('yarn.lock') }} # Java setup: used during 'java' generation or 'cts' - name: Download Java formatter diff --git a/.github/actions/setup/action.yml b/.github/actions/setup/action.yml index 0160d554ad..3214e2186b 100644 --- a/.github/actions/setup/action.yml +++ b/.github/actions/setup/action.yml @@ -35,10 +35,9 @@ runs: node-version-file: .nvmrc cache: yarn - - name: Restore cache + - name: Run yarn install and install java formatter uses: ./.github/actions/cache with: - # We want to load the java formatter language: java - name: Setting diff outputs variables @@ -52,6 +51,11 @@ runs: yarn workspace scripts setRunVariables "$origin" + - 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 + - name: Compute specs matrix if: inputs.type != 'minimal' id: spec-matrix diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 5d6fcb27a7..6f1626e0b0 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -40,6 +40,7 @@ jobs: run: yarn eslint --ext=json . outputs: + CACHE_COMMON_HASH: ${{ env.CACHE_COMMON_HASH }} RUN_SCRIPTS: ${{ steps.setup.outputs.RUN_SCRIPTS }} RUN_SPECS: ${{ steps.setup.outputs.RUN_SPECS }} @@ -71,6 +72,8 @@ jobs: - name: Restore cache uses: ./.github/actions/cache + with: + cache_hash: ${{ needs.setup.outputs.CACHE_COMMON_HASH }} - name: Check script linting run: yarn scripts:lint @@ -93,6 +96,8 @@ jobs: - name: Restore cache uses: ./.github/actions/cache + with: + cache_hash: ${{ needs.setup.outputs.CACHE_COMMON_HASH }} - name: Cache '${{ matrix.client.name }}' bundled specs id: cache @@ -126,6 +131,8 @@ jobs: - name: Restore cache uses: ./.github/actions/cache + with: + cache_hash: ${{ needs.setup.outputs.CACHE_COMMON_HASH }} - name: Cache '${{ matrix.client }}' client id: cache @@ -164,6 +171,7 @@ jobs: with: job: client language: javascript + cache_hash: ${{ needs.setup.outputs.CACHE_COMMON_HASH }} - name: Cache '${{ matrix.client.name }}' client id: cache @@ -215,6 +223,7 @@ jobs: with: job: client language: java + cache_hash: ${{ needs.setup.outputs.CACHE_COMMON_HASH }} - name: Cache '${{ matrix.client.name }}' client id: cache @@ -264,6 +273,7 @@ jobs: with: job: client language: php + cache_hash: ${{ needs.setup.outputs.CACHE_COMMON_HASH }} - name: Cache '${{ matrix.client.name }}' client id: cache @@ -295,7 +305,9 @@ jobs: client_javascript_algoliasearch: timeout-minutes: 10 runs-on: ubuntu-20.04 - needs: client_javascript + needs: + - setup + - client_javascript if: | always() && needs.setup.outputs.RUN_JS_ALGOLIASEARCH == 'true' && @@ -309,6 +321,7 @@ jobs: with: job: codegen language: javascript + cache_hash: ${{ needs.setup.outputs.CACHE_COMMON_HASH }} - name: Cache 'algoliasearch' client id: cache @@ -331,7 +344,9 @@ jobs: client_javascript_tests: runs-on: ubuntu-20.04 timeout-minutes: 10 - needs: client_javascript + needs: + - setup + - client_javascript if: | always() && needs.setup.outputs.RUN_JS_TESTS == 'true' && @@ -344,6 +359,7 @@ jobs: uses: ./.github/actions/cache with: job: cts + cache_hash: ${{ needs.setup.outputs.CACHE_COMMON_HASH }} - name: Run client-common tests run: yarn workspace @experimental-api-clients-automation/client-common test @@ -352,6 +368,7 @@ jobs: runs-on: ubuntu-20.04 timeout-minutes: 20 needs: + - setup - client_javascript - client_java - client_php @@ -371,6 +388,7 @@ jobs: uses: ./.github/actions/cache with: job: cts + cache_hash: ${{ needs.setup.outputs.CACHE_COMMON_HASH }} - name: Push generated code to generated branch id: pushGeneratedCode @@ -398,6 +416,7 @@ jobs: runs-on: ubuntu-20.04 timeout-minutes: 10 needs: + - setup - cts - client_javascript_tests - client_javascript_algoliasearch @@ -417,6 +436,7 @@ jobs: uses: ./.github/actions/cache with: job: codegen + cache_hash: ${{ needs.setup.outputs.CACHE_COMMON_HASH }} - name: Push generated code to main id: pushGeneratedCode diff --git a/scripts/ci/setRunVariables.ts b/scripts/ci/setRunVariables.ts index e8e7be2d46..d83a968498 100644 --- a/scripts/ci/setRunVariables.ts +++ b/scripts/ci/setRunVariables.ts @@ -1,4 +1,7 @@ /* eslint-disable no-console */ +import type { HashElementOptions } from 'folder-hash'; +import { hashElement } from 'folder-hash'; + import { getNbGitDiff } from './utils'; const JS_CLIENT_FOLDER = 'clients/algoliasearch-client-javascript'; @@ -13,10 +16,20 @@ const JS_CLIENT_FOLDER = 'clients/algoliasearch-client-javascript'; * * The variable will be accessible in the CI via `steps.diff.outputs.`. */ -const VARIABLES_TO_CHECK = [ +const VARIABLES_TO_CHECK: Array<{ + name: string; + path: string[]; + needHash?: boolean; + hashOptions?: HashElementOptions; +}> = [ { 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', @@ -33,6 +46,10 @@ const VARIABLES_TO_CHECK = [ { name: 'SCRIPTS_CHANGED', path: ['scripts'], + needHash: true, + hashOptions: { + folders: { include: ['scripts'] }, + }, }, { name: 'GENERATORS_CHANGED', @@ -107,6 +124,12 @@ 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=ORIGIN_BRANCH::${originBranch}`);