-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Use environment for ENV variables * setup a local.properties file for tests * added .env files to .gitignore * Added a 'secrets' test which uses .env for local.properties * move the local properties file creation elsewhere * lint fix * add an action to convert secrets to env variables
- Loading branch information
Showing
6 changed files
with
74 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,3 +47,5 @@ build | |
*~ | ||
*.swp | ||
*.exec | ||
|
||
app/.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* First tries to find the property in the local.properties file, and then falls back to the | ||
* environment variables. If it can't find the property, it returns an empty string. | ||
* | ||
* @param name the fully qualified name of the property | ||
* @return the value of the property, or an empty string if it can't be found | ||
*/ | ||
String propertyEnvOrEmpty(String name) { | ||
return project.findProperty(name) ?: env.fetch(name, "") | ||
} | ||
|
||
/** | ||
* Ensures that the string is enclosed with quotes so that in the BuildConfig.java file, it doesn't | ||
* leave a blank which isn't compilable. | ||
* | ||
* @param name the name of the buildConfig property | ||
* @return either the value of the property, the value of the env variable, or an empty string | ||
* enclosed in quotes | ||
*/ | ||
String buildConfigProperty(String name) { | ||
return "\"${propertyEnvOrEmpty(name)}\"" | ||
} | ||
|
||
android { | ||
defaultConfig { | ||
buildConfigField("String", "ENV_SECRET", buildConfigProperty("ENV_SECRET")) | ||
buildConfigField("String", "PROPERTY_SECRET", buildConfigProperty("PROPERTY_SECRET")) | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
app/src/androidTest/java/com/example/myapplication/SecretsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.example.myapplication | ||
|
||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Assertions.assertTrue | ||
import org.junit.jupiter.api.Test | ||
import org.junit.platform.commons.logging.LoggerFactory | ||
|
||
/** | ||
* Ensure that we can get the secrets from the environment variables and local properties files | ||
* | ||
* Requires the following plugins: | ||
* - for env: https://plugins.gradle.org/plugin/co.uzzu.dotenv.gradle | ||
* - for local.properties: https://github.com/google/secrets-gradle-plugin | ||
*/ | ||
class SecretsTest { | ||
|
||
private val logger = LoggerFactory.getLogger(javaClass) | ||
|
||
@Test fun envSecretTest() { | ||
val envSecret = BuildConfig.ENV_SECRET | ||
logger.info { "envSecret: $envSecret" } | ||
assertTrue(envSecret.isNotEmpty(), "Expecting a value in the ENV_SECRET. You're probably missing a .env file") | ||
assertEquals("test", envSecret, "Expecting the ENV secret to be 'test', set this in your .env file or GH actions secret") | ||
} | ||
|
||
@Test fun localPropertyTest() { | ||
val propertySecret = BuildConfig.PROPERTY_SECRET | ||
logger.info { "propertySecret: $propertySecret" } | ||
assertTrue(propertySecret.isNotEmpty(), "Expecting a value in the PROPERTY_SECRET. You're probably missing a definition in the local.properties file") | ||
assertEquals("property_test", propertySecret, "Expecting the PROPERTY_SECRET secret to be 'property_test', set this in your local_properties file or GH actions secret") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters