|
| 1 | +######################################################################################## |
| 2 | +# "yarn install" composite action for yarn 3/4+ and "nodeLinker: node-modules" # |
| 3 | +#--------------------------------------------------------------------------------------# |
| 4 | +# Requirement: @setup/node should be run before # |
| 5 | +# # |
| 6 | +# Usage in workflows steps: # |
| 7 | +# # |
| 8 | +# - name: 📥 Monorepo install # |
| 9 | +# uses: ./.github/actions/yarn-nm-install # |
| 10 | +# with: # |
| 11 | +# enable-corepack: false # (default = 'false') # |
| 12 | +# cwd: ${{ github.workspace }}/apps/my-app # (default = '.') # |
| 13 | +# cache-prefix: add cache key prefix # (default = 'default') # |
| 14 | +# cache-node-modules: false # (default = 'false') # |
| 15 | +# cache-install-state: false # (default = 'false') # |
| 16 | +# # |
| 17 | +# Reference: # |
| 18 | +# - latest: https://gist.github.com/belgattitude/042f9caf10d029badbde6cf9d43e400a # |
| 19 | +# # |
| 20 | +# Versions: # |
| 21 | +# - 1.1.0 - 22-07-2023 - Option to enable npm global cache folder. # |
| 22 | +# - 1.0.4 - 15-07-2023 - Fix corepack was always enabled. # |
| 23 | +# - 1.0.3 - 05-07-2023 - YARN_ENABLE_MIRROR to false (speed up cold start) # |
| 24 | +# - 1.0.2 - 02-06-2023 - install-state default to false # |
| 25 | +# - 1.0.1 - 29-05-2023 - cache-prefix doc # |
| 26 | +# - 1.0.0 - 27-05-2023 - new input: cache-prefix # |
| 27 | +######################################################################################## |
| 28 | + |
| 29 | +name: 'Monorepo install (yarn)' |
| 30 | +description: 'Run yarn install with node_modules linker and cache enabled' |
| 31 | +inputs: |
| 32 | + cwd: |
| 33 | + description: "Changes node's process.cwd() if the project is not located on the root. Default to process.cwd()" |
| 34 | + required: false |
| 35 | + default: '.' |
| 36 | + cache-prefix: |
| 37 | + description: 'Add a specific cache-prefix' |
| 38 | + required: false |
| 39 | + default: 'default' |
| 40 | + cache-npm-cache: |
| 41 | + description: 'Cache npm global cache folder often used by node-gyp, prebuild binaries (invalidated on lock/os/node-version)' |
| 42 | + required: false |
| 43 | + default: 'true' |
| 44 | + cache-node-modules: |
| 45 | + description: 'Cache node_modules, might speed up link step (invalidated lock/os/node-version/branch)' |
| 46 | + required: false |
| 47 | + default: 'false' |
| 48 | + cache-install-state: |
| 49 | + description: 'Cache yarn install state, might speed up resolution step when node-modules cache is activated (invalidated lock/os/node-version/branch)' |
| 50 | + required: false |
| 51 | + default: 'false' |
| 52 | + enable-corepack: |
| 53 | + description: 'Enable corepack' |
| 54 | + required: false |
| 55 | + default: 'true' |
| 56 | + |
| 57 | +runs: |
| 58 | + using: 'composite' |
| 59 | + |
| 60 | + steps: |
| 61 | + - name: ⚙️ Enable Corepack |
| 62 | + if: inputs.enable-corepack == 'true' |
| 63 | + shell: bash |
| 64 | + working-directory: ${{ inputs.cwd }} |
| 65 | + run: corepack enable |
| 66 | + |
| 67 | + - name: ⚙️ Expose yarn config as "$GITHUB_OUTPUT" |
| 68 | + id: yarn-config |
| 69 | + shell: bash |
| 70 | + working-directory: ${{ inputs.cwd }} |
| 71 | + env: |
| 72 | + YARN_ENABLE_GLOBAL_CACHE: 'false' |
| 73 | + run: | |
| 74 | + echo "CACHE_FOLDER=$(yarn config get cacheFolder)" >> $GITHUB_OUTPUT |
| 75 | + echo "CURRENT_NODE_VERSION="node-$(node --version)"" >> $GITHUB_OUTPUT |
| 76 | + echo "CURRENT_BRANCH=$(echo ${GITHUB_REF#refs/heads/} | sed -r 's,/,-,g')" >> $GITHUB_OUTPUT |
| 77 | + echo "NPM_GLOBAL_CACHE_FOLDER=$(npm config get cache)" >> $GITHUB_OUTPUT |
| 78 | +
|
| 79 | + - name: ♻️ Restore yarn cache |
| 80 | + uses: actions/cache@v4 |
| 81 | + id: yarn-download-cache |
| 82 | + with: |
| 83 | + path: ${{ steps.yarn-config.outputs.CACHE_FOLDER }} |
| 84 | + key: yarn-download-cache-${{ inputs.cache-prefix }}-${{ hashFiles(format('{0}/yarn.lock', inputs.cwd), format('{0}/.yarnrc.yml', inputs.cwd)) }} |
| 85 | + restore-keys: | |
| 86 | + yarn-download-cache-${{ inputs.cache-prefix }}- |
| 87 | +
|
| 88 | + - name: ♻️ Restore node_modules |
| 89 | + if: inputs.cache-node-modules == 'true' |
| 90 | + id: yarn-nm-cache |
| 91 | + uses: actions/cache@v4 |
| 92 | + with: |
| 93 | + path: ${{ inputs.cwd }}/**/node_modules |
| 94 | + key: yarn-nm-cache-${{ inputs.cache-prefix }}-${{ runner.os }}-${{ steps.yarn-config.outputs.CURRENT_NODE_VERSION }}-${{ steps.yarn-config.outputs.CURRENT_BRANCH }}-${{ hashFiles(format('{0}/yarn.lock', inputs.cwd), format('{0}/.yarnrc.yml', inputs.cwd)) }} |
| 95 | + |
| 96 | + - name: ♻️ Restore global npm cache folder |
| 97 | + if: inputs.cache-npm-cache == 'true' |
| 98 | + id: npm-global-cache |
| 99 | + uses: actions/cache@v4 |
| 100 | + with: |
| 101 | + path: ${{ steps.yarn-config.outputs.NPM_GLOBAL_CACHE_FOLDER }} |
| 102 | + key: npm-global-cache-${{ inputs.cache-prefix }}-${{ runner.os }}-${{ steps.yarn-config.outputs.CURRENT_NODE_VERSION }}-${{ hashFiles(format('{0}/yarn.lock', inputs.cwd), format('{0}/.yarnrc.yml', inputs.cwd)) }} |
| 103 | + |
| 104 | + - name: ♻️ Restore yarn install state |
| 105 | + if: inputs.cache-install-state == 'true' && inputs.cache-node-modules == 'true' |
| 106 | + id: yarn-install-state-cache |
| 107 | + uses: actions/cache@v4 |
| 108 | + with: |
| 109 | + path: ${{ inputs.cwd }}/.yarn/ci-cache |
| 110 | + key: yarn-install-state-cache-${{ inputs.cache-prefix }}-${{ runner.os }}-${{ steps.yarn-config.outputs.CURRENT_NODE_VERSION }}-${{ steps.yarn-config.outputs.CURRENT_BRANCH }}-${{ hashFiles(format('{0}/yarn.lock', inputs.cwd), format('{0}/.yarnrc.yml', inputs.cwd)) }} |
| 111 | + |
| 112 | + - name: 📥 Install dependencies |
| 113 | + shell: bash |
| 114 | + working-directory: ${{ inputs.cwd }} |
| 115 | + run: yarn install --immutable --inline-builds |
| 116 | + env: |
| 117 | + # Overrides/align yarnrc.yml options (v3, v4) for a CI context |
| 118 | + YARN_ENABLE_GLOBAL_CACHE: 'false' # Use local cache folder to keep downloaded archives |
| 119 | + YARN_ENABLE_MIRROR: 'false' # Prevent populating global cache for caches misses (local cache only) |
| 120 | + YARN_NM_MODE: 'hardlinks-local' # Reduce node_modules size |
| 121 | + YARN_INSTALL_STATE_PATH: '.yarn/ci-cache/install-state.gz' # Might speed up resolution step when node_modules present |
| 122 | + # Other environment variables |
| 123 | + HUSKY: '0' # By default do not run HUSKY install |
0 commit comments