Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Azure DevOps variables from Android Gradle

While building my Android App inside DevOps Pipeline, I want to access the predefined Azure DevOps variables from inside my gradle script, which looks like this:

apply plugin: 'com.android.application'

// When running on the CI, this will return the build-number. Otherwise use 1
def buildNumber = System.getenv("Build.BuildNumber") as Integer ?: 1

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "my.app.id"
        minSdkVersion 23
        targetSdkVersion 29
        versionCode buildNumber
        versionName "1.0." + buildNumber
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    ...
}

On Bitrise, the call def buildNumber = System.getenv("BITRISE_BUILD_NUMBER") as Integer ?: 1 worked just fine, but I have no idea, how to access the Build.BuildNumber variable in Azure DevOps Pipeline.

like image 607
Alexander Pacha Avatar asked Nov 29 '19 14:11

Alexander Pacha


People also ask

How do you access variables from variable group in Azure DevOps?

To use a variable group, open your pipeline. Select Variables > Variable groups, and then choose Link variable group. In a build pipeline, you see a list of available groups. In a release pipeline, for example, you also see a drop-down list of stages in the pipeline.

How do I set environment variables in gradle?

If you are trying to get an environment variable that might not be set, it would be better to use System. getenv('VAR') which returns null if not assigned. If you use "$System. env.


1 Answers

Ok, I figured out how to access the variables. The trick is to replace the dot with an underscore and lowercase letters with uppercase letters.

So

def buildNumber = System.getenv("Build.BuildNumber") as Integer ?: 1

becomes

def buildNumber = System.getenv("BUILD_BUILDNUMBER") as Integer ?: 1

And given that Build.BuildNumber looks like 20191129.16, which is an invalid version code, I switched to

def buildNumber = System.getenv("BUILD_BUILDID") as Integer ?: 1
like image 200
Alexander Pacha Avatar answered Nov 15 '22 08:11

Alexander Pacha