Read Amazon S3 credentials from ~/.gradle/gradle.properties file?

android
gradle
aws
s3

#1

One of my projects is an Android library which when built, pushes artifacts to my S3 account, and my app projects that depend on it download it from that S3 location. This setup works great on my local machine, of course. Because I have my ~/.gradle/gradle.properties file set with my AWS credentials.

Is there any way to add my AWS credentials to ~/.gradle/gradle.properties file? CircleCI reads from ~/.aws/credentials which is the way AWS works but Gradle does not look in there.

Gradle team is allegedly not going to merge a PR that indeed reads the credentials from ~/.aws/credentials file
Ref - http://brianoneill.blogspot.com/2015/10/using-gradle-with-aws-and-s3-w.html

I think if CircleCI let us add properties in ~/.gradle/gradle.properties that will solve this problem.


#2

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.

This script should only be called during a Circle CI build. 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 + "\"")


#3

Thanks @KioKrofovitch I guess it took me almost a year to get back to solving this issue again :slight_smile:

I am not very used to bash scripting but I keep getting error that the file doesn’t exist. Pasting the console log here

source environmentSetup.sh && copyEnvVarsToGradleProperties
Gradle Properties should exist at /home/ubuntu/.gradle/gradle.properties
Gradle Properties does not exist
Creating Gradle Properties file...
touch: cannot touch ‘/home/ubuntu/.gradle/gradle.properties’: No such file or directory
Writing AWS_CREDENTIALS_ACCESS_KEY_ID to gradle.properties...
environmentSetup.sh: line 37: /home/ubuntu/.gradle/gradle.properties: No such file or directory
Writing AWS_CREDENTIALS_SECRET_ACCESS_KEY to gradle.properties...
environmentSetup.sh: line 40: /home/ubuntu/.gradle/gradle.properties: No such file or directory

source environmentSetup.sh && copyEnvVarsToGradleProperties returned exit code 1

Action failed: source environmentSetup.sh && copyEnvVarsToGradleProperties

Any idea why it is not creating a gradle properties file?


#4