简体   繁体   English

用于 github 操作的 Google 服务 json 文件?

[英]Google services json file for github actions?

I am currently working on a android project and I use github action to test the project.我目前正在处理一个 android 项目,我使用 github 操作来测试该项目。 But whenever it build it ends up with an error for not finding the google-services.json file.但无论何时构建,它都会以找不到google-services.json文件的错误告终。

The error generated is as follows产生的错误如下

File google-services.json is missing. The Google Services Plugin cannot function without it.

Now, I don't want to commit or upload the google-services.json file on the github. So, Is there any other way to solve this?现在,我不想在 github 上提交或上传google-services.json文件。那么,还有其他方法可以解决这个问题吗?

If you are making any public repo or any sample app, you should not commit the google-services.json file to the Github repo.如果您正在制作任何公共存储库或任何示例应用程序,则不应将google-services.json文件提交到 Github 存储库。

While you are working on private repo it depends on how collaborators are managing the firebase credentials.当您处理私人回购时,这取决于合作者如何管理 firebase 凭证。 google-services.json file will have access key and fingerprints of firebase projects. google-services.json文件将包含 firebase 项目的访问密钥和指纹。

Conclusion: Don't share the google-services.json file over any SVN or Git repo.结论:不要在任何 SVN 或 Git 存储库上共享google-services.json文件。 User can add their own google-services.json file to the project and run it on the testing environment.用户可以将自己的google-services.json文件添加到项目中,并在测试环境中运行。

Well... I have found the solution but in case if anyone needs I am explaining here.嗯......我已经找到了解决方案,但如果有人需要,我会在这里解释。

We can store the content of google-sevices.json in Environment variable (aka Secrets in github).我们可以将google-sevices.json的内容存储在环境变量(又名 github 中的 Secrets)中。 Actually github uses linux based cli so we have to execute some command on the cli using github actions.实际上 github 使用基于 linux 的 cli,所以我们必须使用 github 动作在 cli 上执行一些命令。

There will be two steps...会有两个步骤...

  • Firstly create the google-services.json file in base64首先在 base64 中创建 google-services.json 文件
- name: Create file
  run: cat /home/runner/work/<Project-Name>/<Project-Name>/app/google-services.json | base64
  • Then put data in the file (basically this fetch data from github secrets and put the data in json file before building the application)然后将数据放入文件中(基本上是在构建应用程序之前从 github 机密中获取数据并将数据放入 json 文件中)
- name: Putting data
  env:
    DATA: ${{ secrets.GOOGLE_SERVICES_JSON }}
  run: echo $DATA > /home/runner/work/<Project-Name>/<Project-Name>/app/google-services.json
  • Then define the content of google-services.json file in the github secrets via: Setting > Secrets > New Repository Secret using name GOOGLE_SERVICES_JSON然后在 github 机密中定义google-services.json文件的内容: Setting > Secrets > New Repository Secret使用名称GOOGLE_SERVICES_JSON的新存储库机密

Both of these commands should be placed before the gradle build command in gradle.yml这两个命令都应该放在 gradle.yml 中的gradle.yml构建命令之前

By doing this your google-services.json file will be created and has data also so the app will build successfully.通过这样做,您的google-services.json文件将被创建并包含数据,因此应用程序将成功构建。

Adding any sort of credentials to your GitHub repo is dangerous and ill-advised.向您的 GitHub 存储库添加任何类型的凭据都是危险且不明智的。 Even if the repo is private and you are the only one using it right now, maybe you'll invite other people later and unless you change the git history, your credentials will always be accessible even if you delete them via a new commit.即使 repo 是私有的并且你现在是唯一一个使用它的人,也许你稍后会邀请其他人,除非你更改 git 历史记录,即使你通过新提交删除它们,你的凭据也将始终可以访问。

