Google Cloud Build Trigger failing with "ERROR: build step 0 "gcr.io/cloud-builders/docker" failed: step exited with non-zero status: 1"

14,632

When you execute a push command to your github repo, the Cloud Build will triggers and look for the cloudbuild.yaml file. You can specify the cloudbuild.yaml location when you create the build trigger by editing the Configuration section and Cloud Build configuration file (yaml or json) in which you can choose the cloudbuild.yaml location. in your case just make it backend/cloudbuild.yaml.

Now, that's not enough because when the build start, docker build command will initiate to build your image as per your first step. However, your build context for docker is . which should not be because all your repo was copied to GCP and the build context here is relational to the project and not where the cloud build is.

To solve this issue just change the build context of docker to ./backend. Your cloudbuild final version should be something like:

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 ...
Share:
14,632
sdfsdf
Author by

sdfsdf

Updated on June 08, 2022

Comments

  • sdfsdf
    sdfsdf almost 2 years

    I am trying to setup continuous deployment of my golang backend using the Google documentation, but when my trigger fires, it fails with the following error:

    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
    

    My project file structure looks like:

    project
        frontend
        backend
            main.go
            cloudbuild.yaml
            Dockerfile
    

    where my cloudbuild.yaml looks like:

    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
    

    and my Dockerfile looks like

    # 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"]
    

    I got the Dockerfile from Quickstart: Build and Deploy .

    • sllopis
      sllopis about 4 years
      What does your directory structure look like? By the error message unable to evaluate symlinks in Dockerfile path: lstat /workspace/Dockerfile: no such file or directory, I can see that some file or directory cannot be found. Please also show your cloudbuild.yaml content.
    • sdfsdf
      sdfsdf about 4 years
      I updated my post
  • Donnald Cucharo
    Donnald Cucharo almost 3 years
    As additional, users can also use dir field on the build step to tell Cloud Build to work into backend directory when running the step.