Skip to content

Commit

Permalink
updated psake script and tags on module
Browse files Browse the repository at this point in the history
  • Loading branch information
scrthq committed Nov 25, 2018
1 parent 6f3653f commit 6073306
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -12,3 +12,4 @@ FunctionBackups
FunctionBackups/*
BuildOutput/*
*config.csv
PSGSuite.zip
2 changes: 1 addition & 1 deletion PSGSuite/PSGSuite.psd1
Expand Up @@ -128,7 +128,7 @@ All other functions are either intact or have an alias included to support backw
# Prerelease = '-alpha'

# Tags applied to this module. These help with module discovery in online galleries.
Tags = 'Google', 'GSuite', 'Apps', 'G', 'Suite', 'REST', 'API', 'Admin', 'PSModule', 'Directory', 'User', 'Goo.gl', 'PSEdition_Core', 'PSEdition_Desktop'
Tags = 'GSuite', 'Google', 'Apps', 'API', 'Drive', 'Gmail', 'Admin', 'Automation', 'PSEdition_Core', 'PSEdition_Desktop'

# A URL to the license for this module.
# LicenseUri = ''
Expand Down
136 changes: 130 additions & 6 deletions psake.ps1
Expand Up @@ -53,6 +53,10 @@ task Init {
} -description 'Initialize build environment'

task Clean -depends Init {
$zipPath = [System.IO.Path]::Combine($PSScriptRoot,"$($env:BHProjectName).zip")
if (Test-Path $zipPath) {
Remove-Item $zipPath -Force
}
Remove-Module -Name $env:BHProjectName -Force -ErrorAction SilentlyContinue

if (Test-Path -Path $outputDir) {
Expand Down Expand Up @@ -214,7 +218,7 @@ Task Import -Depends Compile {
$pesterScriptBlock = {
Push-Location
Set-Location -PassThru $outputModDir
if(-not $ENV:BHProjectPath) {
if (-not $ENV:BHProjectPath) {
Set-BuildEnvironment -Path $PSScriptRoot\..
}

Expand All @@ -228,9 +232,9 @@ $pesterScriptBlock = {
$testResultsXml = Join-Path -Path $outputDir -ChildPath $TestFile
$pesterParams = @{
OutputFormat = 'NUnitXml'
OutputFile = $testResultsXml
PassThru = $true
Path = $tests
OutputFile = $testResultsXml
PassThru = $true
Path = $tests
}
if ($PSVersionTable.PSVersion.Major -lt 6) {
### $pesterParams['CodeCoverage'] = (Join-Path $outputModVerDir "$($env:BHProjectName).psm1")
Expand All @@ -257,7 +261,87 @@ task Test -Depends Compile $pesterScriptBlock -description 'Run Pester tests'
task TestOnly -Depends Init $pesterScriptBlock -description 'Run Pester tests only [no module compilation]'

$deployScriptBlock = {
function Publish-GitHubRelease {
<#
.SYNOPSIS
Publishes a release to GitHub Releases. Borrowed from https://www.herebedragons.io/powershell-create-github-release-with-artifact
#>
[CmdletBinding()]
Param (
[parameter(Mandatory = $true)]
[String]
$VersionNumber,
[parameter(Mandatory = $false)]
[String]
$CommitId = 'master',
[parameter(Mandatory = $true)]
[String]
$ReleaseNotes,
[parameter(Mandatory = $true)]
[ValidateScript( {Test-Path $_})]
[String]
$ArtifactPath,
[parameter(Mandatory = $true)]
[String]
$GitHubUsername,
[parameter(Mandatory = $true)]
[String]
$GitHubRepository,
[parameter(Mandatory = $true)]
[String]
$GitHubApiKey,
[parameter(Mandatory = $false)]
[Switch]
$PreRelease,
[parameter(Mandatory = $false)]
[Switch]
$Draft
)
$releaseData = @{
tag_name = [string]::Format("v{0}", $VersionNumber)
target_commitish = $CommitId
name = [string]::Format("$($env:BHProjectName) v{0}", $VersionNumber)
body = $ReleaseNotes
draft = [bool]$Draft
prerelease = [bool]$PreRelease
}

$auth = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes($gitHubApiKey + ":x-oauth-basic"))

$releaseParams = @{
Uri = "https://api.github.com/repos/$GitHubUsername/$GitHubRepository/releases"
Method = 'POST'
Headers = @{
Authorization = $auth
}
ContentType = 'application/json'
Body = (ConvertTo-Json $releaseData -Compress)
}
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$result = Invoke-RestMethod @releaseParams
$uploadUri = $result | Select-Object -ExpandProperty upload_url
$uploadUri = $uploadUri -creplace '\{\?name,label\}'
$artifact = Get-Item $ArtifactPath
$uploadUri = $uploadUri + "?name=$($artifact.Name)"
$uploadFile = $artifact.FullName

$uploadParams = @{
Uri = $uploadUri
Method = 'POST'
Headers = @{
Authorization = $auth
}
ContentType = 'application/zip'
InFile = $uploadFile
}
$result = Invoke-RestMethod @uploadParams
}
if (($ENV:BHBuildSystem -eq 'VSTS' -and $env:BHCommitMessage -match '!deploy' -and $env:BHBranchName -eq "master") -or $global:ForceDeploy -eq $true) {
if ($null -eq (Get-Module PoshTwit -ListAvailable)) {
" Installing PoshTwit module..."
Install-Module PoshTwit -Scope CurrentUser
}
Import-Module PoshTwit -Verbose:$false
# Load the module, read the exported functions, update the psd1 FunctionsToExport
$commParsed = $env:BHCommitMessage | Select-String -Pattern '\sv\d+\.\d+\.\d+\s'
if ($commParsed) {
Expand Down Expand Up @@ -300,7 +384,7 @@ $deployScriptBlock = {
# Bump the module version
if ($versionToDeploy) {
try {
if ($env:BUILD_BUILDURI -like 'vstfs:*') {
if ($ENV:BHBuildSystem -eq 'VSTS' -and -not [String]::IsNullOrEmpty($env:NugetApiKey)) {
" Publishing version [$($versionToDeploy)] to PSGallery..."
Update-Metadata -Path (Join-Path $outputModVerDir "$($env:BHProjectName).psd1") -PropertyName ModuleVersion -Value $versionToDeploy
Publish-Module -Path $outputModVerDir -NuGetApiKey $env:NugetApiKey -Repository PSGallery
Expand All @@ -309,6 +393,46 @@ $deployScriptBlock = {
else {
" [SKIPPED] Deployment of version [$($versionToDeploy)] to PSGallery"
}
$commitId = git rev-parse --verify HEAD
if (-not [String]::IsNullOrEmpty($env:GitHubPAT)) {
" Creating Release ZIP..."
$zipPath = [System.IO.Path]::Combine($PSScriptRoot,"$($env:BHProjectName).zip")
if (Test-Path $zipPath) {
Remove-Item $zipPath -Force
}
Add-Type -Assembly System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::CreateFromDirectory($outputModDir,$zipPath)
" Publishing Release v$($versionToDeploy) @ commit Id [$($commitId)] to GitHub..."
$gitHubParams = @{
VersionNumber = $versionToDeploy.ToString()
CommitId = $commitId
ReleaseNotes = $env:BHCommitMessage
ArtifactPath = $zipPath
GitHubUsername = 'scrthq'
GitHubRepository = $env:BHProjectName
GitHubApiKey = $env:GitHubPAT
Draft = $true
}
Publish-GitHubRelease @gitHubParams
" Release creation successful!"
}
else {
" [SKIPPED] Publishing Release v$($versionToDeploy) @ commit Id [$($commitId)] to GitHub"
}
if ($ENV:BHBuildSystem -eq 'VSTS' -and -not [String]::IsNullOrEmpty($env:TwitterAccessSecret) -and -not [String]::IsNullOrEmpty($env:TwitterAccessToken) -and -not [String]::IsNullOrEmpty($env:TwitterConsumerKey) -and -not [String]::IsNullOrEmpty($env:TwitterConsumerSecret)) {
" Publishing tweet about new release..."
$manifest = Import-PowerShellDataFile -Path (Join-Path $outputModVerDir "$($env:BHProjectName).psd1")
$text = "$($env:BHProjectName) v$($versionToDeploy) is now available on the #PowerShell Gallery!"
$manifest.PrivateData.PSData.Tags | Foreach-Object {
$text += " #$($_)"
}
" Tweet text: $text"
Publish-Tweet -Tweet $text -ConsumerKey $env:TwitterConsumerKey -ConsumerSecret $env:TwitterConsumerSecret -AccessToken $env:TwitterAccessToken -AccessSecret $env:TwitterAccessSecret -Verbose
" Tweet successful!"
}
else {
" [SKIPPED] Twitter update of new release"
}
}
catch {
Write-Error $_ -ErrorAction Stop
Expand All @@ -325,6 +449,6 @@ $deployScriptBlock = {
}
}

Task Deploy -Depends Compile $deployScriptBlock -description 'Deploy module to PSGallery' -preaction {
Task Deploy -Depends Import $deployScriptBlock -description 'Deploy module to PSGallery' -preaction {
Import-Module -Name $outputModDir -Force -Verbose:$false
}

0 comments on commit 6073306

Please sign in to comment.