Jenkins build failed: gradlew: not found

1k Views Asked by At

I want to configure Jenkins 2.375.2 to build gradle project. But when I configure the pipe using Blue Ocean plugin and I run the pipeline I get error:

+ ./gradlew build
/var/lib/jenkins/workspace/jenkins_master@tmp/durable-dcccf1cd/script.sh: 1: ./gradlew: not found

Jenkins file:

pipeline {
    agent any
    stages {        
        stage('Build Image') {
            steps {
                sh "echo 'building..'"
                // configure credentials under http://192.168.1.28:8080/user/test/credentials/ and put credentials ID
                git credentialsId: '8f6bc3ab-9ef5-4d89-8e14-4972d63325c5    ', url: 'http://192.168.1.30:7990/scm/jen/spring-boot-microservice.git', branch: 'master'

                // execute Java -jar ... and build docker image
                sh './gradlew build'

                sh 'docker build -t springio/gs-spring-boot-docker .'
            }
        }
}

I tried to add Gradle config

enter image description here

But still I get the same error. Do you know how I can fix this issue?

1

There are 1 best solutions below

0
William Idakwo On

Here is the documentation from gradle on the latest release 8.1-rc-2. Based on the documentation, I've rewritten your code. Note, that I did not run this code. But I am confident, this would do the trick

pipeline {
    agent any
    tools {
        gradle 8.1 
    }
    stages {        
        stage('Build Image') {
            steps {
                sh 'gradle init'
                sh "echo 'building..'"
                git credentialsId: '8f6bc3ab-9ef5-4d89-8e14-4972d63325c5',url:'http://192.168.1.30:7990/scm/jen/spring-boot-microservice.git', 
                branch: 'master'
                withGradle {
                   sh 'gradle wrapper build'
                }
                sh 'docker build -t springio/gs-spring-boot-docker .'
            }
        }
}

In line three, I added tools{} - to specify the name of the build tool and the path to the build tool. The number '8.1' refers to the name used in the tools config as follows Manage Jenkins > Tools > Gradle > Name > 8.1

In line nine, gradle init must run before you attempt to generate a wrapper.

In line 13, withGradle{} is optional, refer to the documentation. Also, note that the correct syntax is gradle wrapper build instead of gradlew build