如何在 GitHub 操作中缓存 yarn 包
How to cache yarn packages in GitHub Actions
我正在使用 GitHub 操作来构建我的 TypeScript 项目。
每次我 运行 操作时,我都在等待 3 分钟以安装所有依赖项。
有没有办法缓存 yarn 依赖项,这样构建时间会更快?
我试过这个:
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
- uses: actions/cache@v1
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install yarn
run: npm install -g yarn
- name: Install project dependencies
run: yarn
但构建时间仍然相同。
如缓存步骤 id
字段旁边的注释中所述:
Use this to check for cache-hit
(steps.yarn-cache.outputs.cache-hit != 'true'
)
您缺少一个条件 if
属性 来确定步骤是否应该 运行:
- name: Install yarn
run: npm install -g yarn
- name: Install project dependencies
if: steps.yarn-cache.outputs.cache-hit != 'true' # Over here!
run: yarn
P.S。您可能应该使用 Setup NodeJS GitHub 为您额外设置 Yarn 的操作:
- uses: actions/setup-node@v1
with:
node-version: '10.x' # The version spec of the version to use.
有关有效输入的完整列表,请参阅 action.yml
file。
编辑:事实证明,Yarn 包含在 software installed on the GitHub-hosted Ubuntu 18.04.4 LTS (ubuntu-latest
/ubuntu-18.04
) runner 的列表中,因此无需包含全局安装 Yarn 的步骤。
这是一个专门用于 Yarn 的 1-liner 缓存:https://github.com/c-hive/gha-yarn-cache
- uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: 12.x
- uses: c-hive/gha-yarn-cache@v1
- name: Install JS dependencies
run: yarn install
- name: Test
run: yarn test
它按照 GitHub 的建议进行缓存。支持 Yarn v1 和 v2。
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
- uses: actions/cache@v1
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
上面的缓存代码只缓存和恢复yarn缓存目录,不缓存node_modules
目录。因此,如果您使用此代码(@Edric 的回答),
- name: Install project dependencies
if: steps.yarn-cache.outputs.cache-hit != 'true' # Over here!
run: yarn
node_modules
未创建,您将收到未找到依赖项错误。
相反,您可以使用这个:
- name: Install project dependencies
run: yarn --prefer-offline
这告诉 yarn
始终 运行 但尽可能使用缓存下载(在上述缓存目录中)而不是从服务器下载。
也可以直接缓存node_modules
目录,有缓存时跳过安装步骤。这实际上是不推荐的(见评论)。示例:
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
- name: Cache yarn cache
uses: actions/cache@v2
id: cache-yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Cache node_modules
id: cache-node-modules
uses: actions/cache@v2
with:
path: node_modules
key: ${{ runner.os }}-${{ matrix.node-version }}-nodemodules-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-${{ matrix.node-version }}-nodemodules-
- run: yarn
if: |
steps.cache-yarn-cache.outputs.cache-hit != 'true' ||
steps.cache-node-modules.outputs.cache-hit != 'true'
正如 readme of the github package 所说:
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '14'
cache: 'npm' # or yarn
- run: npm install
- run: npm test
编辑:
事实证明,文档的编写方式非常具有误导性,他们进行了更新以明确其不缓存 node_modules
文件夹,而仅缓存全局缓存目录,如 this issue.
中所述
也如 Mrchief 在评论中所述:
... you'll still incur the npm i time
, just save on download time from internet (if the module is in npm cache)
所以你仍然应该使用这个节省从互联网下载包的时间,但是如果你想缓存 node_modules
文件夹,请检查它使用 actions/cache.
的其他答案
您还应该检查 答案及其关于为什么您 不应该 缓存 node_modules
文件夹的评论。
我正在使用 GitHub 操作来构建我的 TypeScript 项目。 每次我 运行 操作时,我都在等待 3 分钟以安装所有依赖项。
有没有办法缓存 yarn 依赖项,这样构建时间会更快?
我试过这个:
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
- uses: actions/cache@v1
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install yarn
run: npm install -g yarn
- name: Install project dependencies
run: yarn
但构建时间仍然相同。
如缓存步骤 id
字段旁边的注释中所述:
Use this to check for
cache-hit
(steps.yarn-cache.outputs.cache-hit != 'true'
)
您缺少一个条件 if
属性 来确定步骤是否应该 运行:
- name: Install yarn
run: npm install -g yarn
- name: Install project dependencies
if: steps.yarn-cache.outputs.cache-hit != 'true' # Over here!
run: yarn
P.S。您可能应该使用 Setup NodeJS GitHub 为您额外设置 Yarn 的操作:
- uses: actions/setup-node@v1
with:
node-version: '10.x' # The version spec of the version to use.
有关有效输入的完整列表,请参阅 action.yml
file。
编辑:事实证明,Yarn 包含在 software installed on the GitHub-hosted Ubuntu 18.04.4 LTS (ubuntu-latest
/ubuntu-18.04
) runner 的列表中,因此无需包含全局安装 Yarn 的步骤。
这是一个专门用于 Yarn 的 1-liner 缓存:https://github.com/c-hive/gha-yarn-cache
- uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: 12.x
- uses: c-hive/gha-yarn-cache@v1
- name: Install JS dependencies
run: yarn install
- name: Test
run: yarn test
它按照 GitHub 的建议进行缓存。支持 Yarn v1 和 v2。
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
- uses: actions/cache@v1
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
上面的缓存代码只缓存和恢复yarn缓存目录,不缓存node_modules
目录。因此,如果您使用此代码(@Edric 的回答),
- name: Install project dependencies
if: steps.yarn-cache.outputs.cache-hit != 'true' # Over here!
run: yarn
node_modules
未创建,您将收到未找到依赖项错误。
相反,您可以使用这个:
- name: Install project dependencies
run: yarn --prefer-offline
这告诉 yarn
始终 运行 但尽可能使用缓存下载(在上述缓存目录中)而不是从服务器下载。
也可以直接缓存node_modules
目录,有缓存时跳过安装步骤。这实际上是不推荐的(见评论)。示例:
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
- name: Cache yarn cache
uses: actions/cache@v2
id: cache-yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Cache node_modules
id: cache-node-modules
uses: actions/cache@v2
with:
path: node_modules
key: ${{ runner.os }}-${{ matrix.node-version }}-nodemodules-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-${{ matrix.node-version }}-nodemodules-
- run: yarn
if: |
steps.cache-yarn-cache.outputs.cache-hit != 'true' ||
steps.cache-node-modules.outputs.cache-hit != 'true'
正如 readme of the github package 所说:
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '14'
cache: 'npm' # or yarn
- run: npm install
- run: npm test
编辑:
事实证明,文档的编写方式非常具有误导性,他们进行了更新以明确其不缓存 node_modules
文件夹,而仅缓存全局缓存目录,如 this issue.
也如 Mrchief 在评论中所述:
... you'll still incur the
npm i time
, just save on download time from internet (if the module is in npm cache)
所以你仍然应该使用这个节省从互联网下载包的时间,但是如果你想缓存 node_modules
文件夹,请检查它使用 actions/cache.
您还应该检查 node_modules
文件夹的评论。