Is there a link to GitHub for downloading a file in the latest release of a repository?

前端 未结 18 2268
情深已故
情深已故 2020-12-02 04:26

Using GitHub\'s Release feature, it is possible to provide a link to download a specific version of the published software. However, every time a release is made, the gh-pag

相关标签:
18条回答
  • 2020-12-02 05:09

    In case you want to use in alpine, then follow these steps

     apk add curl ca-certificates wget
    

    wget -q $(curl -s https://api.github.com/repos/<USER>/<REPOSITORY>/releases/latest | grep browser_download_url | grep "$ARCH" | cut -d '"' -f 4)

    -q in wget is quite mode, if you want to see output then use without q

    0 讨论(0)
  • 2020-12-02 05:10

    In case that the repo is using just tags instead of release -- cf. jQuery -- the solutions which based on one URL does not work.

    Instead, you have to query all tags, sort them and construct the download URL. I implemented such a solution for the language Go and the jQuery repo: Link to Github.

    Perhaps, this helps someone.

    0 讨论(0)
  • 2020-12-02 05:11

    Another Linux solution using curl and wget to download a single binary file from the latest release page

    curl -s -L https://github.com/bosun-monitor/bosun/releases/latest | egrep -o '/bosun-monitor/bosun/releases/download/[0-9]*/scollector-linux-armv6' | wget --base=http://github.com/ -i - -O scollector
    

    Explanation:

    curl -s -L is to silently download the latest release HTML (after following redirect)

    egrep -o '...' uses regex to find the file you want

    wget --base=http://github.com/ -i - converts the relative path from the pipeline to absolute URL

    and -O scollector sets the desired file name.

    may be able to add -N to only download if the file is newer but S3 was giving a 403 Forbidden error.

    0 讨论(0)
  • 2020-12-02 05:13

    Not possible according to GitHub support as of 2018-05-23

    Contacted support@github.com on 2018-05-23 with message:

    Can you just confirm that there is no way besides messing with API currently?

    and they replied:

    Thanks for reaching out. We recommend using the API to fetch the latest release because that approach is stable, documented, and not subject to change any time soon:

    https://developer.github.com/v3/repos/releases/#get-the-latest-release

    I will also keep tracking this at: https://github.com/isaacs/github/issues/658

    Python solution without any dependencies

    Robust and portable:

    #!/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'])
    

    See also:

    • What is the quickest way to HTTP GET in Python?
    • Basic http file downloading and saving to disk in python?

    Also consider pre-releases

    /latest does not see pre-releases, but it is easy to do since /releases shows the latest one first:

    #!/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'])
    
    0 讨论(0)
  • 2020-12-02 05:15

    You can do an ajax request to get latest release download URL using the GitHub Releases API. It also shows when it was released and the download count:

    function GetLatestReleaseInfo() {
      $.getJSON("https://api.github.com/repos/ShareX/ShareX/releases/latest").done(function(release) {
        var asset = release.assets[0];
        var downloadCount = 0;
        for (var i = 0; i < release.assets.length; i++) {
          downloadCount += release.assets[i].download_count;
        }
        var oneHour = 60 * 60 * 1000;
        var oneDay = 24 * oneHour;
        var dateDiff = new Date() - new Date(asset.updated_at);
        var timeAgo;
        if (dateDiff < oneDay) {
          timeAgo = (dateDiff / oneHour).toFixed(1) + " hours ago";
        } else {
          timeAgo = (dateDiff / oneDay).toFixed(1) + " days ago";
        }
        var releaseInfo = release.name + " was updated " + timeAgo + " and downloaded " + downloadCount.toLocaleString() + " times.";
        $(".download").attr("href", asset.browser_download_url);
        $(".release-info").text(releaseInfo);
        $(".release-info").fadeIn("slow");
      });
    }
    
    GetLatestReleaseInfo();
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <a class="download" href="https://github.com/ShareX/ShareX/releases/latest">Download</a>
    <p class="release-info"></p>

    It is important for you to set the default button URL to the releases page (like https://github.com/ShareX/ShareX/releases/latest) so if the browser does not support ajax (or javascript) or is too slow to get the URL, the download button will still work.

    When the Ajax request completes, the URL of this button will change automatically to a direct download URL.

    Edit:

    I also made a downloads page that shows multiple releases which you can find here: https://getsharex.com/downloads/

    Source code of it: https://github.com/ShareX/sharex.github.io/blob/master/js/downloads.js

    0 讨论(0)
  • 2020-12-02 05:15

    As noted previously, jq is useful for this and other REST APIs.

    tl;dr - more details below

    Assuming you want the macOS release:

    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"
    

    Solution for atom releases

    Note each repo can have different ways of providing the desired artifact, so I will demonstrate for a well-behaved one like atom.

    Get the names of the assets published

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

    Get the download URL for the desired asset

    Below atom-mac is my desired asset via jq's select(.name=="atom-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
    

    Download the artifact

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

    jq Playground

    jq syntax can be difficult. Here's a playground for experimenting with the jq above: https://jqplay.org/s/h6_LfoEHLZ

    Security

    You should take measures to ensure the validity of the downloaded artifact via sha256sum and gpg, if at all possible.

    0 讨论(0)
提交回复
热议问题