使用GitHub的发布功能,可以提供一个链接来下载已发布软件的特定版本。然而,每次发布时,gh-page也需要更新。

有没有一种方法可以获取到软件最新版本的特定文件的链接?

例如,这将是一个静态链接:

https://github.com/USER/PROJECT/releases/download/v0.0.0/package.zip

我想要的是:

https://github.com/USER/PROJECT/releases/download/latest/package.zip

注意:这个问题和 GitHub最新发布 这个问题特别要求访问文件, 而不是GitHub最新发布的页面


当前回答

晚了几年,但我只是实现了一个简单的重定向到支持https://github.com/USER/PROJECT/releases/latest/download/package.zip。这应该重定向到最新的带标签的package.zip发行资产。希望它很方便!

其他回答

如前所述,jq对于这个和其他REST api很有用。

Tl;dr -详情如下

假设你想要macOS版本:

URL=$( curl -s "https://api.github.com/repos/atom/atom/releases/latest" \
   | jq -r '.assets[] | select(.name=="atom-mac.zip") | .browser_download_url' )
curl -LO "$URL"

原子释放的解决方案

注意,每个repo都可以有不同的方式提供所需的工件,因此我将演示一个表现良好的工件,比如atom。

发布资产的名称

curl -s "https://api.github.com/repos/atom/atom/releases/latest" \
    | jq -r '.assets[] | .name'

atom-1.15.0-delta.nupkg
atom-1.15.0-full.nupkg
atom-amd64.deb
...

获取所需资产的下载URL

下面原子-mac是我想要的资产通过jq的选择(.name=="原子-mac.zip")

curl -s "https://api.github.com/repos/atom/atom/releases/latest" \
    | jq -r '.assets[] | select(.name=="atom-mac.zip") | .browser_download_url'

https://github.com/atom/atom/releases/download/v1.15.0/atom-mac.zip

下载工件

curl -LO "https://github.com/atom/atom/releases/download/v1.15.0/atom-mac.zip"

金桥操场

Jq语法可能很困难。这里有一个实验上面jq的操场: https://jqplay.org/s/h6_LfoEHLZ

安全

如果可能的话,您应该采取措施确保通过sha256sum和gpg下载的工件的有效性。

根据2018-05-23的GitHub支持,不可能

联系support@github.com 2018-05-23,留言:

你能否确认一下,目前除了干扰API,没有别的办法了?

他们回答说:

谢谢你伸出援手。我们建议使用API来获取最新的版本,因为这种方法是稳定的,有文档记录的,并且不会很快改变: https://developer.github.com/v3/repos/releases/#get-the-latest-release

我也会继续关注:https://github.com/isaacs/github/issues/658

没有任何依赖的Python解决方案

健壮、便携:

#!/usr/bin/env python3

import json
import urllib.request

_json = json.loads(urllib.request.urlopen(urllib.request.Request(
    'https://api.github.com/repos/cirosantilli/linux-kernel-module-cheat/releases/latest',
     headers={'Accept': 'application/vnd.github.v3+json'},
)).read())
asset = _json['assets'][0]
urllib.request.urlretrieve(asset['browser_download_url'], asset['name'])

参见:

在Python中获取HTTP GET的最快方法是什么? 基本的http文件下载和保存到磁盘在python?

还要考虑预发行版

/latest看不到预发布版本,但是很容易做到,因为/releases首先显示最新的版本:

#!/usr/bin/env python3

import json
import urllib.request

_json = json.loads(urllib.request.urlopen(urllib.request.Request(
    'https://api.github.com/repos/cirosantilli/linux-kernel-module-cheat/releases',
     headers={'Accept': 'application/vnd.github.v3+json'},
)).read())
asset = _json[0]['assets'][0]
urllib.request.urlretrieve(asset['browser_download_url'], asset['name'])

链接到版本帮助页面确实提到了“最新版本”按钮,但这并没有给你一个下载链接。

https://github.com/reactiveui/ReactiveUI/releases/latest

为此,你需要首先获得最新的标签(如“GitHub URL For最新发布的下载文件?”所述):

latestTag=$(git describe --tags `git rev-list --tags --max-count=1`)

curl -L https://github.com/reactiveui/ReactiveUI/releases/download/$latestTag/ReactiveUI-$latestTag.zip

如果你只想使用curl,你可以尝试使用-w '%{url_effective}',它会在重定向链之后打印URL(如果你使用-L调用它,后面跟着curl)。举个例子

curl -sLo /dev/null -w '%{url_effective}' https://github.com/github-tools/github/releases/latest

输出https://github.com/github-tools/github/releases/tag/v3.1.0。

在PHP -重定向到最新版本下载。简单地放在你的网络空间

<?php

/**
 * Download latest release from github release articats
 * License: Public Domain
 */

define('REPO', 'imi-digital/iRobo');

$opts = [
    'http' => [
        'method' => 'GET',
        'header' => [
            'User-Agent: PHP'
        ]
    ]
];

$context = stream_context_create($opts);

$releases = file_get_contents('https://api.github.com/repos/' . REPO . '/releases', false, $context);
$releases = json_decode($releases);

$url = $releases[0]->assets[0]->browser_download_url;

header('Location: ' . $url);