Cannot read and save versionCode as env variable

My workflow has two main steps where:

  1. The first step is trying to read the version code from app/build.gradle file and save it as an envman variable. It is a bash script and looks like this
  2. The second one just prints the newly saved variable.
    - activate-ssh-key@4.0.2: {}
    - git-clone@4.0.11: {}
    - script@1.1.5:
        title: Get current version code
        inputs:
        - content: |
            #!/bin/bash
            set -ex
            POR="$(grep -o "versionCode\s\+\d\+" app/build.gradle | awk '{ print $2 }')"
            echo "$POR" | envman add --key CURRENT_VERSION_CODE
    - script@1.1.5:
        title: Print version code
        inputs:
        - content: |
            #!/bin/bash
            set -ex
            echo "VERSION CODE IS: $CURRENT_VERSION_CODE"
    description: test something

My question is why am I getting empty value in the second step? What am I doing wrong?

Thanks!

Hi @vladimirpetrovski,

Did you check the build log of the Get current version code step? Is the right version number in there? As you used set -ex it should be printed in the logs if the grep command you used results in a non empty value.

The envman command looks good, probably it could be a bit easier this way - it’s easier to debug as well: envman add --key CURRENT_VERSION_CODE --value "$POR"

But the echo | version should work as well of course. In any case the Get current version code step’s build log should include the value of POR, which is the result of your grep command. I suspect that results in an empty value.

Hi @viktorbenei,

thank you for you answer. You are right, the value is empty but it shouldn’t be. I created a Bitrise step test on my computer locally and tested the same workflow and it works perfectly. I put the same app/build.gradle in _tmp folder. But when running it on the cloud it doesn’t work.

Local result test:

....
++ grep -o 'versionCode\s\+\d\+' app/build.gradle
++ awk '{ print $2 }'
+ POR=1
+ envman add --key CURRENT_VERSION_CODE --value 1
|                                                                              |
+---+---------------------------------------------------------------+----------+
| ✓ | Step Test                                                     | 0.66 sec |
+---+---------------------------------------------------------------+----------+

Here is a print out from the Get current version code step from the cloud:

+------------------------------------------------------------------------------+

| (2) Get current version code                                                 |
+------------------------------------------------------------------------------+
| id: script                                                                   |
| version: 1.1.5                                                               |
| collection: https://github.com/bitrise-io/bitrise-steplib.git                |
| toolkit: bash                                                                |
| time: 2018-07-24T10:57:22Z                                                   |
+------------------------------------------------------------------------------+
|                                                                              |
+ cat app/build.gradle
buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }
    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
repositories {
    maven { url 'https://maven.fabric.io/public' }
}
android {
    compileSdkVersion 27
    buildToolsVersion '27.0.3'
    defaultConfig {
        applicationId "com.xxx.app"
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 1
        versionName "1.0.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
... 
++ grep -o 'versionCode\s\+\d\+' app/build.gradle
++ awk '{ print $2 }'
+ POR=
+ envman add --key CURRENT_VERSION_CODE --value ''
|                                                                              |
+---+---------------------------------------------------------------+----------+
| ✓ | Get current version code                                      | 1.89 sec |
+---+---------------------------------------------------------------+----------+

Still can’t figure it out what am I doing wrong.

I’ve just figured it out why was failing on Bitrise and not locally. It is because /d is not accepted and [0-9] is. Why is that, I am not sure. I think it depends on which operating system runs. Mine is macOS, on Bitrise was Ubuntu. This link helped me solve this problem.

Posting the working example here:

#!/bin/bash
set -ex
POR="$(cat app/build.gradle | grep -o "versionCode\s\+[0-9]\+" | awk '{ print $2 }')"
envman add --key CURRENT_VERSION_CODE --value "$POR"
3 Likes

Glad to hear you found the solution - indeed, although command line tools like grep are similar on Linux and macOS there can be differences, the one you found it is indeed one of those. [0-9] of course should work on both :slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.