在 GCS 上使用 Google Cloud Build 部署失败

Deployment failing using Google Cloud Build on to GCS

我在 Google 云存储上有一个 reactjs 静态应用程序 运行。使用 Cloud Build,我正在尝试自动化构建和部署过程。

以下是 cloudbuild.yaml 文件:

steps:
  # Install
  - name: 'gcr.io/cloud-builders/npm'
    entrypoint: 'npm'
    args: ['install']

  # Test
  #- name: 'gcr.io/cloud-builders/npm'
    #entrypoint: 'npm'
    #args: ['test', 'test:ci']

  # Build
  - name: 'gcr.io/cloud-builders/npm'
    entrypoint: 'npm'
    args: ['build']

  # Syncing to Static Hosting on GCS  
  - name: 'gcr.io/cloud-builders/gsutil'
    args: ["-m", "rsync", "-r", "-c", "-d", "./build", "gs://mybucket.com"]

同步到 GCS 的最后一步失败。我遇到以下错误:

Starting Step #0
Step #0: Already have image (with digest): gcr.io/cloud-builders/npm
Step #0: npm WARN saveError ENOENT: no such file or directory, open '/workspace/package.json'
Step #0: npm notice created a lockfile as package-lock.json. You should commit this file.
Step #0: npm WARN enoent ENOENT: no such file or directory, open '/workspace/package.json'
Step #0: npm WARN workspace No description
Step #0: npm WARN workspace No repository field.
Step #0: npm WARN workspace No README data
Step #0: npm WARN workspace No license field.
Step #0: 
Step #0: up to date in 0.711s
Step #0: found 0 vulnerabilities
Step #0: 
Finished Step #0
Starting Step #1
Step #1: Already have image (with digest): gcr.io/cloud-builders/npm
Finished Step #1
Starting Step #2
Step #2: Already have image (with digest): gcr.io/cloud-builders/gsutil
Step #2: CommandException: arg (./build) does not name a directory, bucket, or bucket subdir.
Finished Step #2
ERROR
ERROR: build step 2 "gcr.io/cloud-builders/gsutil" failed: step exited with non-zero status: 1

我的项目结构:

RootProject
  -> front-end-webapp
           -> src/
           -> cloudbuild.yaml
           -> build/
           -> public/
           -> package.json
  -> backend-service1
  -> backend-service2

我写的触发器指向 front-end-webapp 子文件夹中的 cloudbuild.yaml 文件。 当我手动执行

npm run build or yarn build

然后我在 front-end-webapp 下的构建文件夹中获取生产就绪文件,我想将其放入 Google 云存储。只有构建文件夹中的文件,而不是包含文件的构建文件夹本身。

我试了很多,还是没能搞定。如果我做 。而不是像 args 这样的 args 中的 './build':["-m", "rsync", "-r", "-c", "-d", "./build", "gs://mybucket.com"] 然后云构建成功执行,但它也会复制根项目中存在的完整文件和子文件夹。

构建作业由提交到 front-end-webapp 的文件触发,而您 运行 提交到 front-end-webapp.

cloudbuild.yaml 文件

BUT,repo 的根是 / 而不是 front-end-webapp。因此,添加 dir: 'front-end-webapp' attribute on each step。这会强制步骤开始进入正确的文件夹!

编辑

当我迷失在目录管理中时,我介绍了这个步骤以查看我的工作区中存在的内容。

- name: gcr.io/cloud-builders/gcloud:latest
  entrypoint: "ls"
  args: ["-lah","/workspace"]