
Dockerのためのcontainerイメージを作成していると、イメージのレポジトリに入っていなかったりするため自前でbuildしたりする際にGitHubからダウンロードしたりするわけですが、特に事情がない限りは最新版を常にダウンロード対象にしたかったりするわけです。
どうにかしてそういう方法がないのかなと思って調べてみました。
いくつかのWebサイト
どうやら同じようなことを考える方はいらっしゃるみたいで以下のようなWebサイトが見つかります。

どうやら以下のようにしてやれば良いみたいですね。
Use curl to get the JSON response for the latest release
Use grep to find the line containing file URL
Use cut and tr to extract the URL
Use wget to download ithttps://gist.github.com/steinwaywhw/a4cd19cda655b8249d908261a62687f8
curl -s https://api.github.com/repos/jgm/pandoc/releases/latest \
| grep "browser_download_url.*deb" \
| cut -d : -f 2,3 \
| tr -d \" \
| wget -qi -
~ ❯❯❯ curl -s https://api.github.com/repos/jgm/pandoc/releases/latest \
| grep "browser_download_url.*deb" \
| cut -d : -f 2,3 \
| tr -d \"
https://github.com/jgm/pandoc/releases/download/2.9.2.1/pandoc-2.9.2.1-1-amd64.deb
おお、たしかにちゃんと最新版のこの場合はdebファイルをダウンロードするためのURLを引っ張ってこれていますね。
これはGitHubのAPIを利用している

“Get the latest release” なんてそのものなAPIでの取得方法があったんですね。
APIでのバージョン指定はある?
なら、場合に応じてバージョンを指定してダウンロードする方法はあるのかなと思い調べてみましたが、”Get a single release” がそれに該当するのかな?

とはいえ、ドキュメントにもあるように、毎回release_id
を調べて指定しないといけないのはちょっと面倒くさいかも。
GET /repos/:owner/:repo/releases/:release_id
もしくは以下のように一旦全部のURLを出力してgrepとかでフィルタリングするのが良いのですかね?
~ ❯❯❯ curl -s https://api.github.com/repos/jgm/pandoc/releases \
| grep "browser_download_url.*deb" \
| cut -d : -f 2,3 \
| tr -d \" | head
https://github.com/jgm/pandoc/releases/download/2.9.2.1/pandoc-2.9.2.1-1-amd64.deb
https://github.com/jgm/pandoc/releases/download/2.9.2/pandoc-2.9.2-1-amd64.deb
https://github.com/jgm/pandoc/releases/download/2.9.1.1/pandoc-2.9.1.1-1-amd64.deb
https://github.com/jgm/pandoc/releases/download/2.9.1/pandoc-2.9.1-1-amd64.deb
https://github.com/jgm/pandoc/releases/download/2.9/pandoc-2.9-1-amd64.deb
https://github.com/jgm/pandoc/releases/download/2.8.1/pandoc-2.8.1-1-amd64.deb
https://github.com/jgm/pandoc/releases/download/2.8.0.1/pandoc-2.8.0.1-1-amd64.deb
https://github.com/jgm/pandoc/releases/download/2.8/pandoc-2.8-1-amd64.deb
https://github.com/jgm/pandoc/releases/download/2.7.3/pandoc-2.7.3-1-amd64.deb
https://github.com/jgm/pandoc/releases/download/2.7.2/pandoc-2.7.2-1-amd64.deb
~ ❯❯❯ curl -s https://api.github.com/repos/jgm/pandoc/releases \
| grep "browser_download_url.*deb" \
| cut -d : -f 2,3 \
| tr -d \" | grep 2.6
https://github.com/jgm/pandoc/releases/download/2.6/pandoc-2.6-1-amd64.deb
この辺りの最適解をご存じの方は是非とも教えてください。
一応アプリもある
pythonでコマンドを作ってくださっている方もいらっしゃるみたいですね。
普段使用する分にはいいのかもしれませんが、自分のようにDockerのcontainerイメージを作成するために使用するには向いていませんよね。毎回このPythonパッケージを引っ張ってくるのは手間ですし。
そもそも
ただ残念なことにこの問題、GitHubのreleasesで公開されていないとAPIから情報が取得できないんですよね。私がISC Keaでbuildする際に関連するライブラリとして使用しているbotanがまさしくそうで、以下のような感じで何も返ってきません。
~ ❯❯❯ curl -s https://api.github.com/repos/randombit/botan/releases | jq .
[]
公式ドキュメントにも記載がないようなので用意されていないということですよね?

最後に
結局は今のところうまいことGitHubからは自分が望むような形でダウンロードするようにするには難しそうです。当面は横着せず都度バージョンを調べた上で指定するようにしたいと思います。