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