2016-11-22 35 views
2

我想了解如何通過Github API發佈發佈資源。 除此之外 Github reference, 我還沒有找到任何最近的例子。Github:使用Bash上傳發布資產

我創建了以下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中,ownerrepo變量導出個人訪問令牌,所有者的名稱和回購的名字和theAsset是資產的文件名上傳。

這是上傳版本資產的正確方法嗎?

我需要添加一個Accept標題嗎?我發現一些例子

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

但他們似乎過時給我。

在Windows可執行文件中是否存在特定的媒體(MIME)類型?

回答

1

我發現了一個官方answer

預覽時段期間

,您需要提供在Accept頭自定義介質類型:
application/vnd.github.manifold-preview+json
現在預覽期已經結束,您不再需要傳遞此自定義媒體類型。

不管怎麼說,雖然不是必需的,建議使用以下Accept頭:

application/vnd.github.v3+json 

這樣要求的API的特定版本,而不是現在的一個,和應用將在未來發生重大變化的情況下繼續工作。

+0

很好的接收,比我的答案更準確。 +1 – VonC

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。

相關問題