Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get gradle variables in jenkins pipeline script

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
    }
}
like image 756
p.streef Avatar asked Aug 25 '16 08:08

p.streef


3 Answers

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
like image 20
Pom12 Avatar answered Nov 20 '22 15:11

Pom12


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:

  • Gradle plugin project version number
  • How to do I get the output of a shell command executed using into a variable from Jenkinsfile (groovy)?
like image 94
Tobi Nonymous Avatar answered Nov 20 '22 16:11

Tobi Nonymous


For others who Google'd their way here, if you have the Pipeline Utility Steps plugin and store what you need in your gradle.properties file, you can do something like this in the environment block:

MY_PROPS = readProperties file:"${WORKSPACE}/gradle.properties"
MY_VERSION = MY_PROPS['version']
like image 1
mellow-yellow Avatar answered Nov 20 '22 17:11

mellow-yellow