LoginSignup
6
5

More than 5 years have passed since last update.

GitLab の Artifact 掃除ツール

Posted at

GitLab では CI で build した成果物を artifact として保存することができます。

なんかいつのまにか GitLab サーバーのディスクの使用量が増えちゃってるなあと思ったらこの artifact が膨れ上がっていました。
gitlab-ci.ymlexpire_in で保存する期間を制限しましょう。

また、GitLab は次のようにしてバックアップを取得することができます。保存する期間を指定しておけば、バックアップ時にそれを過ぎたものは自動で削除してくれます。

gitlab-rake gitlab:backup:create

このバックアップファイルには次のファイルが含まれます。

  • repositories/
  • db
  • uploads.tar.gz
  • builds.tar.gz
  • artifacts.tar.gz
  • lfs.tar.gz

はい、 artifacts.tar.gz が含まれてます。artifact が増えるとバックアップファイルも大きくなるのです。

GitLab の画面から artifact を削除するには pipeline の build 履歴から各 build を開いて Erase ボタンをポチポチする必要があります。数が少なければポチポチやっても良いのですが大量にあると大変です。
なんかツールがないかなと思ったらやっぱりありました。

gitlab-artifact-cleanup の使い方

docker で試します

docker run -it --rm python:2.7 bash

private_token は GitLab の User Settings -> Account で確認できます

pip install python-gitlab python-dateutil pytz

cat <<_EOD_ > /root/.python-gitlab.cfg
[global]
default = gitlab1
ssl_verify = true
timeout = 5

[gitlab1]
url = https://gitlab.example.com
private_token = ****************
_EOD_

curl -O https://gitlab.com/JonathonReinhart/gitlab-artifact-cleanup/raw/master/gitlab-artifact-cleanup

python gitlab-artifact-cleanup --all-projects -m "5 days"

これで 5 日を過ぎた tag 付きでない artifact が削除されます。

--project {namespace}{project_namem} で特定のプロジェクトだけを対象にすることもできます。
-n もしくは --dry-run で削除される build を確認することができます。

python-gitlab のマズイ実装

gitlab-artifact-cleanup で使われている python-gitlab は paging されたデータをなぜか再帰呼出しして取得しようとするのでページ数が多いと

gitlab.exceptions.GitlabConnectionError: Can't connect to GitLab server (maximum recursion depth exceeded in cmp)

というエラーでコケてしまいます。

sys.setrecursionlimit(2000)

などとして回避することも可能ですが、時間ができたら直してもらうようにしよう。

gitlab-artifact-cleanup script unable to delete some artifacts #217
えー...

6
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
6
5