If you're using a kotlin based buildfile (build.gradle.kts
) you could solve it like this:
grep -o -E 'versionName = "[[:digit:]\.]+"' app/build.gradle.kts | grep -o -E '[[:digit:]\.]+'
I prefer Grep over Gradle, because Gradle is sooooo slow. (At least in my CD pipeline.)
Whats going on in detail?
Grep is a common Posix utility to search for specific lines in text files. However it can do some pretty neat things.
-o
returns only the matched text.
-E
makes Grep use advanced regex.
In step 1 we read the build.gradle.kts
file and search for the line, that contains our version number.
grep -o -E 'versionName = "[[:digit:]\.]+"' app/build.gradle.kts
returns something like
versionName = "1.0"
In the next step we're piping this output to another grep's STDIN using a pipe symbol (|
). There we match for the version number only. The regex [[:digit:]\.]+
matches for one or more of these characters: 0123456789.
- which should be everything I expect to be in a version number.
./app/build.gradle
so the resulting.travis.yml
line is- export VERSION=$(grep "versionName" ./app/build.gradle | awk '{print $2}')
– Marmoset