Skip to content

Commit 8d84932

Browse files
committed
20220624A
1 parent 42fd985 commit 8d84932

31 files changed

+1965
-78
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,23 @@ Import-Module -Name 'hugoalh.GitHubActionsToolkit' -Prefix 'GitHubActions' -Scop
6161
- `Enable-GitHubActionsProcessingCommands`
6262
- `Enter-GitHubActionsLogGroup`
6363
- `Exit-GitHubActionsLogGroup`
64+
- `Export-GitHubActionsArtifact`
6465
- `Get-GitHubActionsInput`
6566
- `Get-GitHubActionsIsDebug`
6667
- `Get-GitHubActionsOpenIdConnectToken` 🧪
6768
- `Get-GitHubActionsState`
6869
- `Get-GitHubActionsStepSummary`
6970
- `Get-GitHubActionsWebhookEventPayload`
7071
- `Get-GitHubActionsWorkflowRunUri`
72+
- `Import-GitHubActionsArtifact`
7173
- `Remove-GitHubActionsProblemMatcher`
7274
- `Remove-GitHubActionsStepSummary`
7375
- `Set-GitHubActionsEnvironmentVariable`
7476
- `Set-GitHubActionsOutput`
7577
- `Set-GitHubActionsState`
7678
- `Set-GitHubActionsStepSummary`
7779
- `Test-GitHubActionsEnvironment`
80+
- `Test-GitHubActionsNodeJsEnvironment`
7881
- `Write-GitHubActionsAnnotation`
7982
- `Write-GitHubActionsCommand`
8083
- `Write-GitHubActionsDebug`

hugoalh.GitHubActionsToolkit/hugoalh.GitHubActionsToolkit.psd1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,23 @@
7676
'Enable-ProcessingCommands',
7777
'Enter-LogGroup',
7878
'Exit-LogGroup',
79+
'Export-Artifact',
7980
'Get-Input',
8081
'Get-IsDebug',
8182
'Get-OpenIdConnectToken',
8283
'Get-State',
8384
'Get-StepSummary',
8485
'Get-WebhookEventPayload',
8586
'Get-WorkflowRunUri',
87+
'Import-Artifact',
8688
'Remove-ProblemMatcher',
8789
'Remove-StepSummary',
8890
'Set-EnvironmentVariable',
8991
'Set-Output',
9092
'Set-State',
9193
'Set-StepSummary',
9294
'Test-Environment',
95+
'Test-NodeJsEnvironment',
9396
'Write-Annotation',
9497
'Write-Command',
9598
'Write-Debug',

