Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto generate build pipeline for gradle build using Jenkinsfile


I am trying to create a build pipeline based upon the Gradle tasks. I have viewed JenkinsFile configuration Pipeline-as-code-demo but I am unable to create a pipeline for gradle tasks. Please suggest me a possible way so that I can use the Jenkinsfile to automatically show the build pipeline just by reading the configurations from the Jenkinsfile.
Thankyou

like image 879
Aniruddha Sinha Avatar asked Mar 02 '16 16:03

Aniruddha Sinha


2 Answers

In case your project uses Gradle Wrapper you can use the following snippet in your Jenkinsfile:

stage('Gradle Build') {
    if (isUnix()) {
        sh './gradlew clean build'
    } else {
        bat 'gradlew.bat clean build'
    }
}

If you checkout to subdirectory sub-dir you might want to use

stage('Gradle Build') {
    if (isUnix()) {
        dir('sub-dir') {sh './gradlew clean build'}
    } else {
        dir('sub-dir') {bat 'gradlew.bat clean build'}
    }
}
like image 56
aventurin Avatar answered Sep 25 '22 02:09

aventurin


In case you're using Artifactory to resolve your build dependencies or to deploy your build artifacts, it is recommended to use the Pipeline DSL for Gradle build with Artifactory.

Here's an example taken from the Jenkins Pipeline Examples page:

node {
    // Get Artifactory server instance, defined in the Artifactory Plugin administration page.
    def server = Artifactory.server "SERVER_ID"
    // Create an Artifactory Gradle instance.
    def rtGradle = Artifactory.newGradleBuild()

    stage 'Clone sources'
        git url: 'https://github.com/jfrogdev/project-examples.git'

    stage 'Artifactory configuration'
        // Tool name from Jenkins configuration
        rtGradle.tool = "Gradle-2.4"
        // Set Artifactory repositories for dependencies resolution and artifacts deployment.
        rtGradle.deployer repo:'ext-release-local', server: server
        rtGradle.resolver repo:'remote-repos', server: server

    stage 'Gradle build'
        def buildInfo = rtGradle.run rootDir: "gradle-examples/4/gradle-example-ci-server/", buildFile: 'build.gradle', tasks: 'clean artifactoryPublish'

    stage 'Publish build info'
        server.publishBuildInfo buildInfo
}

Otherwise, you can simply run the gradle command with the sh or bat Pipeline steps.

like image 22
Eyal Ben Moshe Avatar answered Sep 24 '22 02:09

Eyal Ben Moshe