Skip to content

Instantly share code, notes, and snippets.

@stefanbuck
Last active October 23, 2025 10:14
Show Gist options
  • Save stefanbuck/ce788fee19ab6eb0b4447a85fc99f447 to your computer and use it in GitHub Desktop.
Save stefanbuck/ce788fee19ab6eb0b4447a85fc99f447 to your computer and use it in GitHub Desktop.

Revisions

  1. stefanbuck revised this gist Feb 14, 2018. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion upload-github-release-asset.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    #!/usr/bin/env bash
    #
    # Author: Stefan Buck
    # License: MIT
    # https://gist.github.com/stefanbuck/ce788fee19ab6eb0b4447a85fc99f447
    #
    #
    @@ -55,7 +56,7 @@ eval $(echo "$response" | grep -m 1 "id.:" | grep -w id | tr : = | tr -cd '[[:al
    [ "$id" ] || { echo "Error: Failed to get release id for tag: $tag"; echo "$response" | awk 'length($0)<100' >&2; exit 1; }

    # Upload asset
    echo "Uploading asset... $localAssetPath" >&2
    echo "Uploading asset... "

    # Construct url
    GH_ASSET="https://uploads.github.com/repos/$owner/$repo/releases/$id/assets?name=$(basename $filename)"
  2. stefanbuck revised this gist Oct 25, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion upload-github-release-asset.sh
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@
    #
    # Example:
    #
    # upload-github-release-asset.sh github_api_token=TOEK owner=stefanbuck repo=playground tag=v0.1.0 filename=./build.zip
    # upload-github-release-asset.sh github_api_token=TOKEN owner=stefanbuck repo=playground tag=v0.1.0 filename=./build.zip
    #

    # Check dependencies.
  3. stefanbuck revised this gist Oct 18, 2016. 1 changed file with 63 additions and 1 deletion.
    64 changes: 63 additions & 1 deletion upload-github-release-asset.sh
    Original file line number Diff line number Diff line change
    @@ -1 +1,63 @@
    #!/usr/bin/env bash
    #!/usr/bin/env bash
    #
    # Author: Stefan Buck
    # https://gist.github.com/stefanbuck/ce788fee19ab6eb0b4447a85fc99f447
    #
    #
    # This script accepts the following parameters:
    #
    # * owner
    # * repo
    # * tag
    # * filename
    # * github_api_token
    #
    # Script to upload a release asset using the GitHub API v3.
    #
    # Example:
    #
    # upload-github-release-asset.sh github_api_token=TOEK owner=stefanbuck repo=playground tag=v0.1.0 filename=./build.zip
    #

    # Check dependencies.
    set -e
    xargs=$(which gxargs || which xargs)

    # Validate settings.
    [ "$TRACE" ] && set -x

    CONFIG=$@

    for line in $CONFIG; do
    eval "$line"
    done

    # 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#"

    if [[ "$tag" == 'LATEST' ]]; then
    GH_TAGS="$GH_REPO/releases/latest"
    fi

    # 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 filename.
    eval $(echo "$response" | grep -m 1 "id.:" | grep -w id | tr : = | tr -cd '[[:alnum:]]=')
    [ "$id" ] || { echo "Error: Failed to get release id for tag: $tag"; echo "$response" | awk 'length($0)<100' >&2; exit 1; }

    # Upload asset
    echo "Uploading asset... $localAssetPath" >&2

    # 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
  4. stefanbuck created this gist Oct 18, 2016.
    1 change: 1 addition & 0 deletions upload-github-release-asset.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    #!/usr/bin/env bash