Skip to content

Instantly share code, notes, and snippets.

@lankaapura
Created May 11, 2018 09:15
Show Gist options
  • Save lankaapura/be3f582603d5cb696dd5a9efae73fa67 to your computer and use it in GitHub Desktop.
Save lankaapura/be3f582603d5cb696dd5a9efae73fa67 to your computer and use it in GitHub Desktop.
Download github release artifacts
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Add-Type -AssemblyName System.IO.Compression.FileSystem
$token = Read-Host -Prompt 'Github PAT'
if(!$token) {$token = ""}
$org = Read-Host -Prompt 'Org or User'
if(!$org) {$org = ""}
$repo = Read-Host -Prompt 'Repo'
if(!$repo) {$repo = ""}
$version = Read-Host -Prompt 'Release Version(default is latest)'
if(!$version) {$version = "latest"} #v0.0.13
$saveto = Read-Host -Prompt 'Save to(default is PS script root)'
if(!$saveto) {$saveto = $PSScriptRoot}
write-host "Downloading release : $version of Repo : $repo"
$headers = @{
Authorization = "token $token"
}
if($version -eq "latest"){
$releases = "https://api.github.com/repos/$org/$repo/releases/latest"
}else{
$releases = "https://api.github.com/repos/$org/$repo/releases/tags/$version"
}
$r = ""
$r = (Invoke-WebRequest -Uri $releases -Headers $headers).Content | ConvertFrom-Json
try {
if($r)
{
# ideally we only have one asset per release
if($r.assets -and $r.assets.Count -eq 1)
{
$saveto = [System.IO.Path]::Combine($saveto, "$($repo)_$($version)")
$saveto = [System.IO.Path]::GetFullPath($saveto)
if (Test-Path -Path $saveto){
Remove-Item $saveto -Force -Recurse
}
New-Item -ItemType Directory $saveto
$r.assets | ForEach-Object {
if ($_) {
$assetUrl = $_.url
$assetName = $_.name
$headers = @{
Authorization = "token $token"
Accept = "application/octet-stream"
}
$artifactPath = [System.IO.Path]::Combine($saveto, $assetName)
$artifactPath = [System.IO.Path]::GetFullPath($artifactPath)
if (Test-Path -Path $artifactPath){
Remove-Item $artifactPath -Force
}
Invoke-WebRequest -Uri $assetUrl -Headers $headers -OutFile $artifactPath
}
}
}else{
write-host "No assets found associated with the release."
}
}else{
write-host "Release not found."
}
}
catch
{
Write-Error $_
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment