Search code examples
jenkinsgradlecontinuous-integrationgradlew

Call gradlew from Jenkinsfile


My project contains a gradlew shell script and a Jenkinsfile in the root. The Jenkinsfile contains:

import jenkins.model.Jenkins
node('jnlp-slave-with-java-build-tools') {
    sh 'gradlew build -x check'
}

When I push this to my respository, Jenkins finds the Jenkinsfile and tries to run gradlew. It fails, however, informing me that:

Running on ECS-2d51eb80546b7 in /home/jenkins/workspace/tems_chore_add-jenkins-file-7VBJJMGP4JS3QPUSNL2ROWDE3ECV5A4EJQJ6Z5VCSPDK7UABIPVQ
[Pipeline] {
[Pipeline] sh
[tems_chore_add-jenkins-file-7VBJJMGP4JS3QPUSNL2ROWDE3ECV5A4EJQJ6Z5VCSPDK7UABIPVQ] Running shell script
+ gradlew build -x check
/home/jenkins/workspace/tems_chore_add-jenkins-file-7VBJJMGP4JS3QPUSNL2ROWDE3ECV5A4EJQJ6Z5VCSPDK7UABIPVQ@tmp/durable-838b5837/script.sh: 2: /home/jenkins/workspace/tems_chore_add-jenkins-file-7VBJJMGP4JS3QPUSNL2ROWDE3ECV5A4EJQJ6Z5VCSPDK7UABIPVQ@tmp/durable-838b5837/script.sh: gradlew: not found
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline

GitHub has been notified of this commit’s build result

ERROR: script returned exit code 127
Finished: FAILURE

How can I modify my Jenkinsfile so that the gradlew script is located and run correctly?

Followup #1

The following:

import jenkins.model.Jenkins
node('jnlp-slave-with-java-build-tools') {
    sh './gradlew build -x check'
}

Results in:

tems_chore_add-jenkins-file-7VBJJMGP4JS3QPUSNL2ROWDE3ECV5A4EJQJ6Z5VCSPDK7UABIPVQ] Running shell script
+ ./gradlew build -x check
/home/jenkins/workspace/tems_chore_add-jenkins-file-7VBJJMGP4JS3QPUSNL2ROWDE3ECV5A4EJQJ6Z5VCSPDK7UABIPVQ@tmp/durable-3c901cf0/script.sh: 2: /home/jenkins/workspace/tems_chore_add-jenkins-file-7VBJJMGP4JS3QPUSNL2ROWDE3ECV5A4EJQJ6Z5VCSPDK7UABIPVQ@tmp/durable-3c901cf0/script.sh: ./gradlew: not found

So still getting a 'not found' error.

Followup #2

Running:

import jenkins.model.Jenkins
node('jnlp-slave-with-java-build-tools') {
    files = sh (script: 'ls -l',
                returnStdout: true).trim()
    echo "${files}"

    sh './gradlew build -x check'
}

Outputs total 0, suggesting that perhaps I'm in the wrong, directory or have a permissions issue? Suggestions would be welcome!


Solution

  • This turned out be a misunderstanding on my part: I'd presumed that Jenkins would pull down the repo before it ran my Jenkinsfile, but this isn't exactly the case, and I needed to do it myself by adding:

    checkout scm
    

    I'm now receiving an unrelated error.

    This is covered in the documentation, but I was under the false impression that it woulnd't be necessary in my setup.