【问题标题】:Cache dependencies in GitHub Actions on Windows在 Windows 上的 GitHub Actions 中缓存依赖项
【发布时间】:2021-11-18 21:09:16
【问题描述】:

我有一个在 Windows 操作系统上运行的 Github 操作。然后,为了缓存依赖项,我使用actions/cache@v2。但是,它没有按预期工作。当我看到调试日志时,缓存的大小只有30 B左右。

这是我正在使用的代码:

name: Build
on: push

jobs:
 build_on_win:
runs-on: windows-latest
if: "!contains(github.event.head_commit.message, 'skip-publish')"

steps:
  - uses: actions/checkout@v2
  - uses: actions/setup-node@master
    with:
      node-version: 15
  - name: Cache NPM dependencies
    uses: actions/cache@v2
    with:
      path: ~/.npm
      key: ${{ runner.OS }}-npm-cache-${{ hashFiles('**/package-lock.json') }}
      restore-keys: |
        ${{ runner.OS }}-npm-cache-
  - name: install dependencies
    run: npm install

  rest of the code..

非常感谢任何帮助!

【问题讨论】:

    标签: node.js npm caching github-actions


    【解决方案1】:

    缓存包依赖项是actions/setup-node@v2 中已经提供的功能。您不必配置 actions/cache@v2 而是您可以做的是使用如下所示的 setup-node@v2 操作,您只需要添加cache: 'npm',它会自动检测并缓存依赖关系。

    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-node@v2
      with:
        node-version: '14'
        cache: 'npm'
    - run: npm install
    - run: npm test
    

    如果你有monorepo,那么你也可以定义路径

    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-node@v2
      with:
        node-version: '14'
        cache: 'npm'
        cache-dependency-path: subdir/package-lock.json
    - run: npm install
    - run: npm test
    

    您可以在Caching packages dependencies 阅读更多相关信息。

    【讨论】:

    • 谢谢!有效!顺便说一句,安装依赖项大约需要1 minute and 90 seconds
    • 是的,安装依赖项需要时间,但如果您想减少安装时间,您可以使用npm ci 而不是npm install,还要确保在存储库中有package-lock.json
    • 哦!谢谢!让我试试吧!
    • 谢谢!它就像一个魅力......现在只需要大约53 s
    猜你喜欢
    • 2021-10-24
    • 2020-07-23
    • 2021-07-12
    • 2021-01-21
    • 2020-06-02
    • 2020-12-10
    • 2021-02-18
    • 2015-09-30
    • 2018-05-29
    相关资源
    最近更新 更多