diff --git a/.gitignore b/.gitignore index 0a17cdcc..c017079e 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ FunctionBackups FunctionBackups/* BuildOutput/* *config.csv +PSGSuite.zip diff --git a/CHANGELOG.md b/CHANGELOG.md index cc6e4725..bf8f1d72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,66 +1,76 @@ # Changelog * [Changelog](#changelog) - * [2.20.0](#2200) - * [2.19.0](#2190) - * [2.18.1](#2181) - * [2.18.0](#2180) - * [2.17.2](#2172) - * [2.17.1](#2171) - * [2.17.0](#2170) - * [2.16.1](#2161) - * [2.16.0](#2160) - * [2.15.4](#2154) - * [2.15.3](#2153) - * [2.15.2](#2152) - * [2.15.1](#2151) - * [2.15.0](#2150) - * [2.14.1](#2141) - * [2.14.0](#2140) - * [2.13.2](#2132) - * [2.13.1](#2131) - * [2.13.0](#2130) - * [2.12.1](#2121) - * [2.12.0](#2120) - * [2.11.0](#2110) - * [2.10.2](#2102) - * [2.10.1](#2101) - * [2.10.0](#2100) - * [2.9.0](#290) - * [2.8.1](#281) - * [2.8.0](#280) - * [2.7.2](#272) - * [2.7.1](#271) - * [2.7.0](#270) - * [2.6.3](#263) - * [2.6.2](#262) - * [2.6.1](#261) - * [2.6.0](#260) - * [2.5.4](#254) - * [2.5.3](#253) - * [2.5.2](#252) - * [2.5.1](#251) - * [2.5.0](#250) - * [2.4.0](#240) - * [2.3.0](#230) - * [2.2.1](#221) - * [2.2.0](#220) - * [2.1.5](#215) - * [2.1.3 / 2.1.4](#213--214) - * [2.1.2](#212) - * [2.1.1](#211) - * [2.1.0](#210) - * [2.0.3](#203) - * [2.0.2](#202) - * [2.0.1](#201) - * [2.0.0](#200) - * [New Functionality](#new-functionality) - * [Breaking Changes in 2.0.0](#breaking-changes-in-200) - * [Gmail Delegation Management Removed](#gmail-delegation-management-removed) - * [Functions Removed](#functions-removed) - * [Functions Aliased](#functions-aliased) + * [2.20.1](#2201) + * [2.20.0](#2200) + * [2.19.0](#2190) + * [2.18.1](#2181) + * [2.18.0](#2180) + * [2.17.2](#2172) + * [2.17.1](#2171) + * [2.17.0](#2170) + * [2.16.1](#2161) + * [2.16.0](#2160) + * [2.15.4](#2154) + * [2.15.3](#2153) + * [2.15.2](#2152) + * [2.15.1](#2151) + * [2.15.0](#2150) + * [2.14.1](#2141) + * [2.14.0](#2140) + * [2.13.2](#2132) + * [2.13.1](#2131) + * [2.13.0](#2130) + * [2.12.1](#2121) + * [2.12.0](#2120) + * [2.11.0](#2110) + * [2.10.2](#2102) + * [2.10.1](#2101) + * [2.10.0](#2100) + * [2.9.0](#290) + * [2.8.1](#281) + * [2.8.0](#280) + * [2.7.2](#272) + * [2.7.1](#271) + * [2.7.0](#270) + * [2.6.3](#263) + * [2.6.2](#262) + * [2.6.1](#261) + * [2.6.0](#260) + * [2.5.4](#254) + * [2.5.3](#253) + * [2.5.2](#252) + * [2.5.1](#251) + * [2.5.0](#250) + * [2.4.0](#240) + * [2.3.0](#230) + * [2.2.1](#221) + * [2.2.0](#220) + * [2.1.5](#215) + * [2.1.3 / 2.1.4](#213--214) + * [2.1.2](#212) + * [2.1.1](#211) + * [2.1.0](#210) + * [2.0.3](#203) + * [2.0.2](#202) + * [2.0.1](#201) + * [2.0.0](#200) + * [New Functionality](#new-functionality) + * [Breaking Changes in 2.0.0](#breaking-changes-in-200) + * [Gmail Delegation Management Removed](#gmail-delegation-management-removed) + * [Functions Removed](#functions-removed) + * [Functions Aliased](#functions-aliased) *** + +## 2.20.1 + +* [Issue #121](https://github.com/scrthq/PSGSuite/issues/121) + * Added: `Update-GSGroupMember` to allow setting a group member's Role and/or DeliverySettings +* Miscellaneous + * Added: GitHub release automation to deploy task + * Added: Twitter update automation on new version release to deploy task + ## 2.20.0 * [Issue #115](https://github.com/scrthq/PSGSuite/issues/115) diff --git a/PSGSuite/PSGSuite.psd1 b/PSGSuite/PSGSuite.psd1 index fcbfe9de..fa40ce43 100644 --- a/PSGSuite/PSGSuite.psd1 +++ b/PSGSuite/PSGSuite.psd1 @@ -12,7 +12,7 @@ RootModule = 'PSGSuite.psm1' # Version number of this module. - ModuleVersion = '2.20.0' + ModuleVersion = '2.20.1' # ID used to uniquely identify this module GUID = '9d751152-e83e-40bb-a6db-4c329092aaec' @@ -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 = '' diff --git a/PSGSuite/Private/ListPrivate/Get-GSGroupMemberListPrivate.ps1 b/PSGSuite/Private/ListPrivate/Get-GSGroupMemberListPrivate.ps1 deleted file mode 100644 index df94ee09..00000000 --- a/PSGSuite/Private/ListPrivate/Get-GSGroupMemberListPrivate.ps1 +++ /dev/null @@ -1,66 +0,0 @@ -function Get-GSGroupMemberListPrivate { - [cmdletbinding()] - Param - ( - [parameter(Mandatory = $true,Position = 0,ValueFromPipeline = $true,ValueFromPipelineByPropertyName = $true)] - [Alias('GroupEmail','Group','Email')] - [String[]] - $Identity, - [parameter(Mandatory = $false)] - [ValidateSet("Owner","Manager","Member")] - [String[]] - $Roles, - [parameter(Mandatory = $false)] - [ValidateRange(1,200)] - [Alias('MaxResults')] - [Int] - $PageSize = "200" - ) - Begin { - $serviceParams = @{ - Scope = 'https://www.googleapis.com/auth/admin.directory.group' - ServiceType = 'Google.Apis.Admin.Directory.directory_v1.DirectoryService' - } - $service = New-GoogleService @serviceParams - } - Process { - foreach ($Id in $Identity) { - try { - if ($Id -notlike "*@*.*") { - $Id = "$($Id)@$($Script:PSGSuite.Domain)" - } - $request = $service.Members.List($Id) - if ($PageSize) { - $request.MaxResults = $PageSize - } - if ($Roles) { - Write-Verbose "Getting all members of group '$Id' in the following role(s): $($Roles -join ',')" - $request.Roles = "$($Roles -join ',')" - } - else { - Write-Verbose "Getting all members of group '$Id'" - } - [int]$i = 1 - do { - $result = $request.Execute() - if ($null -ne $result.MembersValue) { - $result.MembersValue | Add-Member -MemberType NoteProperty -Name 'Group' -Value $Id -PassThru | Add-Member -MemberType ScriptMethod -Name ToString -Value {$this.Email} -PassThru -Force - } - $request.PageToken = $result.NextPageToken - [int]$retrieved = ($i + $result.MembersValue.Count) - 1 - Write-Verbose "Retrieved $retrieved members..." - [int]$i = $i + $result.MembersValue.Count - } - until (!$result.NextPageToken) - } - catch { - if ($ErrorActionPreference -eq 'Stop') { - $PSCmdlet.ThrowTerminatingError($_) - } - else { - Write-Error $_ - } - } - } - } -} \ No newline at end of file diff --git a/PSGSuite/Public/Groups/Get-GSGroupMember.ps1 b/PSGSuite/Public/Groups/Get-GSGroupMember.ps1 index f09c443e..e9675fe0 100644 --- a/PSGSuite/Public/Groups/Get-GSGroupMember.ps1 +++ b/PSGSuite/Public/Groups/Get-GSGroupMember.ps1 @@ -44,13 +44,11 @@ function Get-GSGroupMember { $PageSize="200" ) Begin { - if ($PSCmdlet.ParameterSetName -eq 'Get') { - $serviceParams = @{ - Scope = 'https://www.googleapis.com/auth/admin.directory.group' - ServiceType = 'Google.Apis.Admin.Directory.directory_v1.DirectoryService' - } - $service = New-GoogleService @serviceParams + $serviceParams = @{ + Scope = 'https://www.googleapis.com/auth/admin.directory.group' + ServiceType = 'Google.Apis.Admin.Directory.directory_v1.DirectoryService' } + $service = New-GoogleService @serviceParams } Process { switch ($PSCmdlet.ParameterSetName) { @@ -80,7 +78,44 @@ function Get-GSGroupMember { } } List { - Get-GSGroupMemberListPrivate @PSBoundParameters + foreach ($Id in $Identity) { + try { + if ($Id -notlike "*@*.*") { + $Id = "$($Id)@$($Script:PSGSuite.Domain)" + } + $request = $service.Members.List($Id) + if ($PageSize) { + $request.MaxResults = $PageSize + } + if ($Roles) { + Write-Verbose "Getting all members of group '$Id' in the following role(s): $($Roles -join ',')" + $request.Roles = "$($Roles -join ',')" + } + else { + Write-Verbose "Getting all members of group '$Id'" + } + [int]$i = 1 + do { + $result = $request.Execute() + if ($null -ne $result.MembersValue) { + $result.MembersValue | Add-Member -MemberType NoteProperty -Name 'Group' -Value $Id -PassThru | Add-Member -MemberType ScriptMethod -Name ToString -Value {$this.Email} -PassThru -Force + } + $request.PageToken = $result.NextPageToken + [int]$retrieved = ($i + $result.MembersValue.Count) - 1 + Write-Verbose "Retrieved $retrieved members..." + [int]$i = $i + $result.MembersValue.Count + } + until (!$result.NextPageToken) + } + catch { + if ($ErrorActionPreference -eq 'Stop') { + $PSCmdlet.ThrowTerminatingError($_) + } + else { + Write-Error $_ + } + } + } } } } diff --git a/PSGSuite/Public/Groups/Update-GSGroupMember.ps1 b/PSGSuite/Public/Groups/Update-GSGroupMember.ps1 new file mode 100644 index 00000000..5c6754aa --- /dev/null +++ b/PSGSuite/Public/Groups/Update-GSGroupMember.ps1 @@ -0,0 +1,107 @@ +function Update-GSGroupMember { + <# + .SYNOPSIS + Updates a group member's role and/or delivery preference + + .DESCRIPTION + Updates a group member's role and/or delivery preference + + .PARAMETER GroupEmail + The email or GroupID of the group to update members of + + .PARAMETER Member + The member email or list of member emails that you would like to update + + .PARAMETER Role + The role that you would like to update the members to + + Acceptable values are: + * MEMBER + * MANAGER + * OWNER + + .PARAMETER DeliverySettings + Defines mail delivery preferences of member + + Acceptable values are: + * "ALL_MAIL": All messages, delivered as soon as they arrive. + * "DAILY": No more than one message a day. + * "DIGEST": Up to 25 messages bundled into a single message. + * "DISABLED": Remove subscription. + * "NONE": No messages. + + .EXAMPLE + Get-GSGroupMember myGroup | Update-GSGroupMember -DeliverySettings ALL_MAIL + + Updates the delivery preference for all members of group 'myGroup@domain.com' to 'ALL_MAIL' + #> + [cmdletbinding()] + Param + ( + [parameter(Mandatory = $true,Position = 0,ValueFromPipeline = $true,ValueFromPipelineByPropertyName = $true)] + [Alias('Group')] + [String] + $GroupEmail, + [parameter(Mandatory = $true,Position = 1,ValueFromPipelineByPropertyName = $true)] + [Alias("PrimaryEmail","UserKey","Mail","User","UserEmail","Email")] + [ValidateNotNullOrEmpty()] + [String[]] + $Member, + [parameter(Mandatory = $false)] + [ValidateSet("MEMBER","MANAGER","OWNER")] + [String] + $Role, + [parameter(Mandatory = $false)] + [ValidateSet("ALL_MAIL","DAILY","DIGEST","DISABLED","NONE")] + [String] + $DeliverySettings + ) + Begin { + $serviceParams = @{ + Scope = 'https://www.googleapis.com/auth/admin.directory.group' + ServiceType = 'Google.Apis.Admin.Directory.directory_v1.DirectoryService' + } + $service = New-GoogleService @serviceParams + } + Process { + try { + if ($GroupEmail -notlike "*@*.*") { + $GroupEmail = "$($GroupEmail)@$($Script:PSGSuite.Domain)" + } + #$groupObj = Get-GSGroup -Group $Identity -Verbose:$false + try { + foreach ($U in $Member) { + if ($U -notlike "*@*.*") { + $U = "$($U)@$($Script:PSGSuite.Domain)" + } + Write-Verbose "Updating member '$U' of group '$GroupEmail'" + $body = New-Object 'Google.Apis.Admin.Directory.directory_v1.Data.Member' + if ($PSBoundParameters.Keys -contains 'DeliverySettings') { + $body.DeliverySettings = $PSBoundParameters['DeliverySettings'] + } + if ($PSBoundParameters.Keys -contains 'Role') { + $body.Role = $PSBoundParameters['Role'] + } + $request = $service.Members.Patch($body,$GroupEmail,$U) + $request.Execute() | Add-Member -MemberType NoteProperty -Name 'Group' -Value $GroupEmail -PassThru + } + } + catch { + if ($ErrorActionPreference -eq 'Stop') { + $PSCmdlet.ThrowTerminatingError($_) + } + else { + Write-Error $_ + } + } + } + catch { + if ($ErrorActionPreference -eq 'Stop') { + $PSCmdlet.ThrowTerminatingError($_) + } + else { + Write-Error $_ + } + } + } +} diff --git a/README.md b/README.md index b138a57a..cfe403f7 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,14 @@ Update-GSSheetValue Export-GSSheet ### Most recent changes +#### 2.20.1 + +* [Issue #121](https://github.com/scrthq/PSGSuite/issues/121) + * Added: `Update-GSGroupMember` to allow setting a group member's Role and/or DeliverySettings +* Miscellaneous + * Added: GitHub release automation to deploy task + * Added: Twitter update automation on new version release to deploy task + #### 2.20.0 * [Issue #115](https://github.com/scrthq/PSGSuite/issues/115) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 29795844..660f6c25 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -126,6 +126,11 @@ phases: steps: - powershell: | + $Env:GitHubPAT = '$(GitHub.PAT)' $Env:NuGetApiKey = '$(NuGetApiKey)' + $Env:TwitterAccessSecret = '$(Twitter.AccessSecret)' + $Env:TwitterAccessToken = '$(Twitter.AccessToken)' + $Env:TwitterConsumerKey = '$(Twitter.ConsumerKey)' + $Env:TwitterConsumerSecret = '$(Twitter.ConsumerSecret)' . ./build.ps1 -Task Deploy displayName: Deploy to PowerShell Gallery diff --git a/build.ps1 b/build.ps1 index faea001c..2429f35a 100644 --- a/build.ps1 +++ b/build.ps1 @@ -9,7 +9,8 @@ param( [parameter(ParameterSetName = 'help')] [switch]$Help, - [switch]$UpdateModules + [switch]$UpdateModules, + [switch]$Force ) # build/init script borrowed from PoshBot x Brandon Olin @@ -98,7 +99,7 @@ if ($Help) { } else { if ( - $Task -eq 'Deploy' -and ( + $Task -eq 'Deploy' -and -not $Force -and ( $ENV:BUILD_BUILDURI -notlike 'vstfs:*' -or $env:BUILD_SOURCEBRANCH -like '*pull*' -or $env:BUILD_SOURCEVERSIONMESSAGE -notmatch '!deploy' -or @@ -111,21 +112,23 @@ else { " + Current build system is VSTS : $($env:BUILD_BUILDURI -like 'vstfs:*') [$env:BUILD_BUILDURI]`n" + " + Current branch is master : $($env:BUILD_SOURCEBRANCHNAME -eq 'master') [$env:BUILD_SOURCEBRANCHNAME]`n" + " + Source is not a pull request : $($env:BUILD_SOURCEBRANCH -notlike '*pull*') [$env:BUILD_SOURCEBRANCH]`n" + - " + Commit message matches '!deploy' : $($env:BUILD_SOURCEVERSIONMESSAGE -match '!deploy') [$env:BUILD_SOURCEVERSIONMESSAGE]`n" + " + Current PS major version is 5 : $($PSVersionTable.PSVersion.Major -eq 5) [$($PSVersionTable.PSVersion.ToString())]`n" + " + NuGet API key is not null : $($null -ne $env:NugetApiKey)`n" + + " + Build script is not Force ran : $($Force)`n" + + " + Commit message matches '!deploy' : $($env:BUILD_SOURCEVERSIONMESSAGE -match '!deploy') [$env:BUILD_SOURCEVERSIONMESSAGE]`n" + "Skipping psake for this job!" | Write-Host -ForegroundColor Yellow exit 0 } else { if ($Task -eq 'Deploy') { "Task is 'Deploy' and conditions are correct for deployment:`n" + - " + Current build system is VSTS : $($env:BUILD_BUILDURI -like 'vstfs:*') [$env:BUILD_BUILDURI]`n" + - " + Current branch is master : $($env:BUILD_SOURCEBRANCHNAME -eq 'master') [$env:BUILD_SOURCEBRANCHNAME]`n" + - " + Source is not a pull request : $($env:BUILD_SOURCEBRANCH -notlike '*pull*') [$env:BUILD_SOURCEBRANCH]`n" + - " + Commit message matches '!deploy' : $($env:BUILD_SOURCEVERSIONMESSAGE -match '!deploy') [$env:BUILD_SOURCEVERSIONMESSAGE]`n" + - " + Current PS major version is 5 : $($PSVersionTable.PSVersion.Major -eq 5) [$($PSVersionTable.PSVersion.ToString())]`n" + - " + NuGet API key is not null : $($null -ne $env:NugetApiKey)`n" | Write-Host -ForegroundColor Green + " + Build script is Force ran : $($Force)`n" + + " + Current build system is VSTS : $($env:BUILD_BUILDURI -like 'vstfs:*') [$env:BUILD_BUILDURI]`n" + + " + Current branch is master : $($env:BUILD_SOURCEBRANCHNAME -eq 'master') [$env:BUILD_SOURCEBRANCHNAME]`n" + + " + Source is not a pull request : $($env:BUILD_SOURCEBRANCH -notlike '*pull*') [$env:BUILD_SOURCEBRANCH]`n" + + " + Current PS major version is 5 : $($PSVersionTable.PSVersion.Major -eq 5) [$($PSVersionTable.PSVersion.ToString())]`n" + + " + NuGet API key is not null : $($null -ne $env:NugetApiKey)`n" + + " + Commit message matches '!deploy' : $($env:BUILD_SOURCEVERSIONMESSAGE -match '!deploy') [$env:BUILD_SOURCEVERSIONMESSAGE]"| Write-Host -ForegroundColor Green } 'BuildHelpers','psake' | Resolve-Module @update -Verbose Set-BuildEnvironment -Force @@ -142,6 +145,12 @@ else { else { $global:ExcludeTag = $null } + if ($Force) { + $global:ForceDeploy = $true + } + else { + $global:ForceDeploy = $false + } Invoke-psake @psakeParams @verbose if ($Task -contains 'Import' -and $psake.build_success) { Import-Module ([System.IO.Path]::Combine($env:BHBuildOutput,$env:BHProjectName)) -Verbose:$false diff --git a/psake.ps1 b/psake.ps1 index 70f95b24..3d3bf3e6 100644 --- a/psake.ps1 +++ b/psake.ps1 @@ -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) { @@ -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\.. } @@ -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") @@ -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 = { - if ($ENV:BHBuildSystem -eq 'VSTS' -and $env:BHCommitMessage -match '!deploy' -and $env:BHBranchName -eq "master") { + 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) { @@ -300,10 +384,55 @@ $deployScriptBlock = { # Bump the module version if ($versionToDeploy) { try { - " 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 - " Deployment successful!" + 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 + " Deployment successful!" + } + 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 = $false + } + 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 #PSGallery! #PowerShell" + $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 + " Tweet successful!" + } + else { + " [SKIPPED] Twitter update of new release" + } } catch { Write-Error $_ -ErrorAction Stop @@ -320,4 +449,6 @@ $deployScriptBlock = { } } -Task Deploy -Depends Compile $deployScriptBlock -description 'Deploy module to PSGallery' +Task Deploy -Depends Import $deployScriptBlock -description 'Deploy module to PSGallery' -preaction { + Import-Module -Name $outputModDir -Force -Verbose:$false +}