Instead, put your credentials (in this case, google-services.json ) in a GitHub Action secret and read that secret from the CI file.相反,将您的凭据(在本例中为google-services.json )放入 GitHub 操作机密中,并从 CI 文件中读取该机密。 GitHub secrets are a great place to store this information because only the actions you run can read them and not other members of the repository. GitHub 秘密是存储此信息的好地方,因为只有您运行的操作才能读取它们,而存储库的其他成员则不能。

  1. Encode your file as base64 on your local computer so that it may be easily stored in environment variables : base64 google-services.json在本地计算机上将您的文件编码为 base64,以便它可以轻松存储在环境变量中: base64 google-services.json
  2. Copy the output and store it in a new GitHub Action secret (can be found in the Settings tab of your repository) named GOOGLE_SERVICES_JSON复制 output 并将其存储在名为GOOGLE_SERVICES_JSON的新 GitHub Action secret(可以在存储库的“设置”选项卡中找到)中
  3. In your GitHub Action yml, right before you build, add this step, which adds your encoded file to an environment variable, then decodes it and adds it into the Action's worker directory:在您的 GitHub Action yml 中,就在您构建之前,添加此步骤,将您的编码文件添加到环境变量,然后对其进行解码并将其添加到 Action 的工作目录中:
- name: Create Google Services JSON File
  env:
    GOOGLE_SERVICES_JSON: ${{ secrets.GOOGLE_SERVICES_JSON }}
  run: echo $GOOGLE_SERVICES_JSON | base64 -di > ./<folder>/google-services.json

<folder> is whatever folder you need for your build. <folder>是您构建所需的任何文件夹。 In my Action, I was trying to build an android APK, so my path was ./android/app/google-services.json .在我的 Action 中,我试图构建一个 android APK,所以我的路径是./android/app/google-services.json Also, depending on your path, you may need to create the directory first: run: mkdir <folder> && echo $GOOGLE_SERVICES_JSON | base64 -di >./<folder>/google-services.json此外,根据您的路径,您可能需要先创建目录: run: mkdir <folder> && echo $GOOGLE_SERVICES_JSON | base64 -di >./<folder>/google-services.json run: mkdir <folder> && echo $GOOGLE_SERVICES_JSON | base64 -di >./<folder>/google-services.json

base64 -di decodes the encoded file and also gets rid of "garbage" characters, like newlines that were probably brought over when you copied the original encoded file into the Action secret. base64 -di解码编码文件并去除“垃圾”字符,例如当您将原始编码文件复制到 Action secret 时可能带来的换行符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 包含 Github 操作的 env 文件 - env file with Github Actions 无法从 Firebase 控制台下载 google-services.json 文件 - Unable to download google-services.json file from Firebase Console Github 第二代 Google Functions 的操作 - Github Actions to Google Functions 2nd gen 使用 github 操作从 monorepo 部署单个服务 - Deploy individual services from a monorepo using github actions Flutter:文件 google-services.json 丢失。 没有它,Google 服务插件不能 function - Flutter : File google-services.json is missing. The Google Services Plugin cannot function without it 使用 GOOGLE_APPLICATION_CREDENTIALS env var 和 GitHub Actions secret - Use GOOGLE_APPLICATION_CREDENTIALS env var with GitHub Actions secret google-github-actions/auth 失败,没有注入 $ACTIONS_ID_TOKEN_REQUEST_TOKEN 或 $ACTIONS_ID_TOKEN_REQUEST_URL - google-github-actions/auth failed with did not inject $ACTIONS_ID_TOKEN_REQUEST_TOKEN or $ACTIONS_ID_TOKEN_REQUEST_URL 如何在开发环境中使用没有服务 key.json 文件的谷歌服务,如 bucket 或 pub sub? - How to use the google services like bucket or pub sub without service key.json file in development environment? 如何将 google-services.json 文件放入 android 项目中的自定义位置 - How google-services.json file can be put to custom location within android project Flutter Firebase 没有正确初始化。 您是否已将“google-services.json”文件添加到项目中? - Flutter Firebase has not been correctly initialized. Have you added the "google-services.json" file to the project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM