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

前端 未结 18 2327
情深已故
情深已故 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: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.

提交回复
热议问题