Pass parameter from jenkins stage to gradle task

Viewed 128

I have this jenkins stage where I am trying to pass a paramter to a gradle task. It seems like the gradle task does not recieve it

Jenkins

stage('Version Bump') {
    steps {
        script {
            version = (new Date().format('YYYY.ww.')) + env.BUILD_NUMBER
            sh './gradlew bumpVersion --no-daemon -Pversion=${version}'
        }
    }
}

Gradle task

task bumpVersion() {
    doFirst {
        println version
    }
}

The version isnt printed. I have confirmed the value is correct by echo-ing in the jenkins file.

1 Answers

It should work if you use double quotes on the following line:

        sh "./gradlew bumpVersion --no-daemon -Pversion=${version}"

Otherwise Groovy doesn’t replace ${version} and then the run shell finds the unset version shell variable and expands it to an empty string (which is then printed by Gradle – making it appear as if nothing was printed at all).

Related