Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Circle CI reference gradle.properties credentials?

I am setting up a Circle CI build for an Android project, and am wondering how to add a gradle.properties file to my project build. I use a local gradle.properties to store my API keys and sensitive data. Other CI tools (ie, Jenkins) allow you to upload a gradle.properties file to use across all builds, but I am not able to find a way to do this in Circle CI.

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

Is there a way to use credentials from gradle.properties on Circle CI builds?

like image 963
Kio Krofovitch Avatar asked Feb 16 '16 19:02

Kio Krofovitch


People also ask

What is context in circle CI?

Contexts provide a mechanism for securing and sharing environment variables across projects. The environment variables are defined as name/value pairs and are injected at runtime.


2 Answers

Add all properties in the gradle.properties to CircleCI "Environment Variables", but prepend them with:

ORG_GRADLE_PROJECT_
like image 125
JeffgNpc Avatar answered Oct 24 '22 14:10

JeffgNpc


I have found a way to add credentials / API Keys to gradle.properties via Circle CI. It allows Android projects to reference gradle.properties for credentials in the same way for local and CircleCI builds.

First step, store your credentials in your Circle CI project settings as environment variables, which are guaranteed to be private. In the Circle CI GUI, go to to your project, then select "Project Settings" in the upper right hand corner. In the menu on the left side click "Environment variables" which is under the "Tweaks" header. Here you can add your credentials as a name value pair.

Next, create a bash script in your Android project which will write your Circle CI environment variables to a local gradle.properties file. I have written such a script and posted it here as gist. Here is the method which does the work:

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 a Circle CI build, and not during local builds. Invoke this script as a pre-process dependency in your circle.yml file, so that your gradle.properties is written before the actual gradle build begins:

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

You will continue to access API keys in build.gradle just as you always have:

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

like image 45
Kio Krofovitch Avatar answered Oct 24 '22 15:10

Kio Krofovitch