mailer-daemon
mailer-daemon

Reputation: 11

Difficulty with build block configuring Cloud Run with Dockerfile in Cloud Build Trigger via Terraform

I have a trigger resource that seems to look right (below) on reviewing in GUI, and a cloud_run_v2_service resource, but I can't build the image.

However, on running terraform apply, I get the error:

│ Error: Error waiting to create Service: Error waiting for Creating Service: Error code 13, message: Revision 'cloud_run-00001-xxx' is not ready and cannot serve traffic. Image 'gcr.io/<project>/my-cb-gh-repo' not found.

Looking at Cloud Run Services, the Service has a (!) and the error (on hover):

Revision 'cloud_run-00001-xxx' is not ready and cannot serve traffic. Image 'gcr.io/<project>/my-cb-gh-repo' not found. And inside the service details: Creating revision Image 'gcr.io/<project>/my-cb-gh-repo' not found.

Critically, if I include the step name = <same> args = ["push", "gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA"] The trigger ends up looking for a Cloud Build config file, not a Dockerfile.

Finally, if I go into the errored Cloud Run Service and click "Edit Continuous Deployment Trigger", point it to the GH Repo and Dockerfile, and submit, I get an error (below):

My Question

I assume that I'm setting up the google_cloudbuild_trigger build block incorrectly, but I don't understand how/why cloud build fails to even try to build, why it fails when i force it manually, and why cloud run can't find the image. (The last likely explained by the first?)

Cloud Run Service

resource "google_cloud_run_v2_service" "cloud_run_service" {
  provider = google-beta
  name     = var.cloud_run_service_name
  location = var.region
  template {
    service_account = data.terraform_remote_state.config1.outputs.cloud_run_sa
    containers {
      image = "gcr.io/${data.google_project.project.project_id}/${google_cloudbuildv2_repository.gh_repo.name}"
    }
  }

Trigger

resource "google_cloudbuild_trigger" "cloud_bld_trigger7" {
  name        = "cloud-bld-trigger7"
  description = "Trigger with repository_event_config and build"
  provider    = google-beta
  location    = var.region
  repository_event_config {
    repository = google_cloudbuildv2_repository.gh_repo.id
    push {
      branch = "^main$"
    }
  }
  build {
    images = ["gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA"]
    options {
      logging = "CLOUD_LOGGING_ONLY"
    }
    step {
      name = "gcr.io/cloud-builders/docker"
      args = ["build", "-t", "gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA", "-f", "Dockerfile", "."]
    }
    substitutions = {
      _BRANCH_NAME = "^main$"
    }
  }
}

Dockerfile & go file

I believe this is basically the default for dbt builds like this.

FROM golang:1.13 as builder
WORKDIR /app
COPY invoke.go ./
RUN CGO_ENABLED=0 GOOS=linux go build -v -o server.go

FROM ghcr.io/dbt-labs/dbt-bigquery:1.5.0
USER root
WORKDIR /dbt
COPY --from=builder /app/server ./
COPY script.sh ./
COPY . ./

ENTRYPOINT "./server"

invoke.go

package main

import (
    "fmt"
    "log"
    "net/http"
    "os"
    "os/exec"
)

func handler(w http.ResponseWriter, r *http.Request) {
    log.Print("Request Received")
    cmd := exec.Command("/bin/sh", "script.sh")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    err := cmd.Run()
    if err != nil {
        log.Fatalf("cmd.Run() failed with %s\n", err)
    }
}
func main() {
    log.Print("Server starting")
    http.HandleFunc("/", handler)
    port := os.Getenv("PORT")
    if port == "" {
        port = "8080"
    }
    log.Printf("%s port listening", port)
    log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}

Cloud Build Log Errors

BUILD
Starting Step #0 - "Build"
Step #0 - "Build": Already have image (with digest): gcr.io/cloud-builders/docker
Step #0 - "Build": Sending build context to Docker daemon  548.4kB

Step #0 - "Build": Step 1/11 : FROM golang:1.13 as builder
Step #0 - "Build": 1.13: Pulling from library/golang
Step #0 - "Build": xxxxx: Pulling fs layer // Waiting // Verify Checksum // Download complete // Pull complete
////
Step #0 - "Build": Digest: sha256:yyyyyyyyyy
Step #0 - "Build": Status: Downloaded newer image for golang:1.13
Step #0 - "Build":  ---> aaaaaa
Step #0 - "Build": Step 2/11 : WORKDIR /app
Step #0 - "Build":  ---> Running in bbbb
Step #0 - "Build": Removing intermediate container bbbb
Step #0 - "Build":  ---> cccccc
Step #0 - "Build": Step 3/11 : COPY invoke.go ./
Step #0 - "Build":  ---> ddddd
Step #0 - "Build": Step 4/11 : RUN CGO_ENABLED=0 GOOS=linux go build -v -o server.go
Step #0 - "Build":  ---> Running in eeeee
Step #0 - "Build": can't load package: package .: 
Step #0 - "Build": invoke.go:4:1: expected 'package', found ""
Step #0 - "Build": invoke.go:4:3: string literal not terminated
Step #0 - "Build": The command '/bin/sh -c CGO_ENABLED=0 GOOS=linux go build -v -o server.go' returned a non-zero code: 1
Finished Step #0 - "Build"
ERROR
ERROR: build step 0 "gcr.io/cloud-builders/docker" failed: step exited with non-zero status: 1

"Edit Continuous Deployment" in Cloud Run Service GUI Error

Creating revision
 Image 'gcr.io/<_project_>/my-cb-gh-repo' not found.
Routing traffic
 Pending
Creating Cloud Build trigger
 Completed
Building and deploying from repository (see logs)
 Trigger execution failed: source code could not be built or deployed, find more information in build logs
Revision 'cloud_run-00001-xxx' is not ready and cannot serve traffic. Image 'gcr.io/<_project_>/my-cb-gh-repo' not found.

Appendix: Triggers

For reference/future buffoons brute forcing it as I did:

Trigger Blocks Outcome
github and build blocks everything looking right, BUT it's a "1st Gen" Source
repository_event_config block and filename "2nd Gen", looking for Cloud Build Config in right repo, pointing to looking for CB file in Dockerfile
trigger_template and git_file_source blocks error in gui
github block and filename "1st gen" looking for CB config file in right repo
repository_event_config and build blocks with "push" step "2nd gen" looking for CB config file in right repo
repository_event_config and build blocks without "push" step 2nd Gen GH looking for docker file (desired)

Upvotes: 1

Views: 710

Answers (1)

mailer-daemon
mailer-daemon

Reputation: 11

As mentioned in comments, the issues were fairly simple. Noting here for posterity.

  1. I had mislabeled the shell script the docker file called.
  2. I added this step to cloud_build_trigger ensure the image was pushed.
    step {
      name       = "gcr.io/cloud-builders/gcloud"
      entrypoint = "/bin/bash"
      args       = ["-c", "docker push <<<imagename:tag>>>"]
    }

Not sure why the images in the build block doesn't push for you; docs say "A list of images to be pushed upon the successful completion of all build steps." but I'm out over my skis anyway!

Upvotes: 0

Related Questions