我们可以在 github 操作中缓存纱线全局变量吗
Posted
技术标签:
【中文标题】我们可以在 github 操作中缓存纱线全局变量吗【英文标题】:Can we cache yarn globals in github actions 【发布时间】:2020-09-10 19:34:17 【问题描述】:我有一些全局包,例如无服务器框架、ESLint 等。我已经为 yarn 实现了 GitHub Actions 缓存。下面是我的代码。
- 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: Adding serverless globally
run: yarn global add serverless
- name: Yarn Install
if: steps.yarn-cache.outputs.cache-hit != 'true'
run: |
echo "cache hit failed"
yarn install
env:
CI: false
但是我的全局包没有被缓存。有没有办法缓存 Yarn 全局变量?
【问题讨论】:
为什么在“Yarn Install”步骤中将CI
环境变量设置为false
?
@Edric 这是一个错误。我刚刚复制了我的 yarn 构建块并为 Yarn Install 重命名。
您是否关注tutorial 启用了纱线包的缓存?
@smac89 是的,我做到了。纱线缓存工作正常。问题在于纱线全局缓存。并且在将一些纱线包安装为全局之后,github操作也无法调用它。所以我目前正在使用 NPM 进行全局安装。但是缓存仍然不起作用。
【参考方案1】:
我正在粘贴解决方案的构建文件,
name: global-test
on:
push:
branches:
- dev
pull_request:
branches:
- dev
jobs:
aws-deployment:
runs-on: ubuntu-latest
steps:
- name: CHECKOUT ACTION
uses: actions/checkout@v2
- name: NODE SETUP ACTION
uses: actions/setup-node@v1
with:
node-version: '12.x'
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: |
echo "::set-output name=dir::$(yarn cache dir)"
- name: Set yarn global bin path
run: |
yarn config set prefix $(yarn cache dir)
- name: Add yarn bin path to system path
run: |
echo $(yarn global bin) >> $GITHUB_PATH
- name: Set yarn global installation path
run: |
yarn config set global-folder $(yarn cache dir)
- name: CACHE ACTION
uses: actions/cache@v2
env:
cache-version: v1
id: yarn-cache
with:
path: |
$ steps.yarn-cache-dir-path.outputs.dir
**/node_modules
key: $ runner.os -yarn-$ env.cache-version -$ hashFiles('**/yarn.lock')
restore-keys: |
$ runner.os -yarn-$ env.cache-version -
$ runner.os -yarn-
$ runner.os -
- name: Installing dependencies
if: steps.yarn-cache.outputs.cache-hit != 'true'
run: |
echo "YARN CACHE CHANGED"
yarn install
- name: Adding serverless globally
if: steps.yarn-cache.outputs.cache-hit != 'true'
run: |
echo "NO CACHE HIT"
yarn global add serverless
我为这些步骤命名,以便他们可以理解。
2020-12-06 更新了答案
【讨论】:
这会抛出:The 'add-path' command is disabled. Please upgrade to using Environment Files or opt into unsecure command execution by setting the 'ACTIONS_ALLOW_UNSECURE_COMMANDS' environment variable to 'true'. For more information see: github.blog/changelog/2020-10-01-github-actions-deprecating-set-env-and-add-path-commands
以上是关于我们可以在 github 操作中缓存纱线全局变量吗的主要内容,如果未能解决你的问题,请参考以下文章
Github 操作似乎无法在带有纱线工作区和 lerna 的 monorepo 中找到私有包