如何压缩纱线全局文件夹以便在后续 Google Cloud Build 运行中重复使用

问题描述 投票:0回答:1

我正在尝试缓存我的

node_module
依赖项以加快构建时间,但生成的 zip 文件是空的。受到对另一个问题的THIS回答的启发,我有以下
cloudbuild.yaml

steps:
  - id: 'download-cached-yarn-dependencies'
    name: gcr.io/cloud-builders/gsutil
    entrypoint: bash
    args:
      - '-c'
      - |
        gsutil cp gs://${PROJECT_ID}_cloudbuild/my-app.cache.tgz build-cache.tgz || exit 0
        tar -zxf build-cache.tgz || exit 0


  - id: install
    name: $_NODE
    entrypoint: 'bash'
    args:
      - '-c'
      - |
        mkdir .yarn-cache
        yarn config set enableGlobalCache true
        yarn config set globalFolder .yarn-cache
        yarn install --immutable

  - id: 'upload-cached-yarn-dependencies'
    name: gcr.io/cloud-builders/gsutil
    entrypoint: bash
    args:
      - '-c'
      - |
        tar -zcvf build-cache.tgz .yarn-cache
        gsutil cp build-cache.tgz gs://${PROJECT_ID}_cloudbuild/my-app.cache.tgz

我对之前的ANSWER唯一修改的是添加

mkdir .yarn-cache
以避免“无法统计:没有这样的文件或目录”错误。

此构建成功在我的存储桶中创建了一个

my-app.cache.tgz
文件,但是解压后,它是空的。

我还尝试删除行

yarn config set globalFolder .yarn-cache
并使用默认缓存文件夹
/usr/local/share/.config/yarn/global
SOURCE),但这也给出了“无法统计:没有这样的文件或目录”错误。

如何成功压缩全局文件夹以便在以后的版本中重用?

caching google-cloud-storage node-modules hadoop-yarn google-cloud-build
1个回答
0
投票

我根据另一个问题的THIS答案修改了我的方法。

steps:
  # retrieve the cached node_modules from Cloud Storage
  - id: get-cache
    waitFor: ['-']
    name: 'gcr.io/cloud-builders/gsutil'
    # This approach should work for unpacking the local npm package cache.
    script: |
      #!/usr/bin/env bash
      set -Eeuo pipefail
      gsutil -m cp gs://${PROJECT_ID}_cloudbuild/node_modules.cache.tgz  /tmp/build-cache.tgz || echo "Cache archive not found!"
      tar -xzf /tmp/build-cache.tgz || echo "Cache archive not found!"

  # Install dependencies
  - id: install
    waitFor: ['get-cache']
    name: $_NODE
    entrypoint: 'bash'
    args:
      - '-c'
      - |
        yarn install --immutable --frozen-lockfile

  # Zip and upload the node_modules to Cloud Storage
  # - Uncomment to update the cache
  # - comment out to speed up the build
  - id: cache-node-dependencies
    name: 'gcr.io/cloud-builders/gsutil'
    waitFor: ['install']
    script: |
      #!/usr/bin/env bash
      set -Eeuo pipefail
      tar -czf /tmp/cache.tar.gz ./node_modules &&
      gsutil -m cp /tmp/cache.tar.gz gs://${PROJECT_ID}_cloudbuild/node_modules.cache.tgz
© www.soinside.com 2019 - 2024. All rights reserved.