Reputation: 531
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.
Upvotes: 0
Views: 1260
Reputation: 75810
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
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>
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:
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>
Upvotes: 2