如何使用命令行从私人仓库下载GitHub版本

kei*_*eiw 31 command-line github

GitHub指南解释了2种授权方式,但看起来没有使用Release文件.

后果:

curl -u 'username' -L -o a.tgz https://github.com/company/repository/releases/download/TAG-NAME/A.tgz

总有类似的东西

<!DOCTYPE html> <!-- Hello future GitHubber! ...

ken*_*orb 37

下载从私人回购释放的文件,可以使用可以在生成个人访问令牌设置/令牌私人仓库完全控制范围.

然后使用curl命令下载资产(使用适当的值更改):

curl -vLJO -H 'Accept: application/octet-stream' 'https://api.github.com/repos/:owner/:repo/releases/assets/:id?access_token=:token'
Run Code Online (Sandbox Code Playgroud)

哪里:

  • :owner 是您的用户或组织用户名;
  • :repo 是你的存储库名称;
  • :id 是您的资产ID,可以在标记发布网址中找到,例如:

    https://api.github.com/repos/:owner/:repo/releases/tags/:tag 
    
    Run Code Online (Sandbox Code Playgroud)
  • :token是您的个人访问令牌(可以创建/settings/tokens;

请参阅:GitHub上的存储库API v3


这是Bash脚本,可以下载给定文件名的资产文件:

#!/usr/bin/env bash
# Script to download asset file from tag release using GitHub API v3.
# See: http://stackoverflow.com/a/35688093/55075    
CWD="$(cd -P -- "$(dirname -- "$0")" && pwd -P)"

# Check dependencies.
set -e
type curl grep sed tr >&2
xargs=$(which gxargs || which xargs)

# Validate settings.
[ -f ~/.secrets ] && source ~/.secrets
[ "$GITHUB_API_TOKEN" ] || { echo "Error: Please define GITHUB_API_TOKEN variable." >&2; exit 1; }
[ $# -ne 4 ] && { echo "Usage: $0 [owner] [repo] [tag] [name]"; exit 1; }
[ "$TRACE" ] && set -x
read owner repo tag name <<<$@

# Define variables.
GH_API="https://api.github.com"
GH_REPO="$GH_API/repos/$owner/$repo"
GH_TAGS="$GH_REPO/releases/tags/$tag"
AUTH="Authorization: token $GITHUB_API_TOKEN"
WGET_ARGS="--content-disposition --auth-no-challenge --no-cookie"
CURL_ARGS="-LJO#"

# Validate token.
curl -o /dev/null -sH "$AUTH" $GH_REPO || { echo "Error: Invalid repo, token or network issue!";  exit 1; }

# Read asset tags.
response=$(curl -sH "$AUTH" $GH_TAGS)
# Get ID of the asset based on given name.
eval $(echo "$response" | grep -C3 "name.:.\+$name" | grep -w id | tr : = | tr -cd '[[:alnum:]]=')
#id=$(echo "$response" | jq --arg name "$name" '.assets[] | select(.name == $name).id') # If jq is installed, this can be used instead. 
[ "$id" ] || { echo "Error: Failed to get asset id, response: $response" | awk 'length($0)<100' >&2; exit 1; }
GH_ASSET="$GH_REPO/releases/assets/$id"

# Download asset file.
echo "Downloading asset..." >&2
curl $CURL_ARGS -H 'Accept: application/octet-stream' "$GH_ASSET?access_token=$GITHUB_API_TOKEN"
echo "$0 done." >&2
Run Code Online (Sandbox Code Playgroud)

在运行之前,您需要GITHUB_API_TOKEN使用GitHub令牌进行设置(参见:/settings/tokensGH).这可以放在您的~/.secrets文件中,如:

GITHUB_API_TOKEN=XXX
Run Code Online (Sandbox Code Playgroud)

示例脚本用法:

./get_gh_asset.sh :owner :repo :tag :name
Run Code Online (Sandbox Code Playgroud)

其中name是您的文件名(或部分名称).使用前缀脚本TRACE=1进行调试.


如果你想知道为什么curl有时失败(如其他答案所述):

只允许一种身份验证机制; 只应指定X-Amz-Algorithm查询参数,签名查询字符串参数或Authorization标题.

当跑步时:

curl -vLJ -H 'Authorization: token <token>' -H 'Accept: application/octet-stream' https://api.github.com/repos/:owner/:repo/releases/assets/<id>
Run Code Online (Sandbox Code Playgroud)

这是因为您同时指定了多个机制,因此S3服务器不知道使用哪个机制,因此您只需选择一个,例如:

  • X-Amz-Algorithm 查询参数
  • 签名查询字符串参数(X-Amz-Signature)
  • 授权标题(Authorization: token <token>)

并且由于GitHub会从资产页面重定向您(在请求时application/octet-stream),它会在查询字符串中自动填充凭据,因为curl它会在请求标头(您已指定)中传递相同的凭据,因此它们是冲突的.因此,对于变通方法,您可以使用access_token.

  • “完全控制私有存储库范围”以获取发布数据?我是否可以使用其他范围,因为我只是不想授权除“仅发布”之外的所有内容 (4认同)
  • @EctoRuseff 值得庆幸的是,GitHub 上有一个新的 BETA,称为细粒度令牌,它恰好解决了这个问题(选择性回购、选择性权限(只读、写入……) (2认同)

Dwa*_*yne 10

使用gh release download轻松编写脚本下载。首先gh auth登录进行授权。

因此,要下载示例 URL,https://github.com/company/repository/releases/download/TAG-NAME/A.tgz请使用:

gh release download --repo company/repository TAG-NAME -p 'A.tgz'


nja*_*jam 9

似乎两种身份验证方法仅适用于API端点.有一个用于下载发布资产的API端点(文档):

GET /repos/:owner/:repo/releases/assets/:id
Run Code Online (Sandbox Code Playgroud)

但是人们需要知道资产的数字ID.我询问他们是否可以添加API端点以按名称下载发布资产(就像它们用于tarball一样).


更新:Github支持的回复:

我们不提供您建议的任何API.我们尝试避免在发布资产的标记或文件名发生更改时会发生变化的脆弱URL.我会将您的反馈意见带给团队进一步讨论.


Jos*_*ick 9

我们必须经常从私有GitHub repos下载发布资产,因此我们创建了fetch,这是一个开源的跨平台工具,可以轻松下载源文件并从git标记,提交或公共分支中释放资产和私人GitHub回购.

例如,foo.exe要从0.1.3私有GitHub存储库的版本下载发布资产/tmp,您将执行以下操作:

GITHUB_OAUTH_TOKEN="your token"
fetch --repo="https://github.com/foo/bar" --tag="0.1.3" --release-asset="foo.exe" /tmp
Run Code Online (Sandbox Code Playgroud)

  • 只需在此处添加注释即fetch是一个独立的可执行文件,并且完全符合原始海报的要求.我也将它整合到我团队的构建过程中. (3认同)
  • 唯一对我真正有用的答案。尝试从私人存储库的 github 版本中获取一项特定资产,只有这一项成功了。 (2认同)

two*_*rec 6

这是curl&jq一个;)班轮:

CURL="curl -H 'Authorization: token <auth_token>' \
      https://api.github.com/repos/<owner>/<repo>/releases"; \
ASSET_ID=$(eval "$CURL/tags/<tag>" | jq .assets[0].id); \
eval "$CURL/assets/$ASSET_ID -LJOH 'Accept: application/octet-stream'"
Run Code Online (Sandbox Code Playgroud)

更改<>用您的数据包围的部分。要生成auth_tokengithub.com/settings/tokens

如果你想用密码登录,请使用这个(注意它会要求输入两次密码):

CURL="curl -u <github_user> https://api.github.com/repos/<owner>/<repo>/releases"; \
ASSET_ID=$(eval "$CURL/tags/<tag>" | jq .assets[0].id); \
eval "$CURL/assets/$ASSET_ID -LJOH 'Accept: application/octet-stream'"
Run Code Online (Sandbox Code Playgroud)


phe*_*anu 5

我在这篇评论中找到了答案:https://github.com/request/request/pull/1058#issuecomment-55285276

curl将请求中的身份验证标头转发到存储Github发布资产的AmazonS3存储桶.来自S3的错误响应:

<?xml version="1.0" encoding="UTF-8"?>
<Error>
 <Code>InvalidArgument</Code>
 <Message>
   Only one auth mechanism allowed; only the X-Amz-Algorithm query parameter, Signature query string parameter or the Authorization header should be specified
 </Message>
 <ArgumentName>Authorization</ArgumentName>
 <ArgumentValue>token <yourtoken> </ArgumentValue><RequestId>4BEDDBA630688865</RequestId> <HostId>SXLsRKgKM6tPa/K7g7tSOWmQEqowG/4kf6cwOmnpObXrSzUt4bzOFuihmzxK6+gx</HostId>
</Error>
Run Code Online (Sandbox Code Playgroud)

一线wget解决方案:

wget --auth-no-challenge --header='Accept:application/octet-stream' https://<token>:@api.github.com/repos/:owner/:repo/releases/assets/:id -O app.zip
Run Code Online (Sandbox Code Playgroud)

尝试:curl -i -H "Authorization: token <token>" -H "Accept:application/octet-stream" https://<token>:@api.github.com/repos/:owner/:repo/releases/assets/:id,了解更多细节.单击添加-L以查看S3错误消息.