Search code examples
cachinggitlabgitlab-cigitlab-ci-runner

Clearing the pipeline cache with Gitlab CI


Is it possible to invalidate or clear a pipeline cache with the Gitlab CI after a pipeline completes?

My .gitlab-ci.yml file has the following global cache definition

cache:
  key: "%CI_PIPELINE_ID%"
  paths:
    - './msvc/Project1`/bin/Debug'
    - './msvc/Project2`/bin/Debug'
    - './msvc/Project3`/bin/Debug'

The cache-key value specifies that each pipeline should maintain it's own cache, which is working fine, but the cache file continues to exist after the pipeline completes. With hundreds of pipelines being run, the size starts to add up and manually deleting the cache folder on our machine isn't a great solution.

I tried adding a cleanup job at the end of the pipeline

cleanup:
  stage: cleanup
  script:
  - rm -rf './msvc/Project1/bin'
  - rm -rf './msvc/Project2/bin'
  - rm -rf './msvc/Project3/bin'
  when: always

Which deletes the local files, but won't delete them from the cache.

Am I missing something here?

Currently running Gitlab-EE 10.3.3


Solution

  • It's not a perfect solution, but we ended up creating a cleanup job at the end of our .gitlab-ci.yaml file that deletes the cache directory from the filesystem.

    This way, each pipeline gets its own unique cache, without cluttering up the file system over time.

    cleanup_job:
      stage: cleanup
      script:
        - echo "Cleaning up"
        - rm -rf "%CACHE_PATH%/%CI_PIPELINE_ID%"
      when: always
    

    where

    CACHE_PATH: "C:/gitlab-runner/cache/group/project/repo/"