5

I have a (Windows) Jenkins pipeline for my Android project and I'm trying to get the versionName defined in build.gradle file. According to https://stackoverflow.com/a/51067119/5973853 and https://stackoverflow.com/a/47437369/5973853 I have the setup below:

build.gradle

android {
  defaultConfig {
    versionCode 71
    versionName "2.3.0.Beta10"
  }
}

task versionName{
  doLast {
    println android.defaultConfig.versionName
  }
}

Jenkinsfile

pipeline {
  environment {
    SERVER_PATH = '\\\\ARK_SRV\\Android\\'
  }
  agent any

  stages {

    stage('Create Directory') {
      steps {
        script {
          def VERSION_NAME = bat (script: "./gradlew -q versionName", returnStdout: true).trim()
          echo "Version Name: ${VERSION_NAME}"
          def FINAL_DIR = SERVER_PATH + VERSION_NAME
          echo "Final Directoy: ${FINAL_DIR}"
          // ...
        }
      }
    }

  }

}

However I'm getting both the whole batch command and its output into VERSION_NAME variable, so the printouts look like this:

Actual Values

Version Name: C:\Program Files (x86)\Jenkins\workspace\AndroidTest>./gradlew -q versionName 
2.3.0.Beta10

Final Directoy: \\ARK_SRV\Android\C:\Program Files (x86)\Jenkins\workspace\AndroidTest>./gradlew -q versionName 
2.3.0.Beta10

Expected Values

Version Name: 2.3.0.Beta10

Final Directoy: \\ARK_SRV\Android\2.3.0.Beta10

What am I doing wrong? Is there a better way to retrieve defaultConfig values from my build.gradle?

juliancadi
  • 987
  • 1
  • 8
  • 23

1 Answers1

3

you can split the output and take the required portion of the output in your case you can try below lines:

def output = bat (script: "./gradlew -q versionName", returnStdout: true).trim()
def  VERSION_NAME = output.split('\n')[1]
echo "Version Name: ${VERSION_NAME}"
Amit Nanaware
  • 3,203
  • 1
  • 6
  • 19