如何在通过GitHub操作使用颤动网络时获取机密 [英] How to access secrets when using flutter web with github actions
本文介绍了如何在通过GitHub操作使用颤动网络时获取机密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个颤动Web应用程序,为了访问数据库,我在secrets.dart
文件中硬编码了一个apiKey,这非常好用。我已将此文件添加到.gitignore
,以防止它将其推送到版本控制。但在使用GitHub操作部署应用程序时,脚本会失败,因为它没有检测到机密文件。
我确实查看了GithubEncrypted secrets上的文档,该文档基本上允许存储机密。但这些机密似乎只能在YML文件中访问。
我想知道如何在我的应用程序中使用这个秘密,以便我的脚本成功运行并部署应用程序。 这是我的文件夹结构
lib/
services/
database.dart /// this file uses the APIkey from secrets.dart
secrets.dart /// contains the APIkey
我能想到的解决此问题的一种方法是使用.env
文件,但我不太熟悉如何通过CI脚本在.env文件中添加密钥。我相信这也会解决我的问题。
这是我的CI脚本
# This file was auto-generated by the Firebase CLI
# https://github.com/firebase/firebase-tools
name: Deploy to Firebase Hosting on merge
"on":
push:
branches:
- master
jobs:
build_and_deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v1
with:
java-version: "12.x"
- uses: subosito/flutter-action@v1
with:
channel: "master"
- run: flutter pub get
- run: flutter pub run build_runner build --delete-conflicting-outputs
- run: flutter build web --release
- uses: FirebaseExtended/action-hosting-deploy@v0
with:
repoToken: "${{ secrets.GITHUB_TOKEN }}"
firebaseServiceAccount: "${{ secrets.FIREBASE_SERVICE_ACCOUNT_VOCABHUB_34C7F }}"
channelId: live
projectId: vocabhub-34c7f
env:
FIREBASE_CLI_PREVIEWS: hostingchannels
推荐答案
您可以在被源代码管理忽略的情况下使用secrets.dart
文件。
以下是步骤
- 在本地计算机中,使用Base64命令对
secrets.dart
的内容进行编码:base64 lib/path/to/secrets.dart
- 将输出复制并粘贴到您的GitHub机密中,将其命名为
$SECRETS_FILE_CONTENT
或您想要的任何名称。 - 将此配置项步骤添加到YAML脚本中的
flutter pub get
步骤之前。# other stuff ... - run: echo $SECRETS_FILE_CONTENT | base64 -d > lib/path/to/secrets.dart env: SECRETS_FILE_CONTENT: ${{ secrets.SECRETS_FILE_CONTENT }} - run: flutter pub get - run: flutter pub run build_runner build --delete-conflicting-outputs - run: flutter build web --release # other stuff ...
要在GitHub用户界面中添加密码,请执行以下步骤:
您的密码应显示在用户界面的下半部分存储库密码中,而不应显示在环境密码中,因为这些密码不适用于简单的${{ secrets.name_of_variable }}
。
为了提供更多上下文,以下是存储库中的";Actions&Quot;文件(例如,.githorb/workflow/ci.yml)应如下所示:
name: CI
on:
push:
branches: [ main, dev ]
pull_request:
branches: [ main ]
# Allows to run this workflow manually from the Actions tab
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Decode base64 secrets
run: echo $SECRETS_FILE_CONTENTS | base64 -d > lib/path/to/secrets.dart
env:
SECRETS_FILE_CONTENTS: ${{ secrets.SECRETS_FILE_CONTENTS }}
# … put your steps here
run: flutter pub get
编辑
当人们使用Firebase时隐藏google-services.json
时,这也是相同的过程。或使用签名密钥(key.jks
或key.keystore
)。
这篇关于如何在通过GitHub操作使用颤动网络时获取机密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文