Zufar Muhamadeev
Zufar Muhamadeev

Reputation: 3212

Gradle task output not reused in gitlab for kotlin build

I have created simple kotlin project with gradle build tool and pushed it to gitlab. To speed up pipeline I want to reuse tasks output. So I configured gitlab to cache build folder. Here is gitlab-ci.yml:

stages:
  - build

build:
  stage: build
  image: gradle:7.5.1-jdk17
  variables:
    GRADLE_OPTS: "-Dorg.gradle.daemon=false"
  cache:
    - key: global-cache # gradle thirdparty artifacts cache
      paths:
        - /home/gradle/.gradle
    - key: $CI_COMMIT_REF_NAME # gradle previous build task output cache
      paths:
        - build
  script:
    - ls build/libs
    - gradle -version
    - gradle build --console=plain
  artifacts:
    paths:
      - build/libs/*.jar
    reports:
      junit:
        - build/test-results/test/TEST-*.xml
    expire_in: 1 week
    when: always

In pipeline I have executed build job twice(second job run). Since build folder cached my expectation was all tasks will be UP-TO-DATE for second job. Unfortunately, both jobs have same task state:

> Task :compileKotlin
> Task :compileJava NO-SOURCE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :jar
> Task :inspectClassesForKotlinIC
> Task :assemble
> Task :compileTestKotlin
> Task :compileTestJava NO-SOURCE
> Task :processTestResources NO-SOURCE
> Task :testClasses UP-TO-DATE
> Task :test
> Task :check
> Task :build

To debug "jar" task I printed build/lib/ files to console:

$ ls build/libs
gradle-reuse-build-example.jar

You can notice build/libs have cached file inside. Do you have any ideas why jar task isn't UP-TO-DATE? How should I configurate gitlab/gradle to reuse previous job run task output?

Upvotes: 3

Views: 653

Answers (1)

Bernie
Bernie

Reputation: 2366

Your build fetches the git repo again when running each time(because gitlab uses different shared runner for job execution). That means that the timestamps of the kotlin source files are later than the classes compiled in earlier builds. As a result, the compileKotlin task is not UP-TO-DATE, causing the jar task to also be out of date.

Documentation here: https://docs.gradle.org/current/userguide/more_about_tasks.html#sec:how_does_it_work

As to how to configure the build to do what you want, you may need to look into how to reuse an existing git clone instead of cloning fresh each time. I don't even know if that's possible.

Upvotes: 2

Related Questions