From 07ebae1717d24ccfb06e6a76c3e16d10b8abf86d Mon Sep 17 00:00:00 2001 From: vscaiceanu-1a <86055112+vscaiceanu-1a@users.noreply.github.com> Date: Tue, 5 Nov 2024 16:41:28 +0200 Subject: [PATCH] fix(pipeline): cache yarn deps with corepack --- .../@o3r/pipeline/schematics/ng-add/index.ts | 8 +++++-- .../__dot__github/actions/setup/action.yml | 24 +++++++++++++------ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/packages/@o3r/pipeline/schematics/ng-add/index.ts b/packages/@o3r/pipeline/schematics/ng-add/index.ts index 5afa9fca0a..6e7a7362d7 100644 --- a/packages/@o3r/pipeline/schematics/ng-add/index.ts +++ b/packages/@o3r/pipeline/schematics/ng-add/index.ts @@ -26,12 +26,15 @@ function ngAddFn(options: NgAddSchematicsSchema): Rule { } context.logger.info(`Setting up pipeline for package manager: "${packageManager}" `); const setupCommand = packageManager === 'yarn' ? 'yarn install --immutable' : 'npm ci'; + const isYarn2 = packageManager === 'yarn' && tree.exists('/.yarnrc.yml'); + const yarnCacheFolder = isYarn2 ? 'yarn config get cacheFolder' : 'yarn cache dir'; const baseTemplateSource = apply(url(`./templates/${options.toolchain}`), [ template({ ...options, packageManager, setupCommand, actionVersionString, + yarnCacheFolder, dot: '.' }), move(tree.root.path) @@ -41,7 +44,7 @@ function ngAddFn(options: NgAddSchematicsSchema): Rule { if (!options.npmRegistry) { return tree; } - if (packageManager === 'yarn') { + if (isYarn2) { const yarnrcPath = '/.yarnrc.yml'; if (!tree.exists(yarnrcPath)) { tree.create(yarnrcPath, dump({'npmRegistryServer': options.npmRegistry}, {indent: 2})); @@ -50,7 +53,8 @@ function ngAddFn(options: NgAddSchematicsSchema): Rule { yarnrcContent.npmRegistryServer = options.npmRegistry; tree.overwrite(yarnrcPath, dump(yarnrcContent, {indent: 2})); } - } else if (packageManager === 'npm') { + } else { + // both npm and yarn 1 use .npmrc for the registry const npmrcPath = '/.npmrc'; if (!tree.exists(npmrcPath)) { tree.create(npmrcPath, `registry=${options.npmRegistry}`); diff --git a/packages/@o3r/pipeline/schematics/ng-add/templates/github/__dot__github/actions/setup/action.yml b/packages/@o3r/pipeline/schematics/ng-add/templates/github/__dot__github/actions/setup/action.yml index 2f6c6e6853..8554309218 100644 --- a/packages/@o3r/pipeline/schematics/ng-add/templates/github/__dot__github/actions/setup/action.yml +++ b/packages/@o3r/pipeline/schematics/ng-add/templates/github/__dot__github/actions/setup/action.yml @@ -6,16 +6,26 @@ runs: steps: - uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4 with: - node-version: 20 - cache: <%= packageManager %> + node-version: 20<% if (packageManager !== 'yarn') { %> + cache: <%= packageManager %><% } %> - name: Enable Corepack shell: bash - run: corepack enable - - name: Install -<% if (npmRegistry) { %> + run: corepack enable<% if (packageManager === 'yarn') { %> + - name: Get yarn cache directory path + shell: bash + id: yarn-cache-dir-path + run: echo "dir=$(<%= yarnCacheFolder %>)" >> $GITHUB_OUTPUT + - name: Cache dependencies + uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a # v4.1.2 + with: + path: ${{ steps.yarn-cache-dir-path.outputs.dir }} + key: ${{ runner.os }}-yarn-${{ hashFiles('yarn.lock') }} + restore-keys: | + ${{ runner.os }}-yarn + ${{ runner.os }}<% } %> + - name: Install<% if (npmRegistry) { %> env: COREPACK_NPM_REGISTRY: <%= npmRegistry %> - COREPACK_INTEGRITY_KEYS: "" -<% } %> + COREPACK_INTEGRITY_KEYS: ""<% } %> shell: bash run: <%= setupCommand %>