Skip to content

Commit 1a07642

Browse files
committed
20220128A
1 parent dc46a13 commit 1a07642

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Import-Module -Name 'hugoalh.GitHubActionsToolkit' -Scope Local
5858
- `Remove-GHActionsProblemMatcher`
5959
- `Set-GHActionsOutput`
6060
- `Set-GHActionsState`
61+
- `Test-GHActionsEnvironment`
6162
- `Write-GHActionsAnnotation`
6263
- `Write-GHActionsDebug`
6364
- `Write-GHActionsError`

hugoalh.GitHubActionsToolkit/hugoalh.GitHubActionsToolkit.psd1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
'Remove-GHActionsProblemMatcher',
7979
'Set-GHActionsOutput',
8080
'Set-GHActionsState',
81+
'Test-GHActionsEnvironment',
8182
'Write-GHActionsAnnotation',
8283
'Write-GHActionsDebug',
8384
'Write-GHActionsError',
@@ -161,5 +162,5 @@
161162
# HelpInfoURI = ''
162163

163164
# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
164-
# DefaultCommandPrefix = ''
165+
# DefaultCommandPrefix = 'GHActions'
165166
}

hugoalh.GitHubActionsToolkit/hugoalh.GitHubActionsToolkit.psm1

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,59 @@ function Set-GHActionsState {
624624
Set-Alias -Name 'Save-GHActionsState' -Value 'Set-GHActionsState' -Option ReadOnly -Scope 'Local'
625625
<#
626626
.SYNOPSIS
627+
GitHub Actions - Test Environment
628+
.DESCRIPTION
629+
Test the current process is executing inside the GitHub Actions environment.
630+
.PARAMETER Force
631+
Whether the requirement is force. If forced and not fulfill, will throw an error.
632+
#>
633+
function Test-GHActionsEnvironment {
634+
[CmdletBinding()][OutputType([bool])]
635+
param (
636+
[Alias('Forced', 'Require', 'Required')][switch]$Force
637+
)
638+
if (
639+
($env:CI -ne 'true') -or
640+
($env:GITHUB_ACTIONS -ne 'true') -or
641+
($null -eq $env:GITHUB_ACTION_PATH) -or
642+
($null -eq $env:GITHUB_ACTION_REPOSITORY) -or
643+
($null -eq $env:GITHUB_ACTION) -or
644+
($null -eq $env:GITHUB_ACTOR) -or
645+
($null -eq $env:GITHUB_API_URL) -or
646+
($null -eq $env:GITHUB_ENV) -or
647+
($null -eq $env:GITHUB_EVENT_NAME) -or
648+
($null -eq $env:GITHUB_EVENT_PATH) -or
649+
($null -eq $env:GITHUB_GRAPHQL_URL) -or
650+
($null -eq $env:GITHUB_JOB) -or
651+
($null -eq $env:GITHUB_PATH) -or
652+
($null -eq $env:GITHUB_REF_NAME) -or
653+
($null -eq $env:GITHUB_REF_PROTECTED) -or
654+
($null -eq $env:GITHUB_REF_TYPE) -or
655+
($null -eq $env:GITHUB_REPOSITORY_OWNER) -or
656+
($null -eq $env:GITHUB_REPOSITORY) -or
657+
($null -eq $env:GITHUB_RETENTION_DAYS) -or
658+
($null -eq $env:GITHUB_RUN_ATTEMPT) -or
659+
($null -eq $env:GITHUB_RUN_ID) -or
660+
($null -eq $env:GITHUB_RUN_NUMBER) -or
661+
($null -eq $env:GITHUB_SERVER_URL) -or
662+
($null -eq $env:GITHUB_SHA) -or
663+
($null -eq $env:GITHUB_WORKFLOW) -or
664+
($null -eq $env:GITHUB_WORKSPACE) -or
665+
($null -eq $env:RUNNER_ARCH) -or
666+
($null -eq $env:RUNNER_NAME) -or
667+
($null -eq $env:RUNNER_OS) -or
668+
($null -eq $env:RUNNER_TEMP) -or
669+
($null -eq $env:RUNNER_TOOL_CACHE)
670+
) {
671+
if ($Force) {
672+
throw 'This process require to execute inside the GitHub Actions environment.'
673+
}
674+
return $false
675+
}
676+
return $true
677+
}
678+
<#
679+
.SYNOPSIS
627680
GitHub Actions - Write Annotation
628681
.DESCRIPTION
629682
Prints an annotation message to the log.
@@ -660,9 +713,18 @@ function Write-GHActionsAnnotation {
660713
)
661714
[string]$TypeRaw = ""
662715
switch ($Type.GetHashCode()) {
663-
0 { $TypeRaw = 'notice'; break }
664-
1 { $TypeRaw = 'warning'; break }
665-
2 { $TypeRaw = 'error'; break }
716+
0 {
717+
$TypeRaw = 'notice'
718+
break
719+
}
720+
1 {
721+
$TypeRaw = 'warning'
722+
break
723+
}
724+
2 {
725+
$TypeRaw = 'error'
726+
break
727+
}
666728
}
667729
[hashtable]$Property = @{}
668730
if ($File.Length -gt 0) {
@@ -849,6 +911,7 @@ Export-ModuleMember -Function @(
849911
'Remove-GHActionsProblemMatcher',
850912
'Set-GHActionsOutput',
851913
'Set-GHActionsState',
914+
'Test-GHActionsEnvironment',
852915
'Write-GHActionsAnnotation',
853916
'Write-GHActionsDebug',
854917
'Write-GHActionsError',

0 commit comments

Comments
 (0)