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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With