git 如何下载 GitHub 版本的二进制文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25923939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 10:24:32  来源:igfitidea点击:

How do I download binary files of a GitHub release?

gitgithubbinarytagsrelease

提问by Tyler Pfaff

I have a repo with binary files in it that I need.

我有一个包含我需要的二进制文件的 repo。

I can

我可以

git checkout tags/thetagoftherelease

which seems to checkout the correct tag, but does not pull down the binary files. How can I pull down the binary files that were added to the release (the green boxes on the release)?

这似乎签出正确的标签,但不会拉下二进制文件。我如何拉下添加到发行版中的二进制文件(发行版上的绿框)?

Added picture of binary files in a release.

在发布中添加了二进制文件的图片。

enter image description here

在此处输入图片说明

回答by Ted

I've tried for days trying to find the proper answer to this, and finally I figured out how to do this via the curl command. It's a 3-step process.

我已经尝试了几天试图找到正确的答案,最后我想出了如何通过 curl 命令来做到这一点。这是一个 3 步过程。

First, to get a list of the assets for the latest release:

首先,获取最新版本的资产列表:

curl -H "Authorization: token YOURGITHUBTOKEN" \
    https://api.github.com/repos/NAME/REPO/releases/latest 

Then in the JSON, look up the url of the asset you want. For example it would look like:

然后在 JSON 中,查找所需资产的 url。例如,它看起来像:

"url": "https://api.github.com/repos/NAME/REPO/releases/assets/1275759"

Then you pass this to another curl command to retrieve the actual URL, which is actually a link to an Amazon S3 file.

然后将其传递给另一个 curl 命令以检索实际 URL,它实际上是一个指向 Amazon S3 文件的链接。

curl -H "Authorization: token YOURGITHUBTOKEN" \
     -H "Accept:application/octet-stream" \
     -i https://api.github.com/repos/NAME/REPO/releases/assets/1275759

The URL will be in the "location" field of the HTTP response, and then use curl to get the file like this:

该 URL 将在 HTTP 响应的“位置”字段中,然后使用 curl 获取文件,如下所示:

curl "https://github-cloud.s3.amazonaws.com...." -i -o FILENAME

回答by Chris

Binary release assets exist outside of Git, and cannot be managed using the standard tools.

二进制发布资产存在于 Git 之外,无法使用标准工具进行管理。

They should be available via GitHub's API, though.

不过,它们应该可以通过 GitHub 的 API 获得。

  1. List the repository's release assets:

    GET /repos/:owner/:repo/releases/:id/assets
    

    This will send back a JSON document listing the release assets for the repository, e.g.

    [
      {
        "url": "https://api.github.com/repos/octocat/Hello-World/releases/assets/1",
        "browser_download_url": "https://github.com/octocat/Hello-World/releases/download/v1.0.0/example.zip",
        "id": 1,
        "name": "example.zip",
        "label": "short description",
        "state": "uploaded",
        "content_type": "application/zip",
        "size": 1024,
        "download_count": 42,
        "created_at": "2013-02-27T19:35:32Z",
        "updated_at": "2013-02-27T19:35:32Z",
        "uploader": {
          "login": "octocat",
          ...
        }
      }
    ]
    
  2. Retrieve the assts from the release you want, as defined by its idfrom above:

    GET /repos/:owner/:repo/releases/assets/:id
    

    If you want to download the asset's binary content, pass a media type of "application/octet-stream". The API will either redirect the client to the location, or stream it directly if possible. API clients should handle both a 200or 302response.

  1. 列出存储库的发布资产

    GET /repos/:owner/:repo/releases/:id/assets
    

    这将发回一个 JSON 文档,其中列出了存储库的发布资产,例如

    [
      {
        "url": "https://api.github.com/repos/octocat/Hello-World/releases/assets/1",
        "browser_download_url": "https://github.com/octocat/Hello-World/releases/download/v1.0.0/example.zip",
        "id": 1,
        "name": "example.zip",
        "label": "short description",
        "state": "uploaded",
        "content_type": "application/zip",
        "size": 1024,
        "download_count": 42,
        "created_at": "2013-02-27T19:35:32Z",
        "updated_at": "2013-02-27T19:35:32Z",
        "uploader": {
          "login": "octocat",
          ...
        }
      }
    ]
    
  2. 从你想要的版本中检索 assts,如id上面定义的那样:

    GET /repos/:owner/:repo/releases/assets/:id
    

    如果要下载资产的二进制内容,请传递媒体类型"application/octet-stream". API 要么将客户端重定向到该位置,要么在可能的情况下直接流式传输它。API 客户端应该同时处理 a200302响应。

