Google cloud platform 谷歌云构建触发器失败,错误为;错误:生成步骤0“;gcr.io/cloud builders/docker“;失败:步骤退出,状态为非零:1“;

Google cloud platform 谷歌云构建触发器失败,错误为;错误:生成步骤0“;gcr.io/cloud builders/docker“;失败:步骤退出,状态为非零:1“;,google-cloud-platform,google-cloud-build,Google Cloud Platform,Google Cloud Build,我正在尝试使用设置golang后端的连续部署,但当触发时,它失败,并出现以下错误: starting build "eba3ce39-caad-43f0-a255-0a3cacec4913" FETCHSOURCE Initialized empty Git repository in /workspace/.git/ From https://source.developers.google.com/p/my-porject/r/github_myusername_myproject.com

我正在尝试使用设置golang后端的连续部署,但当触发时,它失败,并出现以下错误:

starting build "eba3ce39-caad-43f0-a255-0a3cacec4913"

FETCHSOURCE
Initialized empty Git repository in /workspace/.git/
From https://source.developers.google.com/p/my-porject/r/github_myusername_myproject.com
 * branch            660796f575bae6860d6f96df60cfd631a730c3ae -> FETCH_HEAD
HEAD is now at 660796f cloudbuild.yaml
BUILD
Starting Step #0
Step #0: Already have image (with digest): gcr.io/cloud-builders/docker
Step #0: unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /workspace/Dockerfile: no such file or directory
Finished Step #0
ERROR
ERROR: build step 0 "gcr.io/cloud-builders/docker" failed: step exited with non-zero status: 1
我的项目文件结构如下所示:

project
    frontend
    backend
        main.go
        cloudbuild.yaml
        Dockerfile
steps:
  # Build the container image
  - name: "gcr.io/cloud-builders/docker"
    args:
      [
        "build",
        "-t",
        "gcr.io/my-project/github.com/username/project.com:$COMMIT_SHA",
        ".",
      ]
  # Push the image to Container Registry
  - name: "gcr.io/cloud-builders/docker"
    args:
      [
        "push",
        "gcr.io/my-project/github.com/username/project.com:$COMMIT_SHA",
      ]
  # Deploy image to Cloud Run
  - name: "gcr.io/cloud-builders/gcloud"
    args:
      - "run"
      - "deploy"
      - "[SERVICE_NAME]"
      - "--image"
      - "gcr.io/my-project/github.com/username/project.com:$COMMIT_SHA"
      - "--region"
      - "us-central1"
      - "--platform"
      - "managed"
images:
  - gcr.io/my-project/github.com/username/project.com
我的cloudbuild.yaml看起来像:

project
    frontend
    backend
        main.go
        cloudbuild.yaml
        Dockerfile
steps:
  # Build the container image
  - name: "gcr.io/cloud-builders/docker"
    args:
      [
        "build",
        "-t",
        "gcr.io/my-project/github.com/username/project.com:$COMMIT_SHA",
        ".",
      ]
  # Push the image to Container Registry
  - name: "gcr.io/cloud-builders/docker"
    args:
      [
        "push",
        "gcr.io/my-project/github.com/username/project.com:$COMMIT_SHA",
      ]
  # Deploy image to Cloud Run
  - name: "gcr.io/cloud-builders/gcloud"
    args:
      - "run"
      - "deploy"
      - "[SERVICE_NAME]"
      - "--image"
      - "gcr.io/my-project/github.com/username/project.com:$COMMIT_SHA"
      - "--region"
      - "us-central1"
      - "--platform"
      - "managed"
images:
  - gcr.io/my-project/github.com/username/project.com
我的Dockerfile看起来像

# Use the official Golang image to create a build artifact.
# This is based on Debian and sets the GOPATH to /go.
# https://hub.docker.com/_/golang
FROM golang:1.13 as builder

# Create and change to the app directory.
WORKDIR /app

# Retrieve application dependencies.
# This allows the container build to reuse cached dependencies.
COPY go.* ./
RUN go mod download

# Copy local code to the container image.
COPY . ./

# Build the binary.
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server

# Use the official Alpine image for a lean production container.
# https://hub.docker.com/_/alpine
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM alpine:3
RUN apk add --no-cache ca-certificates

