如何将经过身份验证的状态从云构建器传递到 docker?

How to pass authenticated state from the cloud builder to docker?

我想使用 Google Cloud Build 构建我的 docker 图像。这些 docker 图像使用从 Google Artifact Registry 下载的私有包。

构建器本身已通过身份验证,可以使用npx google-artifactregistry-auth命令。但是我不能在 docker 构建过程中调用它。

当我在本地构建图像时,我将我的凭据传递到 Dockerfile 中,如下所示: --build-arg GOOGLE_CREDS=\"$(cat $GOOGLE_APPLICATION_CREDENTIALS)\"

有没有办法开箱即用,还是我必须创建一个单独的服务帐户并将其密钥作为秘密上传到云构建?有点烦人,因为这两项服务都在 google 云上....

编辑: 根据要求,我添加了有关在本地构建工件注册表时如何处理工件注册表的信息。 我的 docker 命令是:

docker build --rm --build-arg GOOGLE_CREDS=\"$(cat $GOOGLE_APPLICATION_CREDENTIALS)\" -f 'Dockerfile' -t image:latest .

docker 图片的相关部分是:

ARG GOOGLE_CREDS
ENV GOOGLE_APPLICATION_CREDENTIALS=/credentials.json
RUN echo ${GOOGLE_CREDS} > $GOOGLE_APPLICATION_CREDENTIALS
COPY .npmrc_template /root/.npmrc

RUN npx google-artifactregistry-auth ~/.npmrc 
RUN yarn install --silent

.npmrc_template 包含有关私有存储库的详细信息,但没有密码。然后由 google-artifactregistry-auth 命令

填充

你需要明确地add the cloudbuild network to your Docker Build,就像那样

docker build --rm -f 'Dockerfile' -t image:latest --network=cloudbuild .