【问题标题】:Github: Upload release assets with BashGithub:使用 Bash 上传发布资产
【发布时间】:2016-11-22 04:17:40
【问题描述】:

我想了解如何通过 Github API 上传发布资产。 除此之外 Github reference, 我还没有找到任何最近的例子。

我创建了以下 Bash 脚本:

#!/bin/sh

## Make a draft release json with a markdown body
release='"tag_name": "v1.0.0", "target_commitish": "master", "name": "myapp", '
body="This is an automatic release\\n====\\n\\nDetails follows"
body=\"$body\"
body='"body": '$body', '
release=$release$body
release=$release'"draft": true, "prerelease": false'
release='{'$release'}'
url="https://api.github.com/repos/$owner/$repo/releases"
succ=$(curl -H "Authorization: token $perstok" --data $release $url)

## In case of success, we upload a file
upload=$(echo $succ | grep upload_url)
if [[ $? -eq 0 ]]; then
    echo Release created.
else
    echo Error creating release!
    return
fi

# $upload is like:
# "upload_url": "https://uploads.github.com/repos/:owner/:repo/releases/:ID/assets{?name,label}",
upload=$(echo $upload | cut -d "\"" -f4 | cut -d "{" -f1)
upload="$upload?name=$theAsset"
succ=$(curl -H "Authorization: token $perstok" \
     -H "Content-Type: $(file -b --mime-type $theAsset)" \
     --data-binary @$theAsset $upload)

download=$(echo $succ | egrep -o "browser_download_url.+?")  
if [[ $? -eq 0 ]]; then
    echo $download | cut -d: -f2,3 | cut -d\" -f2
else
     echo Upload error!
fi

当然,perstok、owner 和 repo 变量会导出个人访问令牌、所有者姓名和仓库名称,theAsset 是要上传的资产文件名。

这是上传发布资产的正确方法吗?

我需要添加Accept 标头吗?我发现了一些例子与

-H "Accept: application/vnd.github.manifold-preview" 

但它们对我来说似乎已经过时了。

如果是 Windows 可执行文件,是否有特定的媒体 (mime) 类型?

【问题讨论】:

    标签: bash github release autorelease


    【解决方案1】:

    你还有一个example which does not use Accept header in this gist:

    # Construct url
    GH_ASSET="https://uploads.github.com/repos/$owner/$repo/releases/$id/assets?name=$(basename $filename)"
    
    curl "$GITHUB_OAUTH_BASIC" --data-binary @"$filename" -H "Authorization: token $github_api_token" -H "Content-Type: application/octet-stream" $GH_ASSET
    

    GITHUB_OAUTH_BASIC being

    ${GITHUB_OAUTH_TOKEN:?must be set to a github access token that can add assets to $repo} \
    ${GITHUB_OAUTH_BASIC:=$(printf %s:x-oauth-basic $GITHUB_OAUTH_TOKEN)}
    

    Content-Type: application/octet-stream 应该足够通用以支持任何文件,而不必担心其 MIME。

    【讨论】:

      【解决方案2】:

      我找到了官方answer:

      在预览期间,您需要在Accept 标头中提供自定义媒体类型:
      application/vnd.github.manifold-preview+json
      现在预览期已经结束,您不再需要传递此自定义媒体类型。

      无论如何,虽然不是必需的,但建议使用以下Accept 标头:

      application/vnd.github.v3+json
      

      通过这种方式请求特定版本的 API,而不是当前版本,并且应用程序将继续工作,以防未来发生重大变化。

      【讨论】:

      • 不错,比我的回答更准确。 +1
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-22
      • 2014-04-26
      • 2022-08-11
      • 2020-11-06
      • 2021-08-01
      • 2014-04-06
      • 1970-01-01
      相关资源
      最近更新 更多