Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reference a Travis secure variable in my build.gradle?

One of my project dependencies sits on a private Bintray repo, which requires a username and password to access. Locally, I have these set in my gradle.properties:

bintrayUsername=<myUserName>
bintrayPassword=<myPass>

This works (locally), where hasProperty(X) resolves true and it uses this property:

allprojects {
    repositories {
        jcenter()

        def mahBintrayUsername = hasProperty(bintrayUsername) ? bintrayUsername : System.getenv('bintrayUsername')
        def mahBintrayPassword = hasProperty(bintrayPassword) ? bintrayPassword : System.getenv('bintrayPassword')


        maven {
            credentials {
                username mahBintrayUsername
                password mahBintrayPassword
            }
            url 'http://dl.bintray.com/my-repo-org/maven-private'
        }
    }
}

On Travis, I use secure variables so I don't have to expose these values in my public repo, but with the aim of being able to build directly from my public repo. When the build starts, you can see that the variables are exported:

Setting environment variables from .travis.yml
$ export bintrayUsername=[secure]
$ export bintrayPassword=[secure]
$ export TERM=dumb

...

FAILURE: Build failed with an exception.
* Where:
Build file '/home/travis/build/ataulm/wutson/build.gradle' line: 15
* What went wrong:
A problem occurred evaluating root project 'wutson'.
> Could not find property 'bintrayUsername' on repository container.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED

I'm unsure how to reference the exported environment variables in my build.gradle such that they would be found.

I've checked this answer which doesn't seem to work (as above), as well as this comment which results in the same build failure.

The series of commits I've tried can be seen here, with the latest: https://github.com/ataulm/wutson/commit/9331b8d91b4acf11fd3e286ff8ba1a24ed527177

like image 250
ataulm Avatar asked Dec 29 '14 23:12

ataulm


1 Answers

The error is a result of your ternary statement attempting to evaluate bintrayUsername as part of the condition.

The hasProperty() method takes a String argument so you should use hasProperty('bintrayUsername'), instead of hasProperty(bintrayUsername). Doing the latter will attempt to evaluate a property that may not exist, leading to the MissingPropertyException.

Simply remember that trying to evaluate any symbol that doesn't exist will typically result in a MissingPropertyException.

like image 78
Mark Vieira Avatar answered Oct 07 '22 06:10

Mark Vieira