Can CI link gradle.properties link?

I am creating a Circle CI build for an Android project, and I am wondering how to add the gradle.properties file to my project assembly. I use local gradle.properties to store API keys and sensitive data. Other CI tools (i.e. Jenkins) allow you to load the gradle.properties file for use in all assemblies, but I cannot find a way to do this in Circle CI.

It seems environment variables are the only way Circle CI allows you to add secret credentials to your project.

Is there a way to use the credentials from gradle.properties to build Circle CI?

+7
android continuous-integration gradle circleci
source share
1 answer

I found a way to add API credentials / keys to gradle.properties through Circle CI. It allows Android projects to reference gradle.properties for credentials in the same way for local and CircleCI assemblies.

In the first step, save your credentials in the Circle CI project settings as environment variables that are guaranteed to be private. In the Circle CI GUI, navigate to your project, then select “Project Settings” in the upper right corner. In the menu on the left, click "Environment Variables", which are under the heading "Tweaks". Here you can add your credentials as a pair of name values.

Then create a bash script in an Android project that will write Circle CI environment variables to the local gradle.properties file. I wrote such a script and posted it here as a gist . Here is the way that does the job:

 function copyEnvVarsToGradleProperties { GRADLE_PROPERTIES=$HOME"/.gradle/gradle.properties" export GRADLE_PROPERTIES echo "Gradle Properties should exist at $GRADLE_PROPERTIES" if [ ! -f "$GRADLE_PROPERTIES" ]; then echo "Gradle Properties does not exist" echo "Creating Gradle Properties file..." touch $GRADLE_PROPERTIES echo "Writing TEST_API_KEY to gradle.properties..." echo "TEST_API_KEY=$TEST_API_KEY_ENV_VAR" >> $GRADLE_PROPERTIES fi } 

This script is only called during Circle CI builds, not during local builds. Call this script as a pre-process dependency in the circle.yml file, so your gradle.properties will be written before the gradle build starts:

 dependencies: pre: - source environmentSetup.sh && copyEnvVarsToGradleProperties 

You will continue to access the API keys in the build.gradle file as always:

buildConfigField("String", "THIS_TEST_API_KEY", "\"" + TEST_API_KEY + "\"")

+11
source share

All Articles