我在构建中访问秘密变量时有一些问题。gradle文件。我在Github repo设置中定义了两个秘密:PUBLIC_API_KEY和PRIVATE_API_KEY
在我的行动。我从秘密中读取它们,解码并写入apikey。属性文件
name: My fancy app
on: [ push, pull_request ]
defaults:
run:
shell: bash
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Clone Repo
uses: actions/checkout@v1
- name: Acces API Public Key
env:
PUBLIC_API_KEY: ${{ secrets.PUBLIC_API_KEY }}
run: echo "$PUBLIC_API_KEY" | base64 -d > ./apikey.properties
- name: Acces API Private Key
env:
PRIVATE_API_KEY: ${{ secrets.PRIVATE_API_KEY }}
run: echo "$PRIVATE_API_KEY" | base64 -d > ./apikey.properties
在我的构建。我从apikey创建了一个属性对象。属性文件并尝试读取变量,这些变量在前一步中保存。这是它在我的模型中的样子。gradle文件
def apikeyPropertiesFile = rootProject.file("apikey.properties")
def apikeyProperties = new Properties()
try {
apikeyProperties.load(new FileInputStream(apikeyPropertiesFile))
println("Private Key value" + apikeyProperties['PRIVATE_API_KEY'])
println("Public Key value" + apikeyProperties['PUBLIC_API_KEY'])
} catch(Exception exception) {
println("Error by loading properties file" + exception.message)
}
....
buildConfigField("String", "PRIVATE_API_KEY", apikeyProperties['PRIVATE_API_KEY'])
buildConfigField("String", "PUBLIC_API_KEY", apikeyProperties['PUBLIC_API_KEY'])
构建失败,因为apiKeyProperties值都为空。
谁能告诉我,为什么值是空的?
多亏了这篇文章,我发现,钥匙在apikey中丢失了。属性文件。我的修复是这样的:
....
- name: Acces API Keys
run: |
touch apikey.properties
echo PUBLIC_API_KEY=${{ secrets.PUBLIC_API_KEY }} >> apikey.properties
echo PRIVATE_API_KEY=${{ secrets.PRIVATE_API_KEY }} >> apikey.properties
cat apikey.properties
...