Skip to content

Instantly share code, notes, and snippets.

@illepic
Last active August 3, 2024 16:44
Show Gist options
  • Save illepic/32b8ad914f1dc80446c7e81c3be4e286 to your computer and use it in GitHub Desktop.
Save illepic/32b8ad914f1dc80446c7e81c3be4e286 to your computer and use it in GitHub Desktop.
Download the latest release binary from a private GitHub repo. (i.e. a .tar.gz that you have manually uploaded in a GitHub release). Update OAUTH_TOKEN, OWNER, REPO, FILE_NAME with your custom values.
#!/usr/bin/env bash
# Authorize to GitHub to get the latest release tar.gz
# Requires: oauth token, https://help.github.com/articles/creating-an-access-token-for-command-line-use/
# Requires: jq package to parse json
# Your oauth token goes here, see link above
OAUTH_TOKEN="34k234lk234lk2j3lk4j2l3k4j2kj3lk"
# Repo owner (user id)
OWNER="your-user-name"
# Repo name
REPO="the-repo-clean-name"
# The file name expected to download. This is deleted before curl pulls down a new one
FILE_NAME="file-you-are-downloading.tar.gz"
# Concatenate the values together for a
API_URL="https://$OAUTH_TOKEN:@api.github.com/repos/$OWNER/$REPO"
# Gets info on latest release, gets first uploaded asset id of a release,
# More info on jq being used to parse json: https://stedolan.github.io/jq/tutorial/
ASSET_ID=$(curl $API_URL/releases/latest | jq -r '.assets[0].id')
echo "Asset ID: $ASSET_ID"
# curl does not allow overwriting file from -O, nuke
rm -f $FILE_NAME
# curl:
# -O: Use name provided from endpoint
# -J: "Content Disposition" header, in this case "attachment"
# -L: Follow links, we actually get forwarded in this request
# -H "Accept: application/octet-stream": Tells api we want to dl the full binary
curl -O -J -L -H "Accept: application/octet-stream" "$API_URL/releases/assets/$ASSET_ID"
@Lestx
Copy link

Lestx commented Dec 17, 2016

Got an error.

Warning: Remote filename has no length!
curl: (23) Failed writing body (0 != 87)

@dankhan-adl
Copy link

I am getting

parse error: Invalid numeric literal at line 1, column 9

@Alkanov
Copy link

Alkanov commented Nov 12, 2019

Awesome! Worked like a charm

@Alkanov
Copy link

Alkanov commented Nov 13, 2019

How can we change this so we can pull the latest pre-release?

@mugdha-adhav
Copy link

Great! Worked fine for private repo. Thanks for putting this out.

@pellizzetti
Copy link

Thanks!! Worked 100%.

@RandySage
Copy link

I wanted to download all of the files for a named release, so I rewrote this to make multiple requests and iterate over files: https://github.com/ransage/examples/blob/main/github/fetch_release_artifacts.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment