Closed2

Cloud Run で Go を動かした時のめも

kozkoz
FROM golang:1.16 as builder

# x509: certificate signed by unknown authority エラー回避のやつ
RUN apt-get update && apt-get install -y ca-certificates && update-ca-certificates

WORKDIR /go/src/pj/server/src

COPY go.mod ./
COPY go.sum ./
RUN go mod download

# 必要なソースは全コピ
COPY ./src ./src

ARG CGO_ENABLED=0
ARG GOOS=linux
ARG GOARCH=amd64

RUN go build \
  -o /app \
  -ldflags '-s -w'

FROM scratch as runner

COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /app /app

ENTRYPOINT ["./app"]

参考:
https://qiita.com/ny7760/items/6f209f61d69e00a82676

kozkoz
cloudbuild.yaml
steps:
  - id: Build
    name: "gcr.io/cloud-builders/docker"
    args:
      - "build"
      - "-t"
      - "asia-northeast1-docker.pkg.dev/$PROJECT_ID/dir/server-app:$SHORT_SHA"
      - "-t"
      - "asia-northeast1-docker.pkg.dev/$PROJECT_ID/dir/server-app:latest"
      - "--cache-from"
      - "asia-northeast1-docker.pkg.dev/$PROJECT_ID/dir/server-app:latest"
      - "./server"
    env:
      - "SHORT_SHA=$SHORT_SHA"

  - id: Push
    name: "gcr.io/cloud-builders/docker"
    args:
      ["push", "asia-northeast1-docker.pkg.dev/$PROJECT_ID/dir/server-app"]

  # https://cloud.google.com/sdk/gcloud/reference/run/deploy
  - id: Deploy
    name: "gcr.io/cloud-builders/gcloud"
    args:
      - "run"
      - "deploy"
      - "server-app"
      - "--image"
      - "asia-northeast1-docker.pkg.dev/$PROJECT_ID/dir/server-app:latest"
      - "--region"
      - "asia-northeast1"
      - "--port"
      - "80"
      - "--ingress"
      - "internal-and-cloud-load-balancing"
      - "--allow-unauthenticated"
      - "--tag=v${SHORT_SHA}"
      - "--update-env-vars=BASE_URL=dev"

# cache-fromで利用
images: ["asia-northeast1-docker.pkg.dev/$PROJECT_ID/dir/server-app:latest"]
tags: ["dev/prod"]

ローカル実行時: gcloud builds submit --config=./server/cloudbuild.yaml --substitutions=SHORT_SHA="$(git rev-parse --short HEAD)"

このスクラップは2021/11/12にクローズされました