# Copy the binary to the production image from the builder stage.
COPY --from=builder /app/server /server

# Run the web service on container startup.
CMD ["/server"]

我从中获得Dockerfile。

云构建触发器当前指向
/project/
,而您的目录结构如下所示:

project
    frontend
    backend
        main.go
        cloudbuild.yaml
        Dockerfile
执行触发器时,目录工作区被复制到
/workspace/
,因此它无法在其中找到Dockerfile

您可以将所有内容移动到同一工作目录

  .
    ├── main.go
    ├── cloudbuild.yaml
    ├── Dockerfile

如果您希望保持当前的目录结构,那么您的云构建触发器将需要指向
/project/backend/
。请注意,您可以使用
ls-la
linux命令检查目录结构。

云构建触发器当前指向
/project/
,而目录结构如下所示:

project
    frontend
    backend
        main.go
        cloudbuild.yaml
        Dockerfile
执行触发器时,目录工作区被复制到
/workspace/
,因此它无法在其中找到Dockerfile

您可以将所有内容移动到同一工作目录

  .
    ├── main.go
    ├── cloudbuild.yaml
    ├── Dockerfile

如果您希望保持当前的目录结构,那么您的云构建触发器将需要指向
/project/backend/
。请注意,您可以使用
ls-la
linux命令检查目录结构。

当您对github repo执行push命令时,云构建将触发并查找cloudbuild.yaml文件。通过编辑
配置
部分和
云构建配置文件(yaml或json)
,可以在创建构建触发器时指定cloudbuild.yaml位置,您可以在其中选择cloudbuild.yaml位置。在您的情况下,只需将其设置为
后端/cloudbuild.yaml

现在,这还不够,因为当构建开始时,docker build命令将按照您的第一步启动构建映像。但是,docker的构建上下文是
,这不应该是因为您的所有repo都复制到了GCP,并且这里的构建上下文与项目相关,而不是云构建所在的位置

要解决这个问题,只需将docker的构建上下文更改为
/backend
。您的cloudbuild最终版本应该类似于:

steps:
  # Build the container image
  - name: "gcr.io/cloud-builders/docker"
    args:
      [
        "build",
        "-t",
        "gcr.io/my-project/github.com/username/project.com:$COMMIT_SHA",
        "./backend",
      ]
    #Rest of the steps ...

当您对github repo执行push命令时,云构建将触发并查找cloudbuild.yaml文件。通过编辑
配置
部分和
云构建配置文件(yaml或json)
,可以在创建构建触发器时指定cloudbuild.yaml位置,您可以在其中选择cloudbuild.yaml位置。在您的情况下,只需将其设置为
后端/cloudbuild.yaml

现在,这还不够,因为当构建开始时,docker build命令将按照您的第一步启动构建映像。但是,docker的构建上下文是
,这不应该是因为您的所有repo都复制到了GCP,并且这里的构建上下文与项目相关,而不是云构建所在的位置

要解决这个问题,只需将docker的构建上下文更改为
/backend
。您的cloudbuild最终版本应该类似于:

steps:
  # Build the container image
  - name: "gcr.io/cloud-builders/docker"
    args:
      [
        "build",
        "-t",
        "gcr.io/my-project/github.com/username/project.com:$COMMIT_SHA",
        "./backend",
      ]
    #Rest of the steps ...

您的目录结构是什么样子的?通过错误消息
无法评估Dockerfile路径中的符号链接:lstat/workspace/Dockerfile:没有这样的文件或目录
,我可以看到一些文件或目录找不到。请同时显示您的
cloudbuild.yaml
内容。我更新了我的帖子。您的目录结构是什么样的?通过错误消息
无法评估Dockerfile路径中的符号链接:lstat/workspace/Dockerfile:没有这样的文件或目录
,我可以看到一些文件或目录找不到。请同时显示您的
cloudbuild.yaml
内容。我更新了我的postAs additional,用户还可以在运行步骤时使用构建步骤上的字段告知Cloud build在后端目录中工作。此外,用户还可以在运行步骤时使用构建步骤上的字段告知Cloud build在后端目录中工作。