Search code examples
cachinggitlabgitlab-ci

Easy way to clean cache in Gitlab-CI (using docker image)


I have a GitLab CI which uses cache to store some files to share between stages in the same pipeline.

Extract:

default:
  image: maven:3.9.0-eclipse-temurin-17
  tags:
    - k8s-aws

stages:
  - build
  - deploy

cache:
  paths:
    - .m2/repository/
    - target/
    - pom.xml

build-branch:
  stage: build
  script:
    - some script which alter the pom.xml

deploy:
  stage: deploy
  script:
    - mvn deploy

Now at the end of the pipeline I would like to delete the pom.xml from the cache.

I tried the solution in this post but as I am using docker image, it seems not working.

I there any easy way to clean the full cache at start (and/or the end) of my pipeline?

At the end, I did the reverse operation to get the original pom.xml but I am curious to know if a specific solution to clean the cache as it can contains more than one file in other use-case.

By using a Docker image I naively though the cache would be destroyed between two runs but it seems the docker volumes are persisted.


Solution

  • It seems Cache is persisted between each pipeline execution on purpose to prevent reloading recurring files.

    I found that the right way to pass short lived files between stage and not persisted across all pipeline would be artifacts.

    From my previous example, if I want the pom to be shared between the stage I just need to do the following:

    default:
      image: maven:3.9.0-eclipse-temurin-17
      tags:
        - k8s-aws
    
    stages:
      - build
      - deploy
    
    cache:
      paths:
        - .m2/repository/
        - target/
    
    build-branch:
      stage: build
      artifacts:
        - pom.xml
      script:
        - some script which alter the pom.xml
    
    deploy:
      stage: deploy
      script:
        - mvn deploy
    

    The deploy stage will be able to read the pom.xml from the build-branch stage and it will be removed for the next execution.