github-actions - GitHub Action Cache with npm install -g(没有 package.json)

标签 github-actions

Github Action 缓存可以用于加速全局安装的节点工具吗?

我在 ruby 存储库上使用语义发布,我不想用 package.json 污染该存储库

我的语义释放配置位于 .releaserc

我可以运行这个 GitAction 来更新我的 SemVer。

name: SemVer
on:
  push:
    branches: [ main ]
jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: '16'

      - name: Install Global Dependencies
        run: npm -g install semantic-release @semantic-release/changelog @semantic-release/commit-analyzer @semantic-release/exec @semantic-release/git @semantic-release/release-notes-generator

      - name: Run SemVer
        run: semantic-release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

安装依赖需要22秒

enter image description here

我已经尝试将我发现的缓存配置集成到我的工作流程中,但这不起作用,我认为这是因为没有 package-lock.json 来构建哈希。

key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}

name: SemVer
on:
  push:
    branches: [ main ]
jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: '16'

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

      - name: Install Global Dependencies
        if: steps.cache.outputs.cache-hit != 'true'
        run: npm -g install semantic-release @semantic-release/changelog @semantic-release/commit-analyzer @semantic-release/exec @semantic-release/git @semantic-release/release-notes-generator

      - name: Run SemVer
        run: semantic-release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

enter image description here

最佳答案

如果你没有一个package-lock.json,你不需要散列,你可以散列任何其他文件或一个都不散列,例如:

with:
  path: ~/.npm
  key: ${{ runner.os }}-build-${{ env.cache-name }}

文档可在此处获得https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows

关于github-actions - GitHub Action Cache with npm install -g(没有 package.json),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70797616/

相关文章:

visual-studio - "Visual Studio 16 2019"停止使用 GitHub Actions

c# - 如何为.NET项目中的单元测试创​​建GitHub Action?

javascript - 如何在 JS 文件中使用 Github secret

github - 在 GitHub Actions 中将步骤设置为超时成功

github-actions - GitHub Action 构建矩阵 : How does the $ work?

c - github actions 输出到哪里去了?

react-native - Github Actions expo 上传 :ios returning Invalid credentials

GitHub Actions ***NO_CI*** 替代方案

python - 无法在 github 操作中创建(python)QApplication

github - github.event 的简单回显在 Github Actions 中失败