Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A sample of .gitlab-ci.yml for a gradle project
image: java:8-jdk
stages:
- build
- test
- deploy
before_script:
# - echo `pwd` # debug
# - echo "$CI_BUILD_NAME, $CI_BUILD_REF_NAME $CI_BUILD_STAGE" # debug
- export GRADLE_USER_HOME=`pwd`/.gradle
cache:
paths:
- .gradle/wrapper
- .gradle/caches
build:
stage: build
script:
- ./gradlew assemble
artifacts:
paths:
- build/libs/*.jar
expire_in: 1 week
only:
- master
test:
stage: test
script:
- ./gradlew check
deploy:
stage: deploy
script:
- ./deploy
after_script:
- echo "End CI"
@VinnieM

This comment has been minimized.

Show comment
Hide comment
@VinnieM

VinnieM Oct 18, 2016

If I want to use Ant instead of Gradle, how would I do that?

VinnieM commented Oct 18, 2016

If I want to use Ant instead of Gradle, how would I do that?

@modle

This comment has been minimized.

Show comment
Hide comment
@modle

modle Nov 20, 2016

Excellent.

modle commented Nov 20, 2016

Excellent.

@Ben1980

This comment has been minimized.

Show comment
Hide comment
@Ben1980

Ben1980 Dec 22, 2016

very nice

Ben1980 commented Dec 22, 2016

very nice

@nerzhul

This comment has been minimized.

Show comment
Hide comment
@nerzhul

nerzhul Mar 9, 2017

@VinnieM just don't use ant which is deprecated

nerzhul commented Mar 9, 2017

@VinnieM just don't use ant which is deprecated

@mvysny

This comment has been minimized.

Show comment
Hide comment
@mvysny

mvysny Apr 24, 2017

I'm not sure whether the script actually works. The cache references files not in $HOME, but in workspace path instead, and in my project there is nothing like .gradle/wrapper nor .gradle/caches.
EDIT: ah, missed export GRADLE_USER_HOME=pwd/.gradle - clever. In that case, maybe lock files needs to be deleted? I have seen this in Travis settings:

before_cache:
  - rm -f  $HOME/.gradle/caches/modules-2/modules-2.lock
  - rm -fr $HOME/.gradle/caches/*/plugin-resolution/

mvysny commented Apr 24, 2017

I'm not sure whether the script actually works. The cache references files not in $HOME, but in workspace path instead, and in my project there is nothing like .gradle/wrapper nor .gradle/caches.
EDIT: ah, missed export GRADLE_USER_HOME=pwd/.gradle - clever. In that case, maybe lock files needs to be deleted? I have seen this in Travis settings:

before_cache:
  - rm -f  $HOME/.gradle/caches/modules-2/modules-2.lock
  - rm -fr $HOME/.gradle/caches/*/plugin-resolution/
@mvysny

This comment has been minimized.

Show comment
Hide comment
@mvysny

mvysny Apr 24, 2017

Using reduced version:

image: java:8-jdk
before_script:
  - echo `pwd`
  - export GRADLE_USER_HOME=`pwd`/.gradle
  - rm -f  .gradle/caches/modules-2/modules-2.lock
  - rm -fr .gradle/caches/*/plugin-resolution/
cache:
  paths:
    - .gradle/wrapper
    - .gradle/caches
build:
  script:
    - ./gradlew build

Yet, every time the build is run, gradle re-downloads every dependency and also the whole gradle distribution. Does this mean that the gitlab cache is not working properly/misconfigured somehow? Edit: maybe the problem is in the cache itself: https://forum.gitlab.com/t/cache-not-working/5694

mvysny commented Apr 24, 2017

Using reduced version:

