Read versionName of build.gradle from Jenkins

2020-03-17 07:18发布

I'm using Jenkins for my Android app builds. On every build, I get some info, like build number, etc...

I'm searching a way to read the versionName value of in the build.gradle when Jenkins build the job. I know we can get the buildNumber with the $BUILD_NUMBERenv variable, but how to get the versionName?

2条回答
何必那么认真
2楼-- · 2020-03-17 07:41

A better way to do this is add a "printVersion" task in build.gradle. app/build.gradle

task printVersion{
  doLast {
    print android.defaultConfig.versionName + '-' + android.defaultConfig.versionCode
  }
}

Test it with: ./gradlew -q printVersion

1.8-13

Then in jenkins pipeline, add a stage after git:

   stage('printVersion') {
        def versionInfo = sh (
            script: './gradlew -q printVersion',
            returnStdout: true
        ).trim()
        echo "VersionInfo: ${versionInfo}"
//set the current build to versionInfo plus build number.
        currentBuild.displayName = "${versionInfo}-${currentBuild.number}" ; 
   }
查看更多
家丑人穷心不美
3楼-- · 2020-03-17 07:48

You could do this by adding an Execute Shell step which determines the versionName, and exporting it as an environment variable using the EnvInject Plugin.

Assuming your build.gradle contains:

versionName = myname

You can add this script:

v=$(cat build.gradle  | grep versionName | awk '{print $3}')
echo MY_VERSION_NAME=${v} > env.properties
查看更多
登录 后发表回答