[GitHub][自分用メモ] Github から常に最新リリースをダウンロードしたい

AIX、UNIX、Linux

Dockerのためのcontainerイメージを作成していると、イメージのレポジトリに入っていなかったりするため自前でbuildしたりする際にGitHubからダウンロードしたりするわけですが、特に事情がない限りは最新版を常にダウンロード対象にしたかったりするわけです。

どうにかしてそういう方法がないのかなと思って調べてみました。

いくつかのWebサイト

どうやら同じようなことを考える方はいらっしゃるみたいで以下のようなWebサイトが見つかります。

Download latest GitHub release
I'd like to have "Download Latest Version" button on my website which would represent the link to the latest release (st...

どうやら以下のようにしてやれば良いみたいですね。

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 it

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 -

https://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 \" 
 https://github.com/jgm/pandoc/releases/download/2.9.2.1/pandoc-2.9.2.1-1-amd64.deb

おお、たしかにちゃんと最新版のこの場合はdebファイルをダウンロードするためのURLを引っ張ってこれていますね。

これはGitHubのAPIを利用している

Releases - GitHub Docs
Use the REST API to create, modify, and delete releases and release assets.

“Get the latest release” なんてそのものなAPIでの取得方法があったんですね。

APIでのバージョン指定はある?

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

Releases - GitHub Docs
Use the REST API to create, modify, and delete releases and release assets.

とはいえ、ドキュメントにもあるように、毎回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でコマンドを作ってくださっている方もいらっしゃるみたいですね。

GitHub - dvershinin/lastversion: Find the latest release version of an arbitrary project
Find the latest release version of an arbitrary project - GitHub - dvershinin/lastversion: Find the latest release versi...

普段使用する分にはいいのかもしれませんが、自分のようにDockerのcontainerイメージを作成するために使用するには向いていませんよね。毎回このPythonパッケージを引っ張ってくるのは手間ですし。

そもそも

ただ残念なことにこの問題、GitHubのreleasesで公開されていないとAPIから情報が取得できないんですよね。私がISC Keaでbuildする際に関連するライブラリとして使用しているbotanがまさしくそうで、以下のような感じで何も返ってきません。

~ ❯❯❯  curl -s https://api.github.com/repos/randombit/botan/releases | jq .
[]

公式ドキュメントにも記載がないようなので用意されていないということですよね?

Repositories - GitHub Docs
Use the REST API to create, manage and control the workflow of public and private GitHub repositories.

最後に

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

参考

この記事を書いた人

kometchtech

うつ病を患いながら、IT業界の末席にいるおっさんエンジニア。科学計算をしたことがないのに、HPC分野にお邪魔している。興味のある分野で学習したことをblogにまとめつつ、うつ病の経過症状のメモも置いておく日々。じつはRouterboard User Group JPの中の人でもある。 Amazon欲しいものリスト / Arm板を恵んでくれる人募集中

kometchtechをフォローする
タイトルとURLをコピーしました