Get gradle variables in jenkins pipeline script

18,091

Solution 1

I think you actually have two different approaches to tackle this problem :

1. Get version/group from sh script

Find a way to get Gradle version from gradle build tool (e.g. gradle getVersion(), but I'm not familiar with Gradle) and then use shell script to get this version. If Gradle command to get the version is gradle getVersion(), you would do in your pipeline :

def projectVersion = sh script: "gradle getVersion()", returnStdout: true
def projectGroup= sh script: "gradle getGroup()", returnStdout: true

and then just inject your $projectVersion and $projectGroup variables in your current pipeline.

2. Configure your Gradle build script to publish to Artifactory

This is the reverse approach, which I personnaly prefer : instead of giving Artifactory all your Gradle project information, juste give Gradle your Artifactory settings and use Gradle goal to easily publish to Artifactory.

JFrog has a good documentation for this solution in their Working with Gradle section. Basically, you will follow the following steps :

  1. Generate a compliant Gradle build script from Artifactory using Gradle Build Script Generator and include it to your project build script
  2. Use Gradle goal gradle artifactoryPublish to simply publish your current artifact to Artifactory

Solution 2

For future reference here an example with the more modern declarative pipeline:

pipeline {
    agent any
    stages {
        stage('somestage') {
            steps {
                script {
                    def version = sh (
                        script: "./gradlew properties -q | grep \"version:\" | awk '{print \$2}'",
                        returnStdout: true
                    ).trim()
                    sh "echo Building project in version: $version"

                }
            }
        }
    }
}

see also:

Share:
18,091
p.streef
Author by

p.streef

Updated on June 14, 2022

Comments

  • p.streef
    p.streef almost 2 years

    I'm trying to migrate my build pipelines to the "Pipeline plugin" using the groovy build scripts.

    My pipelines are usually:

    1. Test (gradle)
    2. IntegrationTest (gradle)
    3. Build (gradle)
    4. Publish (artifactory)

    I would like to use the gradle variables like version/group etc. in my jenkins build script to publish to the correct folders in artifactory. Something the artifactory plugin would take care of for me in the past. How can this be achieved?

    For a single gradle project I use something like this:

    node('master')
    {
        def version = 1.0
        def gitUrl = 'some.git'
        def projectRoot = ""
        def group = "dashboard/frontend/"
        def artifactName = "dashboard_ui"
        def artifactRepo = "ext-release-local"
    
        stage "git"
    
        git branch: 'develop', poll: true, url: "${gitUrl}"
    
        dir(projectRoot)
        {
    
            sh 'chmod +x gradlew'
            stage "test"
            sh './gradlew clean test'
    
            stage "build"
            sh './gradlew build createPom'
    
            stage "artifact"
            def server = Artifactory.server('artifactory_dev01')
            def uploadSpec = """{
              "files": [
                {
                  "pattern": "build/**.jar",
                  "target": "${artifactRepo}/$group/${artifactName}/${version}/${artifactName}-${version}.jar"
                },
                {
                  "pattern": "pom.xml",
                  "target": "${artifactRepo}/$group/${artifactName}/${version}/${artifactName}.pom"
                }
             ]
            }"""
            def buildInfo1 = server.upload spec: uploadSpec
            server.publishBuildInfo buildInfo1
        }
    }
    
  • mturra
    mturra about 6 years
    The 1st approche doesn't work if you don't write a task getVersion and getGroup. More the gradle output is difficult to parse. The 2nd approche is good but I don't want to store artifact repository credential in gradle files. I store credential/token/certificate in CI (jenkins credential) server and I need a method to pass it to the publish task.
  • chinabuffet
    chinabuffet about 5 years
    Was the call to trim() required, or just a best practice/recommendation?
  • Yavuz Tas
    Yavuz Tas over 2 years
    Excellent answer! To make it perfect, I propose using \"^version:\" instead of \"version:\". Sometimes it can interfere if you have another Gradle property like some_dubious_library_version: 1.2.3 as I observe.