Skip to content

Instantly share code, notes, and snippets.

@jimbrig
Forked from maxim/gh-dl-release
Last active September 24, 2022 03:00
Show Gist options
  • Save jimbrig/03aa65c9b20dc3ad86bbb442f723672a to your computer and use it in GitHub Desktop.
Save jimbrig/03aa65c9b20dc3ad86bbb442f723672a to your computer and use it in GitHub Desktop.
[gh-dl-release] Download Assets from Private GitHub Releases #script #bash #powershell #github #utility

Get GitHub Release Assets for Private Repo's

  1. Bash Example: gh-dl-release.sh
  2. PowerShell Example: Get-GitHubRelease

[Note]: These functions/scripts will work regardless of private or public facing repos.

Examples

PowerShell:

Get-GitHubRelease -User 'jimbrig' -Repository 'property_allocation_demo' -Tag 'v1.0.0' -Asset 'property_allocation_demo-master.zip' -OutputPath "$HOME\Downloads"

Bash:

gh-dl-release.sh "latest" "~/.local/cache/"
#!/usr/bin/env pwsh
Function Get-GitHubRelease {
<#
.SYNOPSIS
Get the latest release of a GitHub repository
.DESCRIPTION
Get the latest release of a GitHub repository
.PARAMETER User
The GitHub user or organization
.PARAMETER Repository
The GitHub repository (can be private)
.PARAMETER Token
The GitHub API token (PAT)
If not supplied will look for an Environment Variable `$env:GITHUB_API_TOKEN`.
.PARAMETER Tag
The tag to get the release for (i.e. `v1.0.0`)
.PARAMETER File
The asset to get the download URL for
.PARAMETER OutputPath
The path to save the asset to
[NOTE]: Use the directory output, not the path + file + extension.
.EXAMPLE
Get-GitHubRelease -User 'jimbrig' -Repository 'property_allocation_demo' -Tag 'v1.0.0' -Asset 'property_allocation_demo-master.zip' -OutputPath "$HOME\Downloads"
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true)]
[string]$User,
[Parameter(Mandatory=$true)]
[string]$Repository,
[Parameter(Mandatory=$false)]
[string]$Token = $env:GITHUB_API_TOKEN,
[Parameter(Mandatory=$true)]
[string]$Tag,
[Parameter(Mandatory=$true)]
[string]$File,
[Parameter(Mandatory=$true)]
[string]$OutputPath
)
$url = "https://api.github.com/repos/$User/$Repository/releases/tags/$Tag"
$headers = @{
'Authorization' = "token $Token"
'Accept' = 'application/vnd.github.v3+json'
}
$response = Invoke-RestMethod -Uri $url -Headers $headers -Method Get
$asset = $response.assets | Where-Object { $_.name -eq $File }
$asset_url = $asset.browser_download_url
$asset_path = Join-Path $OutputPath $File
# Private
$private_headers = @{
'Authorization' = "token $Token"
'Accept' = 'application/octet-stream'
}
$private_uri = "https://$($Token):@api.github.com/repos/$User/$Repository/releases/assets/$($asset.id)"
Invoke-WebRequest -SkipCertificateCheck -Uri $private_uri -Headers $private_headers -OutFile $asset_path -Resume -PassThru
}
#!/usr/bin/env bash
# TITLE:
# gh-dl-release.sh
#
# DESCRIPTION:
# This script downloads an asset from latest or specific Github release of a
# private repo.
#
# USAGE:
# gh-dl-release.sh [<version>|latest] [<path/to/output/file>]
#
# PREREQUISITES:
# - curl
# - wget
# - jq
#
# NOTES:
# - Set all the variables inside the script, make sure you chmod +x the script:
# chmod +x ./gh-dl-release.sh
# - If your version/tag doesn't match, the script will exit with error.
#
# EXAMPLES:
# - Download specific version to my_app.tar.gz
# gh-dl-release 2.1.1 my_app.tar.gz
# - Download latest version:
# gh-dl-release latest latest.tar.gz
TOKEN="<github_access_token>"
REPO="<user_or_org>/<repo_name>"
FILE="<name_of_asset_file>" # the name of your release asset file, e.g. build.tar.gz
VERSION=$1 # tag name or the word "latest"
GITHUB="https://api.github.com"
alias errcho='>&2 echo'
function gh_curl() {
curl -H "Authorization: token $TOKEN" \
-H "Accept: application/vnd.github.v3.raw" \
$@
}
if [ "$VERSION" = "latest" ]; then
# Github should return the latest release first.
parser=".[0].assets | map(select(.name == \"$FILE\"))[0].id"
else
parser=". | map(select(.tag_name == \"$VERSION\"))[0].assets | map(select(.name == \"$FILE\"))[0].id"
fi;
asset_id=`gh_curl -s $GITHUB/repos/$REPO/releases | jq "$parser"`
if [ "$asset_id" = "null" ]; then
errcho "ERROR: version not found $VERSION"
exit 1
fi;
wget -q --auth-no-challenge --header='Accept:application/octet-stream' \
https://$TOKEN:@api.github.com/repos/$REPO/releases/assets/$asset_id \
-O $2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment