ataulm
ataulm

Reputation: 15334

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

Upvotes: 7

Views: 1462

Answers (2)

RobMcZag
RobMcZag

Reputation: 645

Below an example to define a project property -not a local variable- if it is not defined, by getting the value from the Environment, i.e. where Travis puts your secure variable.

project.ext {
    if (! project.hasProperty('some_prop')) { 
        some_prop = System.getenv('some_prop') 
    }
}

If you never set it as a property (not using gradle.properties files) and you always want to set it from the environment, just remove the IF part. :)

Note: I wanted a project property so I can use it also to set values in my spring-boot YAML file... both locally and in CI.

Upvotes: 0

Mark Vieira
Mark Vieira

Reputation: 13466

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.

Upvotes: 6

Related Questions