Search code examples
javagradlejib

Unable to use custom variables in Gradle extension


I'm using JIB (not super relevant) and I want to pass in variables from command line in my deployment script.

I append using -PinputTag=${DOCKER_TAG} -PbuildEnv=nonprod in my gradle command, which is cool. But when it's missing, I want that ternary to kick in.

I'm getting the error:

Could not get unknown property 'inputTag' for project ':webserver' of type org.gradle.api.Project.
def inputTag = inputTag ?: 'latest'
def buildEnv = buildEnv ?: 'nonprod'
jib {
    container {
        mainClass = 'com.example.hi'
    }
    to {
        image = 'image/cool-image'
        tags = ['latest', inputTag]
    }
    container {
        creationTime = 'USE_CURRENT_TIMESTAMP'
        ports = ['8080']
        jvmFlags = ['-Dspring.profiles.active=' + buildEnv]
    }
}

Found Solution

def inputTag = project.hasProperty('inputTag') ? project.property('inputTag') : 'latest'
def buildEnv = project.hasProperty('buildEnv') ? project.property('buildEnv') : 'nonprod'

This seems to be working, is this the best way?


Solution

  • How about this?

    image = 'image/cool-image:' + (project.findProperty('inputTag') ?: 'latest')
    

    Note jib.to.tags are additional tags. jib.to.image = 'image/cool-image' already implies image/cool-image:latest, so no need to duplicate latest in jib.to.tags.