5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Gitlab-CIのcacheをパイプライン終了後に消す

Last updated at Posted at 2018-04-12

Gitlab-CIで、jobをまたいでgit管理外のファイルを残したい場合、cacheを定義します。
以下のyamlを例にすると、cacheを定義することで、installジョブで作られたgit管理外のファイルをbuildジョブに引き継げます。具体的にはnode_modulesなどです。

## --- ここから
cache:
  untracked: true
  key: "$CI_COMMIT_REF_NAME"
## --- ここまで

stages:
  - pre-build
  - build

install:
  stage: pre-build
  script:
   - yarn

build:
  stage: build
  script:
    - yarn build

ところが、このcacheはCI(pipeline)が完了しても残ります。
今回の場合、cache.keyを$CI_COMMIT_REF_NAMEしているので、ブランチ毎のcacheが消えずに溜まっていきます。
ディスク使用量が80%を超えたぜアラートに怒られてしまいました。

解決策としてはふたつ。

  1. cache.keyを適当なものに変更して、ブランチ毎のキャッシュをやめる
  2. pipelineが完了したらcacheを削除するようにする

自分の場合、(1)は難しく1、(2)になりました。
それ用のパラメータがあるかなとドキュメントをあさったのですが無いらしい。
なので、自分でcacheを削除するジョブを定義してあげるのが今のところの解決策らしいです。

うーむ、綺麗ではないけど消せるようにはなりました。
もっとエレガントな方法があれば教えていただきたいです!

cache:
  untracked: true
  key: "$CI_COMMIT_REF_NAME"

stages:
  - pre-build
  - build
  - cleanup

install:
  stage: pre-build
  script:
   - yarn

build:
  stage: build
  script:
    - yarn build

## --- ここから
cleanup:
  stage: cleanup
  cache:          # 適当なcache設定をして実行時間を短縮
    key: empty
    policy: pull
  script:
    - rm -rf /home/gitlab/cache/xxx/yyy/${CI_COMMIT_REF_NAME}*
  except:
    - master
  1. ブランチ名の一部分をkeyにしたかったのですが、できなそうでした。具体的にはfeature/xxxのfeatureだけをkeyにしたいなど!

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?