Skip to content

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.

Copy link

commented Oct 18, 2016

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

@modle

This comment has been minimized.

Copy link

commented Nov 20, 2016

Excellent.

@Ben1980

This comment has been minimized.

Copy link

commented Dec 22, 2016

very nice

@nerzhul

This comment has been minimized.

Copy link

commented Mar 9, 2017

@VinnieM just don't use ant which is deprecated

@mvysny

This comment has been minimized.

Copy link

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.

Copy link

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.

Copy link

commented 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.

@TamasBarta

This comment has been minimized.

Copy link

commented 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?

@NCzar

This comment has been minimized.

Copy link

commented Jun 27, 2018

Excellent script. Runs build and test on Gradle perfectly.

@ruwanka

This comment has been minimized.

Copy link

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.

Copy link

commented Jul 5, 2018

variables:
  GRADLE_USER_HOME: '.gradle-cache'

Works fine for me

@Seeraeuber

This comment has been minimized.

Copy link

commented Sep 16, 2018

Very useful, thanks!

@louhy

This comment has been minimized.

Copy link

commented Apr 28, 2019

@nerzhul Don't spread false information. Ant isn't "deprecated", they just had a new release last March. That said, I do use Gradle for new projects. But this trend is no reason to wage FUD wars against other tech.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.