gpt4 book ai didi

node.js - gcloud auth 在 dockerfile 中不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 23:47:50 25 4
gpt4 key购买 nike

我制作了一个多阶段 dockerfile,用于安装我的 npm 包,然后尝试将它们部署到 gcp

问题是当我运行部署时,它告诉我无法打开配置文件,并且我没有选择事件帐户

WARNING: Could not open the configuration file: [/root/.config/gcloud/configurations/config_default].
ERROR: (gcloud.functions.deploy) You do not currently have an active account selected.
Please run:

$ gcloud auth login

to obtain new credentials, or if you have already logged in with a
different account:

$ gcloud config set account ACCOUNT

to select an already authenticated account to use.

这是我的 dockerfile:

FROM node:10.19.0-alpine3.9 AS build

ADD . /app
WORKDIR /app

RUN npm install

FROM google/cloud-sdk

WORKDIR /app
COPY --from=build /app /app

RUN gcloud auth activate-service-account --key-file /app/firebase-service-account.json
RUN gcloud config set project MY_PROJECT
RUN gcloud config set account service-account-email@service-account.iam.gserviceaccount.com

我还编写了一个 bash 脚本来构建和部署我的云函数

#! /bin/sh

docker build . -f deploy-functions.dockerfile -t cloud-functions-deploy

docker run cloud-functions-deploy gcloud functions deploy event-log-trigger \
--project ${GCP_PROJECT_ID} \
--entry-point eventLogBuilder \
--trigger-event providers/cloud.firestore/eventTypes/document.update \
--trigger-resource projects/${GCP_PROJECT_ID}/databases/\(default\)/documents/events/\{eventId\} \
--region=${GCP_REGION} --stage-bucket=${STAGE_BUCKET} --runtime nodejs10

在我的第一个 bash 脚本中,我没有 --project 标志,但需要添加它,因为它说该命令没有项目。看来警告是正确的,即使我以 root 身份运行所有内容,它也无法打开 config_default 文件。

最佳答案

您可以通过创建 entrypoint.sh 文件来解决此问题

# entrypoint.sh
# Set your env variables GCP_PROJECT_ID, STAGE_BUCKET, GCP_REGION, ...
gcloud auth activate-service-account --key-file /app/firebase-service-account.json
gcloud config set project $GCP_PROJECT_ID
# your deploy command
gcloud functions deploy event-log-trigger \
--entry-point eventLogBuilder \
--trigger-event providers/cloud.firestore/eventTypes/document.update \
--trigger-resource projects/${GCP_PROJECT_ID}/databases/\(default\)/documents/events/\{eventId\} \
--region=${GCP_REGION} --stage-bucket=${STAGE_BUCKET} --runtime nodejs10

并在 Dockerfile 中设置此信息

# previous stage ... 
FROM google/cloud-sdk

WORKDIR /app
COPY --from=build /app /app

RUN chmod +x /app/entrypoint.sh
ENTRYPOINT ["/bin/sh","/app/entrypoint.sh"]

It seems that the commands do not persist after the image is formed.

RUN gcloud auth activate-service-account --key-file /app/firebase-service-account.json
RUN gcloud config set project MY_PROJECT
RUN gcloud config set account service-account-email@service-account.iam.gserviceaccount.com

我通过运行命令检查了这一点

docker run cloud-functions-deploy gcloud config list

关于node.js - gcloud auth 在 dockerfile 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60406952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com