hugoalh.GitHubActionsToolkit/hugoalh.GitHubActionsToolkit.psm1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
[String]$ModuleRoot = Join-Path -Path $PSScriptRoot -ChildPath 'module'
44
[String[]]$ModulesNames = @(
55
'command-base',
6+
'artifact',
67
'command-control',
78
'environment-variable',
89
'log',
10+
'nodejs-test',
911
'open-id-connect',
1012
'parameter',
1113
'problem-matcher',
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
#Requires -PSEdition Core
2+
#Requires -Version 7.2
3+
Import-Module -Name @(
4+
(Join-Path -Path $PSScriptRoot -ChildPath 'nodejs-invoke.psm1'),
5+
(Join-Path -Path $PSScriptRoot -ChildPath 'utility.psm1')
6+
) -Prefix 'GitHubActions' -Scope 'Local'
7+
<#
8+
.SYNOPSIS
9+
GitHub Actions - Export Artifact
10+
.DESCRIPTION
11+
Export artifact to persist data after a job has completed, and share that data with another job in the same workflow.
12+
.PARAMETER Name
13+
Artifact name.
14+
.PARAMETER Path
15+
Absolute and/or relative path to the file that need to export as artifact.
16+
.PARAMETER LiteralPath
17+
Absolute and/or relative literal path to the file that need to export as artifact.
18+
.PARAMETER BaseRoot
19+
A (literal) path that denote the base root directory of the files for control files structure.
20+
.PARAMETER IgnoreIssuePaths
21+
Whether the export should continue in the event of files fail to export; If set to `$False` and issue is encountered, export will stop and queued files will not export; The partial artifact availables which include files up until the issue; If set to `$True` and issue is encountered, the issue file will ignore and skip, and queued files will still export; The partial artifact availables which include everything but exclude issue files.
22+
.PARAMETER RetentionTime
23+
Duration of artifact expire by days.
24+
.OUTPUTS
25+
[PSCustomObject] Exported artifact's metadata.
26+
#>
27+
Function Export-Artifact {
28+
[CmdletBinding(DefaultParameterSetName = 'Path', HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_export-githubactionsartifact#Export-GitHubActionsArtifact')]
29+
[OutputType([PSCustomObject])]
30+
Param (
31+
[Parameter(Mandatory = $True, Position = 0)][ValidateScript({
32+
Return (Test-ArtifactName -InputObject $_)
33+
}, ErrorMessage = '`{0}` is not a valid GitHub Actions artifact name!')][String]$Name,
34+
[Parameter(Mandatory = $True, ParameterSetName = 'Path', Position = 1, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)][SupportsWildcards()][Alias('File', 'Files', 'Paths')][String[]]$Path,
35+
[Parameter(Mandatory = $True, ParameterSetName = 'LiteralPath', ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)][Alias('LiteralFile', 'LiteralFiles', 'LiteralPaths', 'LP', 'PSPath', 'PSPaths')][String[]]$LiteralPath,
36+
[Alias('Root')][String]$BaseRoot = $Env:GITHUB_WORKSPACE,
37+
[Alias('ContinueOnError', 'ContinueOnIssue', 'ContinueOnIssues', 'IgnoreIssuePath')][Switch]$IgnoreIssuePaths,
38+
[Alias('RetentionDay')][ValidateRange(1, 90)][Byte]$RetentionTime
39+
)
40+
Begin {
41+
If (
42+
![System.IO.Path]::IsPathRooted($BaseRoot) -or
43+
!(Test-Path -LiteralPath $BaseRoot -PathType 'Container')
44+
) {
45+
Return (Write-Error -Message "``$BaseRoot`` is not an exist and valid GitHub Actions artifact base root!" -Category 'SyntaxError')
46+
Break# This is the best way to early terminate this function without terminate caller/invoker process.
47+
}
48+
[String]$BaseRootRegularExpression = "^$([RegEx]::Escape((Resolve-Path -LiteralPath $BaseRoot)))"
49+
[String[]]$PathsValid = @()
50+
[String[]]$PathsInvalid = @()
51+
}
52+
Process {
53+
Switch ($PSCmdlet.ParameterSetName) {
54+
'Path' {
55+
ForEach ($ItemPath In $Path) {
56+
Try {
57+
ForEach ($ItemResolve In [String[]](Resolve-Path -Path ([System.IO.Path]::IsPathRooted($ItemPath) ? $ItemPath : (Join-Path -Path $BaseRoot -ChildPath $ItemPath)))) {
58+
If (!(Test-Path -LiteralPath $ItemResolve -PathType 'Leaf')) {
59+
Continue
60+
}
61+
If (
62+
$ItemResolve -inotmatch $BaseRootRegularExpression -or
63+
!(Test-ArtifactPath -InputObject $ItemResolve)
64+
) {
65+
$PathsInvalid += $ItemResolve
66+
Continue
67+
}
68+
$PathsValid += $ItemResolve
69+
}
70+
} Catch {
71+
$PathsInvalid += $ItemPath
72+
}
73+
}
74+
}
75+
'LiteralPath' {
76+
ForEach ($ItemLiteralPath In $LiteralPath) {
77+
Try {
78+
ForEach ($ItemResolve In [String[]](Resolve-Path -LiteralPath ([System.IO.Path]::IsPathRooted($ItemLiteralPath) ? $ItemLiteralPath : (Join-Path -Path $BaseRoot -ChildPath $ItemLiteralPath)))) {
79+
If (!(Test-Path -LiteralPath $ItemResolve -PathType 'Leaf')) {
80+
Continue
81+
}
82+
If (
83+
$ItemResolve -inotmatch $BaseRootRegularExpression -or
84+
!(Test-ArtifactPath -InputObject $ItemResolve)
85+
) {
86+
$PathsInvalid += $ItemResolve
87+
Continue
88+
}
89+
$PathsValid += $ItemResolve
90+
}
91+
} Catch {
92+
$PathsInvalid += $ItemLiteralPath
93+
}
94+
}
95+
}
96+
}
97+
}
98+
End {
99+
If (!(Test-GitHubActionsEnvironment -Artifact)) {
100+
Return (Write-Error -Message 'Unable to get GitHub Actions artifact resources!' -Category 'ResourceUnavailable')
101+
}
102+
If ($PathsInvalid.Count -igt 0 -and !$IgnoreIssuePaths.IsPresent) {
103+
Return ($PathsInvalid | ForEach-Object -Process {
104+
Return (Write-Error -Message "``$_`` is not an exist and valid file path!" -Category 'SyntaxError')
105+
})
106+
}
107+
If ($PathsValid.Count -ieq 0) {
108+
Return (Write-Error -Message 'No valid path is defined!' -Category 'NotSpecified')
109+
}
110+
[Hashtable]$InputObject = @{
111+
Name = $Name
112+
Path = ($PathsValid | ForEach-Object -Process {
113+
Return ($_ -ireplace $BaseRootRegularExpression, '' -ireplace '\\', '/')
114+
})
115+
BaseRoot = $BaseRoot
116+
IgnoreIssuePaths = $IgnoreIssuePaths.IsPresent
117+
}
118+
If ($Null -ine $RetentionTime) {
119+
$InputObject.RetentionTIme = $RetentionTime
120+
}
121+
$ResultRaw = Invoke-GitHubActionsNodeJsWrapper -Path 'artifact\upload.js' -InputObject ([PSCustomObject]$InputObject | ConvertTo-Json -Depth 100 -Compress)
122+
If ($ResultRaw -ieq $False) {
123+
Return
124+
}
125+
[Hashtable]$Result = ($ResultRaw | ConvertFrom-Json -AsHashtable -Depth 100)
126+
$Result.FailedItem += $PathsInvalid
127+
$Result.FailedItems += $PathsInvalid
128+
Return [PSCustomObject]$Result
129+
}
130+
}
131+
<#
132+
.SYNOPSIS
133+
GitHub Actions - Import Artifact
134+
.DESCRIPTION
135+
Import artifact that shared data from previous job in the same workflow.
136+
.PARAMETER Name
137+
Artifact name.
138+
.PARAMETER Destination
139+
Artifact destination.
140+
.PARAMETER CreateSubfolder
141+
Create a subfolder with artifact name and put data into here.
142+
.PARAMETER All
143+
Import all artifacts that shared data from previous job in the same workflow.
144+
.OUTPUTS
145+
[PSCustomObject[]] Imported artifacts' metadata.
146+
#>
147+
Function Import-Artifact {
148+
[CmdletBinding(DefaultParameterSetName = 'Select', HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_import-githubactionsartifact#Import-GitHubActionsArtifact')]
149+
[OutputType([PSCustomObject[]])]
150+
Param (
151+
[Parameter(Mandatory = $True, ParameterSetName = 'Select', Position = 0, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)][String[]]$Name,
152+
[String]$Destination = $Env:GITHUB_WORKSPACE,
153+
[Parameter(ParameterSetName = 'Select')][Switch]$CreateSubfolder,
154+
[Parameter(Mandatory = $True, ParameterSetName = 'All')][Switch]$All
155+
)
156+
Begin {
157+
If (!(Test-GitHubActionsEnvironment -Artifact)) {
158+
Return (Write-Error -Message 'Unable to get GitHub Actions artifact resources!' -Category 'ResourceUnavailable')
159+
Break# This is the best way to early terminate this function without terminate caller/invoker process.
160+
}
161+
[PSCustomObject[]]$OutputObject = @()
162+
}
163+
Process {
164+
Switch ($PSCmdlet.ParameterSetName) {
165+
'All' {
166+
$ResultRaw = Invoke-GitHubActionsNodeJsWrapper -Path 'artifact\download-all.js' -InputObject ([PSCustomObject]@{
167+
Destination = $Destination
168+
} | ConvertTo-Json -Depth 100 -Compress)
169+
If ($ResultRaw -ieq $False) {
170+
Continue
171+
}
172+
$OutputObject = ($ResultRaw | ConvertFrom-Json -Depth 100)
173+
}
174+
'Select' {
175+
ForEach ($Item In $Name) {
176+
$ResultRaw = Invoke-GitHubActionsNodeJsWrapper -Path 'artifact\download.js' -InputObject ([PSCustomObject]@{
177+
Name = $Item
178+
Destination = $Destination
179+
CreateSubfolder = $CreateSubfolder.IsPresent
180+
} | ConvertTo-Json -Depth 100 -Compress)
181+
If ($ResultRaw -ieq $False) {
182+
Continue
183+
}
184+
$OutputObject += ($ResultRaw | ConvertFrom-Json -Depth 100)
185+
}
186+
}
187+
}
188+
}
189+
End {
190+
Return $OutputObject
191+
}
192+
}
193+
<#
194+
.SYNOPSIS
195+
GitHub Actions (Internal) - Test Artifact Name
196+
.DESCRIPTION
197+
Test artifact name whether is valid.
198+
.PARAMETER InputObject
199+
Artifact name that need to test.
200+
.OUTPUTS
201+
[Boolean] Test result.
202+
#>
203+
Function Test-ArtifactName {
204+
[CmdletBinding()]
205+
[OutputType([Boolean])]
206+
Param (
207+
[Parameter(Mandatory = $True, Position = 0)][Alias('Input', 'Object')][String]$InputObject
208+
)
209+
Return ((Test-ArtifactPath -InputObject $InputObject) -and $InputObject -imatch '^[^\\/]+$')
210+
}
211+
<#
212+
.SYNOPSIS
213+
GitHub Actions (Internal) - Test Artifact Path
214+
.DESCRIPTION
215+
Test artifact path whether is valid.
216+
.PARAMETER InputObject
217+
Artifact path that need to test.
218+
.OUTPUTS
219+
[Boolean] Test result.
220+
#>
221+
Function Test-ArtifactPath {
222+
[CmdletBinding()]
223+
[OutputType([Boolean])]
224+
Param (
225+
[Parameter(Mandatory = $True, Position = 0)][Alias('Input', 'Object')][String]$InputObject
226+
)
227+
Return ($InputObject -imatch '^[^":<>|*?\n\r\t]+$')
228+
}
229+
Export-ModuleMember -Function @(
230+
'Export-Artifact',
231+
'Import-Artifact'
232+
)

hugoalh.GitHubActionsToolkit/module/command-base.psm1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Format command parameter characters that can cause issues.
88
.PARAMETER InputObject
99
String that need to format command parameter characters.
1010
.OUTPUTS
11-
String
11+
[String] A string that formatted command parameter characters.
1212
#>
1313
Function Format-CommandParameter {
1414
[CmdletBinding()]
@@ -27,7 +27,7 @@ Format command value characters that can cause issues.
2727
.PARAMETER InputObject
2828
String that need to format command value characters.
2929
.OUTPUTS
30-
String
30+
[String] A string that formatted command value characters.
3131
#>
3232
Function Format-CommandValue {
3333
[CmdletBinding()]
@@ -51,7 +51,7 @@ Command value.
5151
.PARAMETER Parameter
5252
Command parameter.
5353
.OUTPUTS
54-
Void
54+
[Void]
5555
#>
5656
Function Write-Command {
5757
[CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_write-githubactionscommand#Write-GitHubActionsCommand')]

hugoalh.GitHubActionsToolkit/module/command-control.psm1

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ GitHub Actions - Disable Echoing Commands
2626
.DESCRIPTION
2727
Disable echoing most of the commands, the log will not show the command itself; Secret `ACTIONS_STEP_DEBUG` will ignore this setting; When processing a command, it will still echo if there has any issues.
2828
.OUTPUTS
29-
Void
29+
[Void]
3030
#>
3131
Function Disable-EchoingCommands {
3232
[CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_disable-githubactionsechoingcommands#Disable-GitHubActionsEchoingCommands')]
@@ -57,7 +57,7 @@ Disable processing any commands, to allow log anything without accidentally exec
5757
.PARAMETER EndToken
5858
An end token for function `Enable-GitHubActionsProcessingCommands`.
5959
.OUTPUTS
60-
String
60+
[String] An end token for function `Enable-GitHubActionsProcessingCommands`.
6161
#>
6262
Function Disable-ProcessingCommands {
6363
[CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_disable-githubactionsprocessingcommands#Disable-GitHubActionsProcessingCommands')]
@@ -91,7 +91,7 @@ GitHub Actions - Enable Echoing Commands
9191
.DESCRIPTION
9292
Enable echoing most of the commands, the log will show the command itself; Secret `ACTIONS_STEP_DEBUG` will ignore this setting.
9393
.OUTPUTS
94-
Void
94+
[Void]
9595
#>
9696
Function Enable-EchoingCommands {
9797
[CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_enable-githubactionsechoingcommands#Enable-GitHubActionsEchoingCommands')]
@@ -122,7 +122,7 @@ Enable processing any commands, to allow execute any commands.
122122
.PARAMETER EndToken
123123
An end token from function `Disable-GitHubActionsProcessingCommands`.
124124
.OUTPUTS
125-
Void
125+
[Void]
126126
#>
127127
Function Enable-ProcessingCommands {
128128
[CmdletBinding(HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_enable-githubactionsprocessingcommands#Enable-GitHubActionsProcessingCommands')]
@@ -157,7 +157,7 @@ Test processing commands end token whether is valid.
157157
.PARAMETER InputObject
158158
Processing commands end token that need to test.
159159
.OUTPUTS
160-
Boolean
160+
[Boolean] Test result.
161161
#>
162162
Function Test-ProcessingCommandsEndToken {
163163
[CmdletBinding()]

0 commit comments

Comments
 (0)