云构建将秘密环境传递给 dockerfile

Fel*_*loo 6 go docker google-cloud-platform google-cloud-build

我正在使用 google cloud build 来构建 docker 映像并在云运行中部署。该模块依赖于私有的 Github。在cloudbuild.yaml文件中,我可以访问密钥,例如 Github 令牌,但我不知道将此令牌传递给Dockerfile.

我正在遵循这个官方指南,但它只能在cloudbuild.yaml范围内工作,而不能在Dockerfile. 通过 SSH 密钥从构建访问 GitHub

云构建.yaml

steps:
  - name: gcr.io/cloud-builders/docker
    args: ["build", "-t", "gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA", "."]

  - name: gcr.io/cloud-builders/docker
    args: [ "push", "gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA" ]

  - name: gcr.io/google.com/cloudsdktool/cloud-sdk
    entrypoint: gcloud
    args: [
      "run", "deploy", "$REPO_NAME",
      "--image", "gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA",
      "--platform", "managed",
      "--region", "us-east1",
      "--allow-unauthenticated",
      "--use-http2",
    ]

images:
  - gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA

availableSecrets:
  secretManager:
    - versionName: projects/$PROJECT_ID/secrets/GITHUB_USER/versions/1
      env: "GITHUB_USER"
    - versionName: projects/$PROJECT_ID/secrets/GITHUB_TOKEN/versions/1
      env: "GITHUB_TOKEN"
Run Code Online (Sandbox Code Playgroud)

Dockerfile

# [START cloudrun_grpc_dockerfile]
# [START run_grpc_dockerfile]
FROM golang:buster as builder

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

# Create /root/.netrc cred github
RUN echo machine github.com >> /root/.netrc
RUN echo login "GITHUB_USER" >> /root/.netrc
RUN echo password "GITHUB_PASSWORD" >> /root/.netrc

# Config Github, this create file /root/.gitconfig
RUN git config --global url."ssh://git@github.com/".insteadOf "https://github.com/"

# GOPRIVATE
RUN go env -w GOPRIVATE=github.com/org/repo

# Do I need to remove the /root/.netrc file? I do not want this information to be propagated and seen by third parties.

# Retrieve application dependencies.
# This allows the container build to reuse cached dependencies.
# Expecting to copy go.mod and if present go.sum.
COPY go.* ./
RUN go mod download

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

# Build the binary.
# RUN go build -mod=readonly -v -o server ./cmd/server
RUN go build -mod=readonly -v -o server

# Use the official Debian slim image for a lean production container.
# https://hub.docker.com/_/debian
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM debian:buster-slim
RUN set -x && apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
    ca-certificates && \
    rm -rf /var/lib/apt/lists/*

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

# [END run_grpc_dockerfile]
# [END cloudrun_grpc_dockerfile]
Run Code Online (Sandbox Code Playgroud)

经过两天的尝试,我还没有找到解决方案,我能做的最简单的事情就是生成文件vendor夹并将其提交到存储库并避免go mod download.

gui*_*ere 11

你有几种方法可以做事。

使用 Docker,当您运行构建时,您可以在隔离的环境中运行它(这就是隔离原则)。因此,您无法从构建过程内部访问环境变量。

为了解决这个问题,您可以使用构建参数并将您的秘密值放入该​​参数中。

但是,有一个陷阱:您必须使用 bash 代码,而不是 Cloud Build 中的内置步骤代码。我来给你展示

# Doesn't work
  - name: gcr.io/cloud-builders/docker
    secretEnv: ["GITHUB_USER","GITHUB_TOKEN"]
    args: ["build", "-t", "gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA", "--build-arg=GITHUB_USER=$GITHUB_USER,GITHUB_TOKEN=$GITHUB_TOKEN","."]

# Working version
  - name: gcr.io/cloud-builders/docker
    secretEnv: ["GITHUB_USER","GITHUB_TOKEN"]
    entrypoint: bash
    args: 
     - -c
     - |
        docker build -t gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA --build-arg=GITHUB_USER=$$GITHUB_USER,GITHUB_TOKEN=$$GITHUB_TOKEN .
Run Code Online (Sandbox Code Playgroud)

您还可以在 Dockerfile 之外执行操作。大致相同的事情:加载一个容器,执行操作,加载另一个容器并继续。

  • 感谢您的回答,我已经考虑过 --build-args 但我在某处读到它不安全,因为可以在某些日志中看到密钥。您写的最后一件事是跳过“Dockerfile”并使用已经存在的 golang 构建器映像在“cloudbuild.yaml”中执行所有操作。我考虑过最后一个,因为它在我的用例中有很多优点,但我不知道它是否实用,现在你能为我们写一个例子,说明如果没有“Dockerfile”,这会是什么样子吗? (3认同)