Last active
February 25, 2018 22:39
-
-
Save leachdaniel/6ef2467e8c728d226d99437fd08083cc to your computer and use it in GitHub Desktop.
Upload Release Artifact By Tag Name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Upload Release Artifact By Tag Name | |
# If the release doesn't exist it is created | |
# https://developer.github.com/v3/repos/releases/#upload-a-release-asset | |
$tagName = "test" | |
$owner = "leachdaniel" | |
$repo = "TestGitHubApi" | |
$gitHubBaseUri = "https://api.github.com" | |
$filePath = "T:\files.zip" | |
$fileContentType = "application/zip" | |
$apiToken = $env:GITHUBAPITOKEN | |
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 | |
$releaseByTagUri = "$gitHubBaseUri/repos/$owner/$repo/releases/tags/$tagName" | |
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("leachdaniel:$apiToken"))) | |
$headers = @{Authorization=("Basic $base64AuthInfo")} | |
try { | |
$release = Invoke-WebRequest -Uri $releaseByTagUri -Method GET -Headers $headers -UseBasicParsing | |
} | |
catch { | |
if ($_.Exception.Response.StatusCode -eq 404) { | |
Write-Host "Didn't exist creating..." | |
$body = @{ | |
tag_name = $tagName | |
name = $tagName | |
body = ("Release " + $tagName) | |
draft = $false | |
prerelease = $false | |
} | |
$createReleaseUrl = "$gitHubBaseUri/repos/$owner/$repo/releases" | |
$release = Invoke-WebRequest -Uri $createReleaseUrl -Method Post -Headers $headers -Body (ConvertTo-Json $body) -UseBasicParsing | |
} | |
else { | |
throw | |
exit 1; | |
} | |
} | |
# assets_url : https://api.github.com/repos/leachdaniel/TestGitHubApi/releases/9826535/assets | |
# upload_url : https://uploads.github.com/repos/leachdaniel/TestGitHubApi/releases/9826535/assets{?name,label} | |
$upload_url = (($release.Content | ConvertFrom-Json).upload_url.Replace("{?name,label}","")) | |
$uri = "$($upload_url)?name=$(Split-Path $filePath -leaf)" | |
# or include time specific if you have more than one with the same name | |
# $uri = "$($upload_url)?name=$((Get-Date -format s).Replace(":","."))_$(Split-Path $filePath -leaf)" | |
Write-Host "Uploading to $uri" | |
$body = Get-Content($filePath) -Raw | |
Invoke-WebRequest -Uri $uri -Method Post -ContentType $fileContentType -Body $body -Headers $headers -UseBasicParsing | Write-Host |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment