Google Cloud Run-在Firebase项目中运行Shell脚本 [英] Google Cloud Run - Run Shell Script in firebase project

查看:108
本文介绍了Google Cloud Run-在Firebase项目中运行Shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是google cloud-run的新手,我希望实现在firebase项目中运行脚本以更新配置(env变量)

I'm new to google cloud-run and I'm hoping to achieve to run scripts in the firebase project to update configs (env variables)

这是过程

firebase函数被调用->将param(bar,baz)传递到云运行中->运行脚本 firebase functions:config:set foo.bar = baz

firebase function invoked-> pass param(bar, baz) into cloud run -> run scripts firebase functions:config:set foo.bar=baz

我所做的是创建由cloud-builders-community共享的firebase-tools的图像,下面是代码

What I have done is to crate an image of firebase-tools shared by cloud-builders-community, and below is the code

// cloudbuild.yaml
steps:
- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '-t', 'gcr.io/$PROJECT_ID/firebase', '.']
images:
- 'gcr.io/$PROJECT_ID/firebase'
tags: ['cloud-builders-community']

以下是Dockerfile

and below is the Dockerfile

//Dockerfile

FROM node

RUN npm i -g firebase-tools
ADD firebase.bash /usr/bin
RUN chmod +x /usr/bin/firebase.bash

ENTRYPOINT [ "/usr/bin/firebase.bash" ]

所以从这里开始,我想知道如何编写和运行脚本.任何想法或建议将不胜感激.

so from here, I was wondering how to write and run scripts. Any idea or suggestion will be appreciated.

推荐答案

Cloud Run允许您托管响应HTTP请求的容器.

Cloud Run allows you to host containers that answer to HTTP request.

在您的设计中,您如何设法调用容器?...是的,您没有定义任何端点.

In your design, how did you manage to invoke your container? ... Yes you don't have any endpoint defined.

我在一个开源项目中做出了贡献,我发现了一个工具,该工具可以自动为bash脚本提供HTTP端点服务.看看这个 Dockerfile .它使用工具 shell2http

I contributed on an open source project and I discovered a tool that serve you automatically bash script as http endpoint. Have a look to this Dockerfile. It use the tool shell2http

根据您的情况,我建议您使用这样的Dockerfile:

In your case, I recommend you to have a Dockerfile like this:

FROM node
RUN npm i -g firebase-tools
ADD my_script.sh /
COPY --from=msoap/shell2http /app/shell2http /shell2http

RUN chmod +x my_script.sh

ENTRYPOINT ["/shell2http","-export-all-vars"]
CMD ["/update","/my_script.sh"]

编写您的 my_script.sh 以根据查询参数运行所需的bash命令.

Write your my_script.sh to run the bash command that you want according with the query params.

工作示例 my_script.sh

#!/bin/sh
firebase --version

然后,在构建容器并将其部署到Cloud Run上之后,调用 https://myservice.....run.app/update?< your env var>

And, after building your container and deploying it on Cloud Run, invoke https://myservice.....run.app/update?<your env var>

编辑

对于Firebase身份验证,您有2种解决方案,但是在详细介绍之前,您必须按照

For the Firebase authentication, you have 2 solutions, but before going in detail, you have to generate your refresh token as explained here

  1. 使用env var传递令牌.这不是最安全的方法,因为您的令牌以纯文本格式存储在Cloud Run env var中.

像这样部署您的服务

gcloud run deploy --image=... --set-env-vars=TOKEN=<tokenValue>

像这样构建 my_script.sh 文件

#!/bin/sh
firebase --token $TOKEN <command>

  1. 使用秘密管理器.它更安全,但是需要做更多的事情.首先将您的Firebase刷新令牌保存到Secret Manager中
  1. Use secret manager. It's more secure but require more thing to do. Start by saving your firebase refresh token into secret manager

echo "<tokenContent>" | gcloud beta secrets create --replication-policy=automatic --data-file=- myFirebaseToken

我更改了创建容器的方式,因为我需要使用gcloud.因此,这里是新容器

I changed the way to create the container because I need to use gcloud. Thus here the new container

FROM google/cloud-sdk

RUN apt-get update && apt-get install -y nodejs npm
RUN echo $(npm i -g  firebase-tools)
RUN node -v

ADD my_script.sh /
COPY --from=msoap/shell2http /app/shell2http /shell2http
RUN chmod +x my_script.sh

ENTRYPOINT ["/shell2http","-export-all-vars"]
CMD ["/update","/my_script.sh"]

2条评论:

  • 默认节点版本为12.我不知道这对您来说没有问题
  • 节点12版本是Firebase工具的问题,因为已弃用1个依赖项.然后 npm i 命令以非零代码退出(并且构建失败).这里的技巧是在命令周围加上echo RUN echo $(npm i -g firebase-tools).并不是很干净,但是可以.
  • The default node version is 12. I don't know if it's an issue for you
  • The node 12 version is an issue for firebase tools because there is 1 dependency deprecated. And the npm i command exit with a non zero code (and the build failed). The hack here is to surround the command with an echo RUN echo $(npm i -g firebase-tools). Not really clean, but it works.

现在是 my_script.sh 文件

#!/bin/sh
TOKEN=$(gcloud beta secrets versions access --secret=<mySecretToken> latest)
firebase --token $TOKEN <command>

这篇关于Google Cloud Run-在Firebase项目中运行Shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