Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Download a private binary release file from GitHub in bash
#!/usr/bin/env bash
#
# This is an adaptation of code I wrote to download a private binary from GitHub. Such...pain.
# Why can't GitHub just offer a standardized URL you can download a release binary from and attach
# your Github Personal Access Token as a header?
#
# Since this code is an adaptation it hasn't been directly tested, but the code it was adapted from works
# and hopefully you can get the missing piece you're after by looking here.
#
set -e
# Parse CLI args
readonly github_oauth_token="$1"
readonly git_tag="$2"
readonly github_repo_owner="$3"
readonly github_repo_name="$4"
readonly release_asset_filename="$5"
readonly output_path="$6"
# Get the "github tag id" of this release
github_tag_id=$(curl --silent --show-error \
--header "Authorization: token $github_oauth_token" \
--request GET \
"https://api.github.com/repos/$github_repo_owner/$github_repo_name/releases" \
| jq --raw-output ".[] | select(.tag_name==\"$git_tag\").id")
# Get the download URL of our desired asset
download_url=$(curl --silent --show-error \
--header "Authorization: token $github_oauth_token" \
--header "Accept: application/vnd.github.v3.raw" \
--location \
--request GET \
"https://api.github.com/repos/$github_repo_owner/$github_repo_name/releases/$github_tag_id" \
| jq --raw-output ".assets[] | select(.name==\"$release_asset_filename\").url")
# Get GitHub's S3 redirect URL
# Why not just curl's built-in "--location" option to auto-redirect? Because curl then wants to include all the original
# headers we added for the GitHub request, which makes AWS complain that we're trying strange things to authenticate.
redirect_url=$(curl --silent --show-error \
--header "Authorization: token $github_oauth_token" \
--header "Accept: application/octet-stream" \
--request GET \
--write-out "%{redirect_url}" \
"$download_url")
# Finally download the actual binary
sudo curl --silent --show-error \
--header "Accept: application/octet-stream" \
--output "$output_path" \
--request GET \
"$redirect_url"
@ymgyt

This comment has been minimized.

Copy link

@ymgyt ymgyt commented May 13, 2019

It worked. thank you 👍
(# 34 $$github_repo_name -> $github_repo_name)

@josh-padnick

This comment has been minimized.

Copy link
Owner Author

@josh-padnick josh-padnick commented Oct 3, 2019

Thanks, fixed line 34!

@jcop

This comment has been minimized.

Copy link

@jcop jcop commented Oct 10, 2019

Thank you!
Little fix, line 26
select(.tag_name=="git_tag") -> select(.tag_name=="$git_tag")

@josh-padnick

This comment has been minimized.

Copy link
Owner Author

@josh-padnick josh-padnick commented Oct 10, 2019

Thanks! Fixed that one, too. :)

@pnathan

This comment has been minimized.

Copy link

@pnathan pnathan commented Oct 23, 2019

Probably don't need sudo on line 48. :)

@jonasgraudums

This comment has been minimized.

Copy link

@jonasgraudums jonasgraudums commented Mar 11, 2020

I was starting pulling hairs from my head getting binaries from GitHub. Just removed the "sudo" from line 48. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.