LoginSignup
0

More than 3 years have passed since last update.

Azure Pipelinesの「Pipeline caching (preview)」をMavenで試す

Last updated at Posted at 2019-07-25

※7/24時点でプレビューの機能なので今後変更がある可能性があります

Azure Pipelinesに個人的に待望だったPipeline cachingがプレビューで実装されました。

CIを回す時、npmやmaven等のパッケージ管理ツールで発生するダウンロードにかかる時間がビルド時間を増大させていました。こんな感じで初回のみでビルドごとに行う必要性がすくない作業はしばしばあります。
Pipeline cachingはこの点を解消できるかもしれません。(ただ、現時点では依存が多いnodejsプロジェクトでは遅くなるケースがあるようです)

メジャーなパッケージ管理ツールはドキュメントに例があるので、すぐに組み込めると思います。

組み込みと実測

昔Pipelinesで遊んだ時のナイーブなSpring Bootアプリケーションで早速試してみます。
https://dev.azure.com/fukasawah/_git/springboot-example

azure-pipelines.ymlはこんな感じに修正。簡単ですね。

変更直後のビルド。

image.png

再度Enqueueして2回目のビルド

image.png

mavenタスクだけ見ると、193秒→23秒で処理時間が約1/8になりました。
今回はコードの規模が大したことないので極端に出ましたが、それでも約3分は早く出来るはずです。

後はpom.xmlに変更が加わらない限りはこの高速化が効きます。

ただバージョンを範囲指定していたり、SNAPSHOTを使うにしていると、更新が発生しキャッシュの恩恵が受けにくくなるはずです。
npmはlockファイルを作るので、後半のキャッシュ登録を再度行ってくれるはずですが、mavenには無かったはず(effective-pomで出来るかも?)

仕組みを追う

CacheBetaを定義すると、2つのtaskを埋め込みます。

  • keyのハッシュ値に該当するキャッシュがあれば取得する(なければ何もしない)
  • 後処理(post)で、指定したPathのキャッシュをkeyのハッシュ値に基づいて行う

pom.xmlに変更が加わるとキャッシュが無効になってしまいます。

1回目と2回目のタスクの内容を見てみます。

1回目

Information, Using the following fingerprint argument (/home/vsts/work/1/s/pom.xml) as a file.
Information, Generated fingerprint: B57C33C1E5F2310961322676F7FD7FFB6130E2230823B37F7CB6B84A593FC97F00
Information, Getting a pipeline cache artifact with the following fingerprint: B57C33C1E5F2310961322676F7FD7FFB6130E2230823B37F7CB6B84A593FC97F00
Information, There is a cache miss.
Information, Pipeline cache item with fingerprint "B57C33C1E5F2310961322676F7FD7FFB6130E2230823B37F7CB6B84A593FC97F00" does not exist.
Information, Using the following fingerprint argument (/home/vsts/work/1/s/pom.xml) as a file.
Information, Generated fingerprint: B57C33C1E5F2310961322676F7FD7FFB6130E2230823B37F7CB6B84A593FC97F00
Information, Getting a pipeline cache artifact with the following fingerprint: B57C33C1E5F2310961322676F7FD7FFB6130E2230823B37F7CB6B84A593FC97F00
Information, There is a cache miss.
Information, Pipeline cache item with fingerprint "B57C33C1E5F2310961322676F7FD7FFB6130E2230823B37F7CB6B84A593FC97F00" does not exist.
Information, DedupManifestArtifactClient will correlate http requests with X-TFS-Session 869da5a9-241f-40c8-b8eb-05e6ff782dce
Information, 1805 files to be processed in 19 groups.
Information, 5 out of 1805 files processed (Group: 19/19)
Information, 105 out of 1805 files processed (Group: 17/19)
(中略)
Information, 1805 out of 1805 files processed (Group: 18/19)
Information, Processed 1805 files from /home/vsts/work/1/.m2/repository successfully.
Information, Uploading 1805 files from: /home/vsts/work/1/.m2/repository
Information, Uploaded 0.0 MB out of 54.2 MB.
(中略)
Information, Uploaded 54.2 MB out of 54.2 MB.
Information, Upload completed.
Information, 
Upload statistics:
Total Content: 54.2 MB
Physical Content Uploaded: 48.6 MB
Logical Content Uploaded: 54.2 MB
Compression Saved: 5.6 MB
Deduplication Saved: 0.0 MB
Number of Chunks Uploaded: 2,365

Information, Using the following fingerprint argument (/home/vsts/work/1/s/pom.xml) as a file.
Information, Generated fingerprint: B57C33C1E5F2310961322676F7FD7FFB6130E2230823B37F7CB6B84A593FC97F00
Information, Creating a pipeline cache artifact with the following fingerprint: B57C33C1E5F2310961322676F7FD7FFB6130E2230823B37F7CB6B84A593FC97F00
Information, Cache item created.
Saved item.

ハッシュ値はB57C33C1のようです。

1回目なのでキャッシュがないこと、キャッシュを登録している様子がわかります。

2回目

Information, Using the following fingerprint argument (/home/vsts/work/1/s/pom.xml) as a file.
Information, Generated fingerprint: B57C33C1E5F2310961322676F7FD7FFB6130E2230823B37F7CB6B84A593FC97F00
Information, Getting a pipeline cache artifact with the following fingerprint: B57C33C1E5F2310961322676F7FD7FFB6130E2230823B37F7CB6B84A593FC97F00
Information, There is a cache hit.
Manifest ID is: 8B02C61FC8EC70C3CD646355B373CE35F1D0DACFBBCA1B13A9452325C783972902
Information, DedupManifestArtifactClient will correlate http requests with X-TFS-Session d36d5e3e-7887-46b6-92c9-11fe4a8a3a91
Information, Could not initialize dataport.
Information, Downloaded 0.0 MB out of 53.9 MB (0%).
Information, Downloaded 25.4 MB out of 53.9 MB (47%).
Information, Downloaded 53.9 MB out of 53.9 MB (100%).
Information, 
Download statistics:
Total Content: 53.9 MB
Physical Content Downloaded: 48.4 MB
Compression Saved: 5.4 MB
Local Caching Saved: 0.0 MB
Chunks Downloaded: 2,361
Nodes Downloaded: 0

Information, Download completed.
Cache restored.
Information, Using the following fingerprint argument (/home/vsts/work/1/s/pom.xml) as a file.
Information, Generated fingerprint: B57C33C1E5F2310961322676F7FD7FFB6130E2230823B37F7CB6B84A593FC97F00
Information, Getting a pipeline cache artifact with the following fingerprint: B57C33C1E5F2310961322676F7FD7FFB6130E2230823B37F7CB6B84A593FC97F00
Information, There is a cache hit.
Cache with fingerprint B57C33C1E5F2310961322676F7FD7FFB6130E2230823B37F7CB6B84A593FC97F00 already exists.

2回目のハッシュ値もB57C33C1で1回目と同じでした。そのため、1回目で生成したキャッシュを見つけることができ、それをダウンロード、展開しているようです。
その後、再度ハッシュ値を計算してB57C33C1と同じハッシュ値になったため、キャッシュは保存しないようになっています。もしアップデートがあればキャッシュは更新されるでしょう。

その他

今回は1つしか定義していませんが、もちろん複数定義することができるはずです。

また、キャッシュが効いた/効いてないときだけ、というタスクを書く事も可能です。詳しくはドキュメントをお読みください。

以下のPull Request(caching pipelineの草案)をwatchしておくと動きが追えるかもしれません。

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
0