bashgithubreleaseautorelease

Github: Upload release assets with Bash


I would like to learn about release asset upload through Github API. Apart from this Github reference, I haven't found any recent example.

I created the following Bash script:

#!/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

Of course perstok, owner and repo variables export the personal access token, the owner's name and the repo name and theAsset is the asset filename to upload.

Is this the proper way to upload release assets?

Do I need to add an Accept header? I found some examples with

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

but they seem outdated to me.

In case of Windows executables is there a specific media (mime) type?


Solution

  • I found an official answer:

    during the preview period, you needed to provide a custom media type in the Accept header:
    application/vnd.github.manifold-preview+json
    Now that the preview period has ended, you no longer need to pass this custom media type.

    Anyway, while not required, it is recommended to use the following Accept header:

    application/vnd.github.v3+json
    

    In this way a specific version of the API is requested, instead of the current one, and an application will keep working in case of future breaking changes.