image: java:8-jdk
before_script:
  - echo `pwd`
  - export GRADLE_USER_HOME=`pwd`/.gradle
  - rm -f  .gradle/caches/modules-2/modules-2.lock
  - rm -fr .gradle/caches/*/plugin-resolution/
cache:
  paths:
    - .gradle/wrapper
    - .gradle/caches
build:
  script:
    - ./gradlew build

Yet, every time the build is run, gradle re-downloads every dependency and also the whole gradle distribution. Does this mean that the gitlab cache is not working properly/misconfigured somehow? Edit: maybe the problem is in the cache itself: https://forum.gitlab.com/t/cache-not-working/5694

@madnotdead

This comment has been minimized.

Show comment
Hide comment
@madnotdead

madnotdead Nov 7, 2017

My workaround to cache issue was to cache this:

before_script:
    - if [ ! -d /cache$ANDROID_HOME ]; then mv $ANDROID_HOME /cache$ANDROID_HOME; fi
    - export ANDROID_HOME=/cache$ANDROID_HOME
    - cd LTCfastpay
    - export GRADLE_USER_HOME=`pwd`/.gradle
    - chmod +x ./gradlew
stages:
  - build
  - test
  - deploy

build:
  stage: build 
  script:
     - ./gradlew -g /cache assembleRelease
  artifacts:
    paths:
    - /app/build/outputs/apk
  tags:
     - android

In this way, ANDROID_HOME and /gradle are cached. This reduce my build time by half.

My workaround to cache issue was to cache this:

before_script:
    - if [ ! -d /cache$ANDROID_HOME ]; then mv $ANDROID_HOME /cache$ANDROID_HOME; fi
    - export ANDROID_HOME=/cache$ANDROID_HOME
    - cd LTCfastpay
    - export GRADLE_USER_HOME=`pwd`/.gradle
    - chmod +x ./gradlew
stages:
  - build
  - test
  - deploy

build:
  stage: build 
  script:
     - ./gradlew -g /cache assembleRelease
  artifacts:
    paths:
    - /app/build/outputs/apk
  tags:
     - android

In this way, ANDROID_HOME and /gradle are cached. This reduce my build time by half.

@TamasBarta

This comment has been minimized.

Show comment
Hide comment
@TamasBarta

TamasBarta Jan 4, 2018

As far as I know, check depends on other build tasks, that are performed by assembleRelease. I'd guess in this example those tasks are run twice. I set everything in the build folder as an artifact (expiring in 1 day), so those are restored in the test job, and Gradle will find the redundant tasks up to date, so they are not run. But I'm not 100% percent satisfied, since for a 4MB APK final artifact, this means ~150MB of artifacts to be uploaded (though for 1 day only, but still). Does anyone have a better solution for that?

As far as I know, check depends on other build tasks, that are performed by assembleRelease. I'd guess in this example those tasks are run twice. I set everything in the build folder as an artifact (expiring in 1 day), so those are restored in the test job, and Gradle will find the redundant tasks up to date, so they are not run. But I'm not 100% percent satisfied, since for a 4MB APK final artifact, this means ~150MB of artifacts to be uploaded (though for 1 day only, but still). Does anyone have a better solution for that?

@NCzar

This comment has been minimized.

Show comment
Hide comment
@NCzar

NCzar Jun 27, 2018

Excellent script. Runs build and test on Gradle perfectly.

NCzar commented Jun 27, 2018

Excellent script. Runs build and test on Gradle perfectly.

@ruwanka

This comment has been minimized.

Show comment
Hide comment
@ruwanka

ruwanka Jul 4, 2018

How can we use secret variables as Gradle command line argument? For an example pass user credentials to publish task, Does anyone know a solution? You may answer on this SO, Thanks for the script.

ruwanka commented Jul 4, 2018

How can we use secret variables as Gradle command line argument? For an example pass user credentials to publish task, Does anyone know a solution? You may answer on this SO, Thanks for the script.

@deepy

This comment has been minimized.

Show comment
Hide comment
@deepy

deepy Jul 5, 2018

variables:
  GRADLE_USER_HOME: '.gradle-cache'

Works fine for me

deepy commented Jul 5, 2018

variables:
  GRADLE_USER_HOME: '.gradle-cache'

Works fine for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment