简体   繁体   中英

Save gradle command result into a variable in Gitlab CI/CD

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM