@@ -624,6 +624,59 @@ function Set-GHActionsState {
624
624
Set-Alias - Name ' Save-GHActionsState' - Value ' Set-GHActionsState' - Option ReadOnly - Scope ' Local'
625
625
<#
626
626
. 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
627
680
GitHub Actions - Write Annotation
628
681
. DESCRIPTION
629
682
Prints an annotation message to the log.
@@ -660,9 +713,18 @@ function Write-GHActionsAnnotation {
660
713
)
661
714
[string ]$TypeRaw = " "
662
715
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
+ }
666
728
}
667
729
[hashtable ]$Property = @ {}
668
730
if ($File.Length -gt 0 ) {
@@ -849,6 +911,7 @@ Export-ModuleMember -Function @(
849
911
' Remove-GHActionsProblemMatcher' ,
850
912
' Set-GHActionsOutput' ,
851
913
' Set-GHActionsState' ,
914
+ ' Test-GHActionsEnvironment' ,
852
915
' Write-GHActionsAnnotation' ,
853
916
' Write-GHActionsDebug' ,
854
917
' Write-GHActionsError' ,
0 commit comments