diff --git a/BuildHelpers/Public/Get-BuildEnvironment.ps1 b/BuildHelpers/Public/Get-BuildEnvironment.ps1 index c7ad119..770f642 100644 --- a/BuildHelpers/Public/Get-BuildEnvironment.ps1 +++ b/BuildHelpers/Public/Get-BuildEnvironment.ps1 @@ -15,6 +15,7 @@ function Get-BuildEnvironment { CommitMessage via Get-BuildVariable CommitHash via Get-BuildVariable BuildNumber via Get-BuildVariable + IsPullRequest via Get-BuildVariable ProjectName via Get-ProjectName PSModuleManifest via Get-PSModuleManifest ModulePath via Split-Path on PSModuleManifest @@ -107,6 +108,11 @@ function Get-BuildEnvironment { PSModuleManifest = ${Build.ManifestPath} ModulePath = ${Build.ModulePath} } + if (${Build.Vars}.IsPullRequest) { + #This will ensure IsPullRequest is inserted into the ordered table after the BuildNumber property + $index = $($BuildHelpersVariables.keys).IndexOf("BuildNumber") + $BuildHelpersVariables.Insert($index+1,"IsPullRequest",${Build.Vars}.IsPullRequest) + } foreach($VarName in $BuildHelpersVariables.keys){ $BuildOutput = $BuildOutput -replace "\`$$VarName", $BuildHelpersVariables[$VarName] } diff --git a/BuildHelpers/Public/Get-BuildVariable.ps1 b/BuildHelpers/Public/Get-BuildVariable.ps1 index 3ffb8c6..bbf862f 100644 --- a/BuildHelpers/Public/Get-BuildVariable.ps1 +++ b/BuildHelpers/Public/Get-BuildVariable.ps1 @@ -35,6 +35,7 @@ function Get-BuildVariable { BranchName: git branch for this build CommitMessage: git commit message for this build BuildNumber: Build number provided by the CI system + IsPullRequest: "True" if CI says the current build is the result of a PR, otherwise this value will not be present .PARAMETER Path Path to project root. Defaults to the current working path @@ -263,7 +264,20 @@ function Get-BuildVariable { $BuildNumber = 0 } - [pscustomobject]@{ + $PullRequest = switch ($BuildSystem) + { + 'AppVeyor' {if ($Environment.APPVEYOR_PULL_REQUEST_NUMBER) {"True"}; break} + 'GitLab CI' {if ($Environment.CI_MERGE_REQUEST_ID) {"True"}; break} + 'Azure Pipelines' {if ($ENV:BUILD_REASON -eq "PullRequest") {"True"}; break} + 'Bamboo' {if ($Environment.bamboo.repository.pr.key) {"True"}; break} + 'GoCD' {if ($Environment.PR_TITLE) {"True"}; break} + 'Travis CI' {if ($ENV:TRAVIS_EVENT_TYPE -eq "pull_request") {"True"}; break} + 'GitHub Actions' {if ($ENV:GITHUB_EVENT_NAME -eq "pull_request") {"True"}; break} + #'Jenkins' {if ($Environment.CHANGE_ID) {"True"}} ???? is this correct? + #'Teamcity' {if ???????) {"True"}} who even knows + } + + $ReturnHash = @{ BuildSystem = $BuildSystem ProjectPath = $BuildRoot BranchName = $BuildBranch @@ -271,4 +285,10 @@ function Get-BuildVariable { CommitHash = $CommitHash BuildNumber = $BuildNumber } + if ($PullRequest) { + $ReturnHash['IsPullRequest'] = $PullRequest + } + + [PSCustomObject]$ReturnHash + } diff --git a/BuildHelpers/Scripts/Set-BuildVariable.ps1 b/BuildHelpers/Scripts/Set-BuildVariable.ps1 index 387a7b1..7bd962c 100644 --- a/BuildHelpers/Scripts/Set-BuildVariable.ps1 +++ b/BuildHelpers/Scripts/Set-BuildVariable.ps1 @@ -12,7 +12,9 @@ BHProjectPath via Get-BuildVariable BHBranchName via Get-BuildVariable BHCommitMessage via Get-BuildVariable + BHCommitHash via Get-BuildVariable BHBuildNumber via Get-BuildVariable + BHIsPullRequest via Get-BuildVariable BHProjectName via Get-ProjectName BHPSModuleManifest via Get-PSModuleManifest BHModulePath via Split-Path on BHPSModuleManifest @@ -111,11 +113,15 @@ $BuildHelpersVariables = @{ ProjectPath = ${Build.Vars}.ProjectPath BranchName = ${Build.Vars}.BranchName CommitMessage = ${Build.Vars}.CommitMessage + CommitHash = ${Build.Vars}.CommitHash BuildNumber = ${Build.Vars}.BuildNumber ProjectName = ${Build.ProjectName} PSModuleManifest = ${Build.ManifestPath} ModulePath = $(Split-Path -Path ${Build.ManifestPath} -Parent) } +if (${Build.Vars}.IsPullRequest) { + $BuildHelpersVariables.IsPullRequest = [bool]${Build.Vars}.IsPullRequest +} foreach ($VarName in $BuildHelpersVariables.Keys) { Set-Variable -Scope $Scope -Name ('{0}{1}' -f $VariableNamePrefix,$VarName) -Value $BuildHelpersVariables[$VarName] }