Clearing the pipeline cache with Gitlab CI

Dan picture Dan · Jan 26, 2018 · Viewed 17.4k times · Source

Is is possible to invalidate or clear a pipeline cache with 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

Answer

Dan picture Dan · Jan 29, 2018

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/"