1

I'm trying to use Gitlab to create an automatic build for my Spring project. I've installed Docker on my local machine and setup Gitlab with a runner (the runner is not inside docker, I installed as regular windows service).

According to this configuration found on this stackoverflow thread I'm able to cache the .m2 folder to store all the downloaded dependencies.

Now, looking at Gitlab documentation I see that I can tag my cache that will be used across different branches. The same branch should use always the same cache to speed up the build process.

Now I tried to implement the pipeline and all is fine. But if I clear the cache and re-run the pipeline, the cache is used anyway, also if I cleared it (using the interface button).

This is the log of the pipeline:

Removing "..\\..\\..\\cache\\root\\core-library\\master-12\\cache.zip"
Removing .m2/
Removing target/
Skipping Git submodules setup
Restoring cache
Checking cache for master-13...
No URL provided, cache will not be downloaded from shared cache server. Instead a local version of cache will be extracted. 
Successfully extracted cache

If i clear the cache, the next log increase the cache number (in this case become master-14) but the cache is used anyway.. also if is the first run.

This is my simple pipeline, can someone help me to understand why the cache is used also if i forced the deletion ?

Pipeline example:

image: maven:3.6.3-openjdk-11

variables:
  MAVEN_OPTS: "-Dhttps.protocols=TLSv1.2 -Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true"
  MAVEN_CLI_OPTS: "--batch-mode --errors --fail-at-end --show-version -DinstallAtEnd=true -DdeployAtEnd=true"

cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - .m2/repository

build-job:
  stage: build  
  script:
    - echo "Cache key is $CI_COMMIT_REF_SLUG"
    - mvn $MAVEN_CLI_OPTS -DskipTests install
DV82XL
  • 5,350
  • 5
  • 30
  • 59
Mistre83
  • 2,677
  • 6
  • 40
  • 77
  • 1
    The pipeline contains configuration which is not needed or can be made easier. Fist if you use JDK11 you don't need to set TLSv1.2 that was needed for JDK7. Furthermore the whole setup for suppressing download informations can be done simpler via `mvn --no-transfer-progress` (since Maven 3.6.1 https://maven.apache.org/docs/3.6.1/release-notes.html) Also I would questioning why you do an `-DskipTests`? Is an `install` really needed? Maybe `mvn verify` enugh? – khmarbaise Feb 28 '21 at 12:57
  • Oh.. so the problem is elsewhere.. using just `MAVEN_OPTS: "-Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository"` the dependencies are re-downloaded.. so the option `--no-transfer-progress` simply hide the log.. Except the `mvn -DskipTests install` do you see problem in this pipeline? I dont understand why i'm not able to make the cache works.. :( – Mistre83 Feb 28 '21 at 15:35
  • Ok, the problems seems to be related to my local environmente, because this pipeline in gitlab.com works as espected :/ – Mistre83 Feb 28 '21 at 16:10

1 Answers1

0

The configuration seems to be fine, except the part related to mvn as said by khmarbaise, so thanks for your support!

Because i'm running Gitlab on Docker using windows, I installed the runner as windows service but for some reason this will need some kind of different configuration.

So, I decided to move the runner in a Docker container (in the same machine where i'm running gitlab - my local desktop computer). After that, the cache works! Maybe there are something related to the path path.

Anyway, those are the two commands I executed to register the runner and start the runner.

Runner registration:

docker run --rm -v ${PWD}:/etc/gitlab-runner gitlab/gitlab-runner register `
  --non-interactive `
  --executor "docker" `
  --docker-image ruby:2.6 `
  --url "http://192.168.0.151:10080/" `
  --registration-token "7e_CQCVYEyfCTwE_hZxp" `
  --description "docker-runner" `
  --tag-list "docker" `
  --run-untagged="true" `
  --locked="false" `
  --access-level="not_protected"

Runner startup:

docker run --name gitlab-runner `
     -v ${PWD}:/etc/gitlab-runner `
     -v /var/run/docker.sock:/var/run/docker.sock  `
     gitlab/gitlab-runner:latest
Mistre83
  • 2,677
  • 6
  • 40
  • 77