微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用github操作缓存npm依赖项

如何解决使用github操作缓存npm依赖项

我想缓存npm依赖项,这样我每次推入时都不会进行npm安装,而是从缓存中加载它。

我认为github action现在支持吗?:How do I cache steps in GitHub actions?

这里是少数情况

  • 如果package.json发生了变化,这意味着yarn.lockpackage-lock.json发生了变化,那么npm也会安装并更新缓存
  • 根据我的观点,贡献者可能同时在做yarn installnpm install

根据上述相同的问题,我将github操作更改为类似的内容

name: Tsc compilation test
on: [push,pull_request]
jobs:
  build:
    name: Build
    runs-on: ubuntu-18.04
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Cache NPM dependencies
        uses: actions/cache@v1
        with:
          path: ~/.npm
          key: ${{ runner.OS }}-npm-cache-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.OS }}-npm-cache-
      - name: Install dependencies
        run: npm install
      - name: Test tsc
        run: npm run ts-compile-check

这仍然npm install仍然有效,并且没有减少安装依赖项的计算时间(因此,我不确定这是否工作正常)

然后我做了yarn install axios,希望它可以更新我的缓存,但是在安装后,我将其视为已记录

Post job cleanup.
Cache hit occurred on the primary key Linux-npm-cache-,not saving cache.

这是我的问题,能否实现

  • 如果package.json发生了变化,这意味着yarn.lockpackage-lock.json发生了变化,npm install也会发生变化并更新缓存
  • 根据我的观点,贡献者可能同时在做yarn installnpm install

有人可以向我解释一下

    with:
      path: ~/.npm
      key: ${{ runner.OS }}-npm-cache-${{ hashFiles('**/package-lock.json') }}
      restore-keys: |
        ${{ runner.OS }}-npm-cache-

解决方法

为了使用GitHub动作进行有效的缓存,需要显示package-lock.jsonyarn.lock。安装软件包时会自动生成此文件。如果您需要有关package-lock.json的更多信息,请check out the docs

现在有一个问题,是否应在同一项目中使用npm和yarn。 More about that subject here

基于该问题,我们假设package-lock.json和yarn.lock都存在。如果您仅使用两者之一,请随时从下面移除其中之一。以下配置适用于纱线版本2,该版本使用yarn config get cacheFolder获取缓存文件夹。对于另一种纱线see the docs

name: Tsc compilation test
on: [push,pull_request]
jobs:
  build:
    name: Build
    runs-on: ubuntu-18.04
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Get yarn cache directory path
        id: yarn-cache-dir-path
        run: echo "::set-output name=dir::$(yarn config get cacheFolder)"

      - name: Cache yarn dependencies
        uses: actions/cache@v2
        with:
          path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-

      - name: Cache npm dependencies
        uses: actions/cache@v2
        with:
          path: '~/.npm'
          key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-

      - name: Install dependencies
        run: npm install # or yarn install

      - name: Test tsc
        run: npm run ts-compile-check

就是这样!好吧,除了一件事。上面我们使用~/.npm来缓存npm依赖项。从性能角度来看,缓存**/node_modules会更快,但是在某些情况下会引入冲突。随意尝试看看哪个对您有用。有关herehere的更多信息。


从您的问题中,您还要求解释以下代码:

    with:
      path: ~/.npm
      key: ${{ runner.OS }}-npm-cache-${{ hashFiles('**/package-lock.json') }}
      restore-keys: |
        ${{ runner.OS }}-npm-cache-

https://github.com/actions/cache#usage

路径-要缓存和还原的文件,目录和通配符模式的列表。有关支持的模式,请参见@ actions / glob。
-用于还原和保存缓存的显式键
restore-keys (还原密钥)-如果密钥没有发生高速缓存命中,则用于恢复高速缓存的密钥的有序列表

因此,上面的代码:

  • path是将被缓存/还原的文件夹(安装依赖项的地方)
  • key是将要缓存的路径的唯一标识符。在这种情况下,它使用**/package-lock.json对任何hashFiles文件的内容进行哈希处理。基本上,当package-lock.json文件更改时,这意味着依赖项已更改,并且不应使用缓存。
  • restore-keys基本上是默认键,以防key不匹配

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。