As documented, these requests are all relative to https://api.github.com.

正如所记录的,这些请求都是相对于https://api.github.com.

回答by Rodrigo Estebanez

For Ansible, you can use this tasklist (same steps as @ted):

对于 Ansible,您可以使用此任务列表(与 @ted 的步骤相同):

- name: Get latest version
  uri:
    url: "https://api.github.com/repos/{{github_user}}/{{github_repo}}/releases/latest"
    return_content: yes
    headers:
      Authorization: "token {{ vault_github_deploying_token }}"
  register: github_response

- set_fact:
    binary_asset_url: "{{  github_response.json.assets|json_query(query) }}"
  vars:
    query: "[?name=='{{your_github_binary_filename}}'].url | [0]"   

- name: Get Binary asset's location
  uri:
    url: "{{ binary_asset_url }}"
    return_content: no
    follow_redirects: none
    status_code: 302
    headers:
      Authorization: "token {{ vault_github_deploying_token }}"
      Accept: "application/octet-stream"
  register: assets

- name: Download binary
  get_url:
    url: "{{ assets.location }}"
    dest: "/tmp/{{ your_github_binary_filename }}"
    mode: 0755

回答by Sylvain

Note that wgetcat get it directly by following download link as a browser do.

请注意,wgetcat 可以像浏览器一样通过下载链接直接获取它。

Rewrite $URLas you need. My convention is to upload with a suffix for binary version.

$URL根据需要重写。我的约定是上传后缀为二进制版本。

A sample script:

示例脚本:

#!/bin/bash
#
# You can fetch some binary directly from release on github
#
# We encourage to build your own version from source.
#

GIT_USER=me
GIT_PROJECT=project_name
BASE_URL=https://github.com/$GIT_USER/$GIT_PROJECT/releases/download
RELEASE=v0.6.3-alpha1
BINARY=bin_file_on_release

if [[ -e $BINARY ]]
then
  echo "file in the way: '$BINARY' remove it."
  exit 1
fi

if [[ $(getconf LONG_BIT) == "64" ]]
then
    echo "I'm 64-bits"
    URL="$BASE_URL/$RELEASE/$BINARY"
else
    echo "I'm 32-bits"
    URL="$BASE_URL/$RELEASE/${BINARY}-32bits"
fi

set -e
echo "Fetching from: $URL"
wget -q -O $BINARY "$URL"
file $BINARY
chmod a+x $BINARY

回答by pglez82

This is a mini script to download an asset knowing its file name (can be easily modified to download other assets:

这是一个下载资产的小脚本,知道其文件名(可以轻松修改以下载其他资产:

asseturl=$(curl -H "Authorization: token ${token}" https://api.github.com/repos/${repo}/releases/${releaseid}/assets | jq ".[] | select(.name==\"${filename}\") | .url")
curl -L -H "Authorization: token ${token}" -H "Accept:application/octet-stream" $(echo $asseturl | tr -d '"') > ${filename}
  • $tokenis the access token for github
  • $filenameis the filename for the asset
  • $releaseidis the releaseid where the binary file is stored
  • $token是github的访问令牌
  • $filename是资产的文件名
  • $releaseid是存储二进制文件的releaseid

回答by F1Linux

One-Step Process:

一步法:

Here's a one-liner (sans auth) I used in a script to download Digital Ocean's doctlinterface from Github. It just grabs the LATESTrelease and unTARs it:

这里是(一个一行SANS AUTH)我在脚本用来下载数字海洋doctl从GitHub接口。它只是获取最新版本并解压缩它:

curl -sL $(curl -s https://api.github.com/repos/digitalocean/doctl/releases/latest | grep "http.*linux-amd64.tar.gz" | awk '{print }' | sed 's|[\"\,]*||g') | tar xzvf -

If not using Linux, replace "http.*linux-amd64.tar.gz"in the above with "darwin", "windows", etc and the appropriate arch.

如果不使用 Linux,请将"http.*linux-amd64.tar.gz"上述内容替换为“darwin”、“windows”等以及相应的 arch。

NOTE: The above shouldwork programmatically grabbing binaries from other Github repos, but I've only tested it with doctl. HTH-

注意:以上应该以编程方式从其他 Github 存储库中抓取二进制文件,但我只使用doctl. HTH-