Google Cloud Run - Run Shell Script in firebase project

痴心易碎 提交于 2021-01-29 16:28:51

问题


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)

here's the process

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

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']

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.


回答1:


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.

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

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

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

Example of working my_script.sh

#!/bin/sh
firebase --version

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

EDIT

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

  1. Use the env var to pass your token. It's not the most secure because your token is in plain text into the Cloud Run env var.

Deploy your service like this

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

And build your my_script.sh file like this

#!/bin/sh
firebase --token $TOKEN <command>
  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

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 remarks:

  • 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.

And now the my_script.sh file

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


来源:https://stackoverflow.com/questions/62462157/google-cloud-run-run-shell-script-in-firebase-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!