Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing the pipeline cache with Gitlab CI

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

like image 728
Dan Avatar asked Jan 26 '18 21:01

Dan


People also ask

Where is GitLab CI cache?

The location also depends on the type of executor. Locally, under the gitlab-runner user's home directory: /home/gitlab-runner/cache/<user>/<project>/<cache-key>/cache. zip . Locally, under Docker volumes: /var/lib/docker/volumes/<volume-id>/_data/<user>/<project>/<cache-key>/cache.

How does GitLab cache work?

Cache is stored where GitLab Runner is installed and uploaded to S3 if distributed cache is enabled. Use artifacts to pass intermediate build results between stages. Artifacts are generated by a job, stored in GitLab, and can be downloaded.


2 Answers

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/"
like image 44
Dan Avatar answered Sep 19 '22 13:09

Dan


Artifacts are the solution as mentioned in the comments. However there is an option to clear caches in the Pipelines page as shown in the image below.

enter image description here

like image 143
Merhawi Fissehaye Avatar answered Sep 20 '22 13:09

Merhawi Fissehaye