7272 run : echo "actual-commit-hash=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
7373 shell : bash
7474
75- - run : corepack enable
75+ - run : COREPACK_ENABLE_UNSAFE_CUSTOM_URLS=1 corepack enable
7676 shell : bash
7777
7878 # In a low-risk environment, try to download cache of `node_modules`, if it
@@ -83,27 +83,26 @@ runs:
8383 uses : actions/cache/restore@v4
8484 with :
8585 path : ' **/node_modules'
86- key : node-modules-${{ steps.get-commit-hash.outputs.actual-commit-hash }}
86+ key : |
87+ node-modules-${{ steps.get-commit-hash.outputs.actual-commit-hash && steps.get-commit-hash.outputs.actual-commit-hash || github.sha }}
8788
8889 - name : Set up Node.js
8990 uses : actions/setup-node@v4
9091 id : setup-node
9192 with :
9293 node-version : ${{ inputs.node-version }}
9394 node-version-file : ${{ inputs.node-version == '' && '.nvmrc' || '' }}
94- # Always use setup-node's cache for Yarn in non-high-risk environments.
95- # This ensures Yarn is correctly installed/restored by setup-node, preventing
96- # Corepack from attempting to download Yarn and hitting rate limits.
97- # Notes on this approach:
98- # 1) Security for high-risk environments is maintained: If 'is-high-risk-environment'
99- # is 'true', setup-node's caching is disabled (evaluates to '').
100- # 2) Self-hosted runner compatibility: actions/setup-node is expected to handle cache
101- # misses gracefully. If issues arise, they might be specific to runner configuration.
102- # 3) Performance: While there might be a slight overhead if node_modules is already cached,
103- # the reliability gained by consistently caching Yarn via setup-node and avoiding
104- # rate-limit failures outweighs this. The .yarn folder is relatively small.
95+ # If the `node_modules` cache was not found, use `setup-node` cache to
96+ # restore the `.yarn` folder.
97+ # Notes: if this is always set to 'yarn':
98+ # 1) Will not be secure for high-risk environment.
99+ # 2) Self-hosted runners will fail to find this cache, and then fail
100+ # on the 'Post Setup environment' step
101+ # 3) This action will run a few seconds slower, because when we
102+ # restore the `node_modules` folder from cache, there's no need to
103+ # download the `.yarn` folder too
105104 # (GHA does not allow the : ? ternary operator, you must write && ||)
106- cache : ${{ inputs.is-high-risk-environment != 'true' && 'yarn' || '' }}
105+ cache : ${{ ( inputs.is-high-risk-environment != 'true' && steps.download-node-modules.outputs.cache-hit != 'true' ) && 'yarn' || '' }}
107106
108107 # If the node_modules cache was not found (or it's a high-risk environment),
109108 # run the yarn install
@@ -135,4 +134,5 @@ runs:
135134 uses : actions/cache/save@v4
136135 with :
137136 path : ' **/node_modules'
138- key : node-modules-${{ steps.get-commit-hash.outputs.actual-commit-hash }}
137+ key : |
138+ node-modules-${{ steps.get-commit-hash.outputs.actual-commit-hash && steps.get-commit-hash.outputs.actual-commit-hash || github.sha }}
0 commit comments