Skip to content

Commit ade9416

Browse files
author
JustinGrote
committed
Add Github Release CI
1 parent 94f6ec1 commit ade9416

2 files changed

Lines changed: 58 additions & 13 deletions

File tree

PSModule.build.ps1

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ param (
1616
#Which build files/folders should be excluded from packaging
1717
[String[]]$BuildFilesToExclude = @("Build","BuildOutput","Tests",".git*","appveyor.yml","gitversion.yml","*.build.ps1",".vscode",".placeholder"),
1818
#NuGet API Key for Powershell Gallery Deployment. Defaults to environment variable of the same name
19-
$NuGetAPIKey = $env:NuGetAPIKey,
19+
[String]$NuGetAPIKey = $env:NuGetAPIKey,
20+
#GitHub User for Github Releases. Defaults to environment variable of the same name
21+
[String]$GitHubUserName = $env:GitHubAPIKey,
2022
#GitHub API Key for Github Releases. Defaults to environment variable of the same name
21-
$GitHubAPIKey = $env:GitHubAPIKey
23+
[String]$GitHubAPIKey = $env:GitHubAPIKey
2224
)
2325

2426
#Initialize Build Environment
@@ -77,7 +79,6 @@ Enter-Build {
7779

7880
$PassThruParams = @{}
7981

80-
8182
#If the branch name is master-test, run the build like we are in "master"
8283
if ($env:BHBranchName -eq 'master-test') {
8384
write-build Magenta "Detected master-test branch, running as if we were master"
@@ -138,6 +139,9 @@ Enter-Build {
138139
#Define the Project Build Path
139140
$SCRIPT:ProjectBuildPath = $ENV:BHBuildOutput + "\" + $ENV:BHProjectName
140141
Write-Build Green "Module Build Output Path: $ProjectBuildPath"
142+
143+
#Force TLS 1.2 for all HTTPS transactions
144+
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
141145
}
142146

143147
task Clean {
@@ -380,14 +384,14 @@ task PreDeploymentChecks {
380384
if (-not (Get-Item $ProjectBuildPath/*.psd1 -erroraction silentlycontinue)) {throw "No Powershell Module Found in $ProjectBuildPath. Skipping deployment. Did you remember to build it first with {Invoke-Build Build}?"}
381385
}
382386
}
383-
387+
#TODO: Replace SkipPublish Logic with Proper invokebuild task skipping
384388
task PublishPSGallery {
385389
if (-not $SkipPublish) {
386390
if ($AppVeyor -and -not $NuGetAPIKey) {
387391
write-build DarkYellow "Couldn't find NuGetAPIKey in the Appveyor secure environment variables. Did you save your NuGet/Powershell Gallery API key as an Appveyor Secure Variable? https://docs.microsoft.com/en-us/powershell/gallery/psgallery/creating-and-publishing-an-item and https://www.appveyor.com/docs/build-configuration/"
388392
$SkipPublish = $true
389393
}
390-
if (-not $env:NuGetAPIKey) {
394+
if (-not $NuGetAPIKey) {
391395
#TODO: Add Windows Credential Store support and some kind of Linux secure storage or caching option
392396
write-build DarkYellow '$env:NuGetAPIKey was not found as an environment variable. Please specify it or use {Invoke-Build Deploy -NuGetAPIKey "MyAPIKeyString"}. Have you registered for a Powershell Gallery API key yet? https://docs.microsoft.com/en-us/powershell/gallery/psgallery/creating-and-publishing-an-item'
393397
$SkipPublish = $true
@@ -412,29 +416,70 @@ task PublishPSGallery {
412416
}
413417

414418
task PublishGitHubRelease Package,{
415-
416-
#TODO: Add Prerelease Logic when message commit says "!prerelease"
419+
#TODO: Add Prerelease Logic when message commit says "!prerelease" or is in a release branch
417420
if (-not $SkipPublish) {
418421
if ($AppVeyor -and -not $GitHubAPIKey) {
419422
write-build DarkYellow "Task PublishGitHubRelease: Couldn't find GitHubAPIKey in the Appveyor secure environment variables. Did you save your Github API key as an Appveyor Secure Variable? https://docs.microsoft.com/en-us/powershell/gallery/psgallery/creating-and-publishing-an-item and https://github.com/settings/tokens"
420423
$SkipPublish = $true
421424
}
422-
if (-not $env:GitHubAPIKey) {
425+
if (-not $GitHubAPIKey) {
423426
#TODO: Add Windows Credential Store support and some kind of Linux secure storage or caching option
424-
write-build DarkYellow 'Task PublishGitHubRelease: $env:GitHubAPIKey was not found as an environment variable. Please specify it or use {Invoke-Build Deploy -NuGetAPIKey "MyAPIKeyString"}. Have you created a GitHub API key with minimum public_repo scope permissions yet? https://github.com/settings/tokens'
427+
write-build DarkYellow 'Task PublishGitHubRelease: $env:GitHubAPIKey was not found as an environment variable. Please specify it or use {Invoke-Build Deploy -GitHubUser "MyGitHubUser" -GitHubAPIKey "MyAPIKeyString"}. Have you created a GitHub API key with minimum public_repo scope permissions yet? https://github.com/settings/tokens'
428+
$SkipPublish = $true
429+
}
430+
if (-not $GitHubUserName) {
431+
write-build DarkYellow 'Task PublishGitHubRelease: $env:GitHubUserName was not found as an environment variable. Please specify it or use {Invoke-Build Deploy -GitHubUser "MyGitHubUser" -GitHubAPIKey "MyAPIKeyString"}. Have you created a GitHub API key with minimum public_repo scope permissions yet? https://github.com/settings/tokens'
425432
$SkipPublish = $true
426433
}
427434
}
428435
if ($SkipPublish) {
429436
write-build Magenta "Task $($task.name): Skipping Publish to GitHub Releases"
430437
} else {
431-
#TODO: Add GitHubRelease Logic
432-
}
438+
#TODO: Add Prerelease Logic when message commit says "!prerelease" or is in a release branch
439+
#Inspiration from https://www.herebedragons.io/powershell-create-github-release-with-artifact
440+
441+
#Create the release
442+
$releaseData = @{
443+
tag_name = [string]::Format("v{0}", $ProjectBuildVersion);
444+
target_commitish = "master";
445+
name = [string]::Format("v{0}", $ProjectBuildVersion);
446+
body = $env:BHCommitMessage;
447+
draft = $false;
448+
prerelease = $false;
449+
}
433450

451+
$releaseParams = @{
452+
Uri = "https://api.github.com/repos/$gitHubUserName/$env:BHProjectName/releases"
453+
Method = 'POST'
454+
Headers = @{
455+
Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes($GitHubApiKey + ":x-oauth-basic"))
456+
}
457+
ContentType = 'application/json'
458+
Body = (ConvertTo-Json $releaseData -Compress)
459+
}
460+
461+
$result = Invoke-RestMethod @releaseParams -ErrorAction stop
462+
463+
$uploadUri = $result.upload_url
464+
$uploadUri = $uploadUri -creplace '\{\?name,label\}' #, "?name=$artifact"
465+
$uploadUri = $uploadUri + "?name=$(split-path $zipArchivePath -leaf)"
466+
$uploadFile = Join-Path -path $artifactOutputDirectory -childpath $artifact
467+
468+
$uploadParams = @{
469+
Uri = $uploadUri;
470+
Method = 'POST';
471+
Headers = @{
472+
Authorization = $auth;
473+
}
474+
ContentType = 'application/zip';
475+
InFile = $zipArchivePath
476+
}
477+
$result = Invoke-RestMethod @uploadParams -erroraction stop
478+
}
434479
}
435480

436481
#Deploy Supertask
437-
task Deploy PreDeploymentChecks,Package,PublishPSGallery,PublishGitHubRelease
482+
task Deploy PreDeploymentChecks,Package,PublishGitHubRelease,PublishPSGallery
438483

439484
#Build SuperTask
440485
task Build Clean,CopyFilesToBuildDir,UpdateMetadata

Tests/PowerHTML.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ Describe 'HTTP Operational Tests - REQUIRES INTERNET CONNECTION!' {
7979
$result.innertext -match 'Google' | Should Be $true
8080
}
8181
It "Can parse $uri piped from Invoke-WebRequest" {
82-
$result = Invoke-WebRequest $uri | ConvertFrom-HTML
82+
$result = Invoke-WebRequest -verbose:$false $uri | ConvertFrom-HTML
8383
$result | Should Be HtmlAgilityPack.HTMLNode
8484
$result.innertext -match 'Google' | Should Be $true
8585
}

0 commit comments

Comments
 (0)