Using Gradle for CI

#1

Hey! So we’re having this group project in Software Development where I’ve gotten the job as a tester. Thus, I have to put up an environment in GitLab which automatically builds the code and run the tests people have written in jUnit. Problem is, I have no experience with GitLab or build automation systems (neither do any of the others), so all of this is quite foreign to me.

I know it is common to use Maven, but I figured that if I’m first about to learn how to use such tools I might as well learn Gradle, which I’ve heard is better. So I scoured the web and found three different .gitlab-ci.yml examples, where I got one of them to build after some modification but failed the test with error “Task ‘check’ not found in root project ‘hello’”:

image: gradle:alpine

stages:
  - build
  - test

variables:
  GRADLE_OPTS: "-Dorg.gradle.daemon=false"

before_script:
  - export GRADLE_USER_HOME=`pwd`/.gradle


build:
  stage: build
  script:
    gradle --build-cache build
  artifacts:
    paths:
      - build/libs/*.jar
    expire_in: 1 week

test:
  stage: test
  script:
    gradle check


after_script:
  - echo "End CI"

The other ones failed the builds with errors:
“/bin/bash: line 72: ./gradlew: No such file or directory”
“Task ‘assemble’ not found in root project ‘hello’”

I tried finding a tutorial for how to set up Gradle in GitLab but to no avail. Would someone please be so kind and give me a step-by-step tutorial for how to set up an automatic test routine with Gradle, or maybe know of some resources that could be helpful? That would be very much appreciated! I guess I have to put a build.gradle file somewhere? I will study these things more thoroughly when I have time, but right now I just need to get it up and running before our sprint starts :grinning:

EDIT: Nevermind, I figured it out :slight_smile:

0 Likes

#2

Glad you figured it out. Can you share what your resolution was? Thanks, -MpH

0 Likes

#3

Sure, first I created a Gradle project in Eclipse using the Buildship plugin. Then I pushed all of that to the repo. I tried different YAML-files that I found on the internet but only this one worked:

image: gradle:alpine

variables:
  GRADLE_OPTS: "-Dorg.gradle.daemon=false"

before_script:
  - export GRADLE_USER_HOME=`pwd`/.gradle

stages:
  - build
  - test

build:
  stage: build
  script: ./gradlew --build-cache assemble
  cache:
    key: "$CI_COMMIT_REF_NAME"
    policy: push
    paths:
      - build
      - .gradle

test:
  stage: test
  script: ./gradlew check
  cache:
    key: "$CI_COMMIT_REF_NAME"
    policy: pull
    paths:
      - build
      - .gradle

Using “./gradlew” only worked after changing permissions for gradlew from 10644 to 10755. (You can just use “gradle” also)

Here is our build-file if you’re interested:

plugins {
    // Apply the java-library plugin to add support for Java Library
    id 'java-library'
}

apply plugin: "eclipse"

dependencies {
    // This dependency is exported to consumers, that is to say found on their compile classpath.
    api 'org.apache.commons:commons-math3:3.6.1'

    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    implementation 'com.google.guava:guava:23.0'

    // Use JUnit test framework  
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
    
    //MySQL connector
    compile 'mysql:mysql-connector-java:8.0.15'
    
    //Mockito, for mock objects
    testCompile 'org.mockito:mockito-core:2.24.0'
}

// In this section you declare where to find the dependencies of your project
repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

test {
    // Enable JUnit 5 (Gradle 4.6+).
    useJUnitPlatform()
}

I’m still trying to figure out how to make Jacoco generate a test coverage report. It works locally on my computer but on GitLab it doesn’t show any reports.

0 Likes