LoginSignup
12
12

More than 5 years have passed since last update.

GitHub Releases にアップロードしてある最新バージョンの成果物を取得するワンライナー

Posted at

e.g. dtan4/s3url 最新バージョンの Linux 64bit バイナリが入った tarball を取得する

$ curl -s https://api.github.com/repos/dtan4/s3url/releases | jq -r '.[0].assets[] | select(.name | test("linux-amd64.tar.gz")) | .browser_download_url'
https://github.com/dtan4/s3url/releases/download/v0.3.1/s3url-v0.3.1-linux-amd64.tar.gz

サーバに Go バイナリを配置していて定期的に更新したい場合は、このワンライナーと展開処理を行うスクリプトを cron とかで回しておけばよさそうです。

curl -s https://api.github.com/repos/dtan4/s3url/releases

dtan4/s3url リポジトリの Releases を取得する GitHub API です。
Public リポジトリなら認証無しで叩けますが、その場合の呼出回数は1時間あたり最大60回となってるので、Access Token を付与するほうが安心です。

Releases | GitHub Developer Guide

jq -r

-r オプションをつけると、ダブルクオートで囲まずそのまま文字列を返します。

.[0].assets[]

上記 API のレスポンスは Release 作成時刻 created_at 降順になっているので、先頭の要素が最新の Release となります。assets に成果物 (asset) のメタデータが配列で格納されてます。

select(.name | test("linux-amd64.tar.gz"))

name フィールドが linux-amd64.tar.gz にマッチする asset を抜き出します。test は与えられた正規表現にマッチするかどうかを true/false で返す jq 関数です。

jq Manual (development version)

ここまでで、こういう JSON が得られます。

{
  "url": "https://api.github.com/repos/dtan4/s3url/releases/assets/2505550",
  "id": 2505550,
  "name": "s3url-v0.3.1-linux-amd64.tar.gz",
  "label": "",
  "uploader": {
    "login": "dtan4",
    "id": 680124,
    "avatar_url": "https://avatars.githubusercontent.com/u/680124?v=3",
    "gravatar_id": "",
    "url": "https://api.github.com/users/dtan4",
    "html_url": "https://github.com/dtan4",
    "followers_url": "https://api.github.com/users/dtan4/followers",
    "following_url": "https://api.github.com/users/dtan4/following{/other_user}",
    "gists_url": "https://api.github.com/users/dtan4/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/dtan4/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/dtan4/subscriptions",
    "organizations_url": "https://api.github.com/users/dtan4/orgs",
    "repos_url": "https://api.github.com/users/dtan4/repos",
    "events_url": "https://api.github.com/users/dtan4/events{/privacy}",
    "received_events_url": "https://api.github.com/users/dtan4/received_events",
    "type": "User",
    "site_admin": false
  },
  "content_type": "application/gzip",
  "state": "uploaded",
  "size": 1773482,
  "download_count": 3,
  "created_at": "2016-10-20T15:58:38Z",
  "updated_at": "2016-10-20T15:58:38Z",
  "browser_download_url": "https://github.com/dtan4/s3url/releases/download/v0.3.1/s3url-v0.3.1-linux-amd64.tar.gz"
}

.browser_download_url

成果物そのものの URL を抜き出します。

12
12
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
12
12