1

I would like to save gradle-based project's version into a variable in gitlab-ci script. In my build.gradle I have:

tasks.register('version') {
    doLast {
        println(version)
    }
}

It reads version from gradle.properties (let's say version=0.1) and returns it.

I execute it as gradlew version -q so I get only result, with no unnecessary output. When using unix-style variable creation of command result, that is: version=$(./gradlew version -q), the runner ends script. Is it possible to save the output into a variable for script?

My .gitlab-ci.yml:

image: gradle:jdk11

cache: &wrapper
  paths:
    - .gradle/wrapper
    - .gradle/caches

before_script:
  - export GRADLE_USER_HOME=`pwd`/.gradle
  - chmod a+x gradlew

stages:
  - prepare
  - build
  - deploy

wrapper:
  stage: prepare
  script:
    - gradle wrapper

compile:
  stage: build
  script:
    - ./gradlew assemble
  artifacts:
    paths:
      - build/classes/**
      - build/libs/*.jar
  cache:
    <<: *wrapper
    policy: pull

properties:
  stage: deploy
  script:
    - eval version=$(./gradlew version -q)
    - echo $version # not even called

I also tried to omit eval to have version=$(./gradlew version -q) in script, but nothing changes.

CI output:

$ export GRADLE_USER_HOME=`pwd`/.gradle
$ chmod a+x gradlew
$ version=$(./gradlew version -q)
Cleaning up file based variables
Andret2344
  • 653
  • 1
  • 12
  • 33

1 Answers1

1

Ok, I've found the solution. I mustn't use variable, simply. It's needed to directly pass evaluation to another command, using double quotes, like:

.gitlab-ci.yml (part):

properties:
  stage: deploy
  script:
    - echo "$(./gradlew version -q)"

It started to work

Andret2344
  • 653
  • 1
  • 12
  • 33
  • This was helpful. Thank you. Much prefer this to grep-ing the file looking for a the version ex: https://stackoverflow.com/questions/39752984/is-it-possible-to-use-a-gradle-config-variable-in-my-gitlab-ci-file and https://stackoverflow.com/questions/53124873/how-to-get-versionname-versioncode-from-kotlin-dsl-gradle-file-in-gitlab-ci-scri – astropcr Jan 28 '22 at 16:56