Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@daicham
Last active May 3, 2023 07:05
Show Gist options
  • Star 91 You must be signed in to star a gist
  • Fork 21 You must be signed in to fork a gist
  • Save daicham/5ac8461b8b49385244aa0977638c3420 to your computer and use it in GitHub Desktop.
Save daicham/5ac8461b8b49385244aa0977638c3420 to your computer and use it in GitHub Desktop.
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
Copy link

VinnieM commented Oct 18, 2016

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

@modle
Copy link

modle commented Nov 20, 2016

Excellent.

@Ben1980
Copy link

Ben1980 commented Dec 22, 2016

very nice

@nerzhul
Copy link

nerzhul commented Mar 9, 2017

@VinnieM just don't use ant which is deprecated

@mvysny
Copy link

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
Copy link

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
Copy link

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
Copy link

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?

@blocknik22
Copy link

Excellent script. Runs build and test on Gradle perfectly.

@ruwanka
Copy link

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
Copy link

deepy commented Jul 5, 2018

variables:
  GRADLE_USER_HOME: '.gradle-cache'

Works fine for me

@Seeraeuber
Copy link

Very useful, thanks!

@louhy
Copy link

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

@sk8geek
Copy link

sk8geek commented Mar 31, 2020

Thanks for this, new to CI and this has got me up and running :-)

@zhabba
Copy link

zhabba commented May 21, 2020

Yep, thanks a lot!

@rajesh-allanki
Copy link

can anyone provide where will be the default cache will be store and which artifacts we need to add for catching up results

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