Skip to content

Commit 09e2714

Browse files
committed
20221011A
1 parent 825b5a3 commit 09e2714

File tree

21 files changed

+170
-196
lines changed

21 files changed

+170
-196
lines changed

hugoalh.GitHubActionsToolkit/module/artifact.psm1

Lines changed: 24 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,32 @@ Import-Module -Name (
1111
.SYNOPSIS
1212
GitHub Actions - Export Artifact
1313
.DESCRIPTION
14-
Export artifact to persist data and/or share with another job in the same workflow.
14+
Export artifact to persist data and/or share with future job in the same workflow.
1515
.PARAMETER Name
16-
Artifact name.
16+
Name of the artifact.
1717
.PARAMETER Path
18-
Absolute and/or relative path to the file that need to export as artifact.
18+
Paths to the files that need to export as artifact.
1919
.PARAMETER LiteralPath
20-
Absolute and/or relative literal path to the file that need to export as artifact.
20+
Literal paths to the files that need to export as artifact.
2121
.PARAMETER BaseRoot
22-
A (literal) path that denote the base root directory of the files for control files structure.
22+
Absolute literal path of the base root directory of the files for control files structure.
2323
.PARAMETER ContinueOnIssue
2424
Whether the export should continue in the event of files fail to export; If not set 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 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.
2525
.PARAMETER RetentionTime
26-
Duration of artifact expire, by days.
26+
Duration of the artifact become expire, by days.
2727
.OUTPUTS
28-
[PSCustomObject] Exported artifact's metadata.
28+
[PSCustomObject] Metadata of the exported artifact.
2929
#>
3030
Function Export-Artifact {
3131
[CmdletBinding(DefaultParameterSetName = 'Path', HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_export-githubactionsartifact#Export-GitHubActionsArtifact')]
3232
[OutputType([PSCustomObject])]
3333
Param (
34-
[Parameter(Mandatory = $True, Position = 0, ValueFromPipelineByPropertyName = $True)][ValidateScript({ Test-ArtifactName -InputObject $_ }, ErrorMessage = '`{0}` is not a valid GitHub Actions artifact name!')][String]$Name,
35-
[Parameter(Mandatory = $True, ParameterSetName = 'Path', Position = 1, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)][SupportsWildcards()][Alias('File', 'Files', 'Paths')][String[]]$Path,
36-
[Parameter(Mandatory = $True, ParameterSetName = 'LiteralPath', ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)][Alias('LiteralFile', 'LiteralFiles', 'LiteralPaths', 'LP', 'PSPath', 'PSPaths')][String[]]$LiteralPath,
37-
[Parameter(ValueFromPipelineByPropertyName = $True)][ValidateScript({ [System.IO.Path]::IsPathRooted($_) -and (Test-Path -LiteralPath $_ -PathType 'Container') }, ErrorMessage = '`{0}` is not an exist and valid GitHub Actions artifact base root!')][Alias('Root')][String]$BaseRoot = $Env:GITHUB_WORKSPACE,
34+
[Parameter(Mandatory = $True, Position = 0, ValueFromPipelineByPropertyName = $True)][String]$Name,
35+
[Parameter(Mandatory = $True, ParameterSetName = 'Path', Position = 1, ValueFromPipelineByPropertyName = $True)][SupportsWildcards()][Alias('File', 'Files', 'Paths')][String[]]$Path,
36+
[Parameter(Mandatory = $True, ParameterSetName = 'LiteralPath', ValueFromPipelineByPropertyName = $True)][Alias('LiteralFile', 'LiteralFiles', 'LiteralPaths', 'LP', 'PSPath', 'PSPaths')][String[]]$LiteralPath,
37+
[Parameter(ValueFromPipelineByPropertyName = $True)][Alias('Root')][String]$BaseRoot = $Env:GITHUB_WORKSPACE,
3838
[Parameter(ValueFromPipelineByPropertyName = $True)][Alias('ContinueOnError')][Switch]$ContinueOnIssue,
39-
[Parameter(ValueFromPipelineByPropertyName = $True)][ValidateRange(1, 90)][Alias('RetentionDay')][Byte]$RetentionTime
39+
[Parameter(ValueFromPipelineByPropertyName = $True)][Alias('RetentionDay')][Byte]$RetentionTime
4040
)
4141
Begin {
4242
[Boolean]$NoOperation = !(Test-GitHubActionsEnvironment -Artifact)# When the requirements are not fulfill, only stop this function but not others.
@@ -48,26 +48,12 @@ Function Export-Artifact {
4848
If ($NoOperation) {
4949
Return
5050
}
51-
Switch ($PSCmdlet.ParameterSetName) {
52-
'LiteralPath' {
53-
[String[]]$PathsProceed = $LiteralPath |
54-
ForEach-Object -Process { [System.IO.Path]::IsPathRooted($_) ? $_ : (Join-Path -Path $BaseRoot -ChildPath $_) }
55-
}
56-
'Path' {
57-
[String[]]$PathsProceed = @()
58-
ForEach ($Item In $Path) {
59-
Try {
60-
$PathsProceed += Resolve-Path -Path ([System.IO.Path]::IsPathRooted($Item) ? $Item : (Join-Path -Path $BaseRoot -ChildPath $Item))
61-
}
62-
Catch {
63-
$PathsProceed += $Item
64-
}
65-
}
66-
}
67-
}
6851
[Hashtable]$InputObject = @{
6952
Name = $Name
70-
Path = $PathsProceed
53+
Path = ($PSCmdlet.ParameterSetName -ieq 'LiteralPath') ? (
54+
$LiteralPath |
55+
ForEach-Object -Process { [WildcardPattern]::Escape($_) }
56+
) : $Path
7157
BaseRoot = $BaseRoot
7258
ContinueOnIssue = $ContinueOnIssue.IsPresent
7359
}
@@ -83,25 +69,25 @@ Set-Alias -Name 'Save-Artifact' -Value 'Export-Artifact' -Option 'ReadOnly' -Sco
8369
.SYNOPSIS
8470
GitHub Actions - Import Artifact
8571
.DESCRIPTION
86-
Import artifact that shared data from previous job in the same workflow.
72+
Import artifact that shared data from past job in the same workflow.
8773
.PARAMETER Name
88-
Artifact name.
74+
Name of the artifact.
8975
.PARAMETER CreateSubfolder
90-
Create a subfolder with artifact name and put data into here.
76+
Whether to create a subfolder with artifact name and put data into here.
9177
.PARAMETER All
92-
Import all artifacts that shared data from previous job in the same workflow; Always create subfolder.
78+
Import all of the artifacts that shared data from past job in the same workflow; Always create subfolders.
9379
.PARAMETER Destination
94-
Artifact destination.
80+
Absolute literal path(s) of the destination of the artifact(s).
9581
.OUTPUTS
96-
[PSCustomObject] Imported artifact's metadata.
97-
[PSCustomObject[]] Imported artifacts' metadata.
82+
[PSCustomObject] Metadata of the imported artifact.
83+
[PSCustomObject[]] Metadata of the imported artifacts.
9884
#>
9985
Function Import-Artifact {
10086
[CmdletBinding(DefaultParameterSetName = 'Single', HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_import-githubactionsartifact#Import-GitHubActionsArtifact')]
10187
[OutputType([PSCustomObject[]], ParameterSetName = 'All')]
10288
[OutputType([PSCustomObject], ParameterSetName = 'Single')]
10389
Param (
104-
[Parameter(Mandatory = $True, ParameterSetName = 'Single', Position = 0, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)][ValidateScript({ Test-ArtifactName -InputObject $_ }, ErrorMessage = '`{0}` is not a valid GitHub Actions artifact name!')][String]$Name,
90+
[Parameter(Mandatory = $True, ParameterSetName = 'Single', Position = 0, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)][String]$Name,
10591
[Parameter(ParameterSetName = 'Single', ValueFromPipelineByPropertyName = $True)][Switch]$CreateSubfolder,
10692
[Parameter(Mandatory = $True, ParameterSetName = 'All')][Switch]$All,
10793
[Parameter(ValueFromPipelineByPropertyName = $True)][Alias('Dest', 'Target')][String]$Destination = $Env:GITHUB_WORKSPACE
@@ -135,48 +121,6 @@ Function Import-Artifact {
135121
}
136122
}
137123
Set-Alias -Name 'Restore-Artifact' -Value 'Import-Artifact' -Option 'ReadOnly' -Scope 'Local'
138-
<#
139-
.SYNOPSIS
140-
GitHub Actions (Private) - Test Artifact Name
141-
.DESCRIPTION
142-
Test GitHub Actions artifact name whether is valid.
143-
.PARAMETER InputObject
144-
GitHub Actions artifact name that need to test.
145-
.OUTPUTS
146-
[Boolean] Test result.
147-
#>
148-
Function Test-ArtifactName {
149-
[CmdletBinding()]
150-
[OutputType([Boolean])]
151-
Param (
152-
[Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $True)][Alias('Input', 'Object')][String]$InputObject
153-
)
154-
Process {
155-
(Test-ArtifactPath -InputObject $InputObject) -and $InputObject -imatch '^[^\\/]+$' |
156-
Write-Output
157-
}
158-
}
159-
<#
160-
.SYNOPSIS
161-
GitHub Actions (Private) - Test Artifact Path
162-
.DESCRIPTION
163-
Test GitHub Actions artifact path whether is valid.
164-
.PARAMETER InputObject
165-
GitHub Actions artifact path that need to test.
166-
.OUTPUTS
167-
[Boolean] Test result.
168-
#>
169-
Function Test-ArtifactPath {
170-
[CmdletBinding()]
171-
[OutputType([Boolean])]
172-
Param (
173-
[Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $True)][Alias('Input', 'Object')][String]$InputObject
174-
)
175-
Process {
176-
$InputObject -imatch '^[^":<>|*?\n\r\t]+$' |
177-
Write-Output
178-
}
179-
}
180124
Export-ModuleMember -Function @(
181125
'Export-Artifact',
182126
'Import-Artifact'

hugoalh.GitHubActionsToolkit/module/cache.psm1

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ Import-Module -Name (
1111
.SYNOPSIS
1212
GitHub Actions - Restore Cache
1313
.DESCRIPTION
14-
Restore cache that shared data from previous job in the same workflow.
14+
Restore cache that shared data from past job in the same workflow.
1515
.PARAMETER Key
16-
Cache key.
16+
Key of the cache.
1717
.PARAMETER Path
18-
Cache destination path.
18+
Path of the destination of the cache.
1919
.PARAMETER LiteralPath
20-
Cache destination literal path.
20+
Absolute literal path of the destination of the cache.
2121
.PARAMETER NotUseAzureSdk
22-
Do not use Azure Blob SDK to download caches that are stored on Azure Blob Storage, this maybe affect reliability and performance.
22+
Whether to not use Azure Blob SDK to download caches that are stored on the Azure Blob Storage, this maybe affect the reliability and performance.
2323
.PARAMETER DownloadConcurrency
2424
Number of parallel downloads (only for Azure SDK).
2525
.PARAMETER Timeout
@@ -84,11 +84,11 @@ Set-Alias -Name 'Import-Cache' -Value 'Restore-Cache' -Option 'ReadOnly' -Scope
8484
.SYNOPSIS
8585
GitHub Actions - Save cache
8686
.DESCRIPTION
87-
Save cache to persist data and/or share with another job in the same workflow.
87+
Save cache to persist data and/or share with future job in the same workflow.
8888
.PARAMETER Key
89-
Cache key.
89+
Key of the cache.
9090
.PARAMETER Path
91-
Cache path.
91+
Paths of the cache.
9292
.PARAMETER LiteralPath
9393
Cache literal path.
9494
.PARAMETER UploadChunkSizes

hugoalh.GitHubActionsToolkit/module/internal/token.psm1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Generate a new random token.
1111
.PARAMETER Length
1212
Length of the token.
1313
.PARAMETER WithUpperCase
14-
Contain upper case letters in the token.
14+
Whether to contain upper case letters in the token.
1515
.OUTPUTS
1616
[String] A new random token.
1717
#>
@@ -22,7 +22,8 @@ Function New-RandomToken {
2222
[Parameter(Position = 0)][ValidateRange(1, [UInt32]::MaxValue)][UInt32]$Length = 8,
2323
[Alias('UpperCase')][Switch]$WithUpperCase
2424
)
25-
[Char[]]$Pool = $PoolLowerCase + $PoolNumber + ($WithUpperCase.IsPresent ? $PoolUpperCase : @())
25+
[Char[]]$Pool = $PoolLowerCase + $PoolNumber + ($WithUpperCase.IsPresent ? $PoolUpperCase : @()) |
26+
Get-Random -Shuffle
2627
@(1..$Length) |
2728
ForEach-Object -Process {
2829
$Pool |

hugoalh.GitHubActionsToolkit/module/nodejs-wrapper/artifact/download-all.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1+
import { argumentHandle, errorHandle } from "../handle.js";
12
import { create as ghactionsArtifact } from "@actions/artifact";
2-
const [inputs, delimiter] = process.argv.slice(2);
3-
const { Destination } = JSON.parse(inputs);
4-
const result = await ghactionsArtifact().downloadAllArtifacts(Destination)
5-
.catch((reason) => {
6-
console.error(reason);
7-
return process.exit(1);
8-
});
3+
const {
4+
delimiter,
5+
Destination
6+
} = argumentHandle();
7+
const result = await ghactionsArtifact().downloadAllArtifacts(Destination).catch(errorHandle);
98
console.log(delimiter);
109
console.log(JSON.stringify(result.map((value) => {
1110
return {

hugoalh.GitHubActionsToolkit/module/nodejs-wrapper/artifact/download.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1+
import { argumentHandle, errorHandle } from "../handle.js";
12
import { create as ghactionsArtifact } from "@actions/artifact";
2-
const [inputs, delimiter] = process.argv.slice(2);
33
const {
4+
delimiter,
45
CreateSubfolder,
56
Destination,
67
Name
7-
} = JSON.parse(inputs);
8-
const result = await ghactionsArtifact().downloadArtifact(Name, Destination, { createArtifactFolder: CreateSubfolder })
9-
.catch((reason) => {
10-
console.error(reason);
11-
return process.exit(1);
12-
});
8+
} = argumentHandle();
9+
const result = await ghactionsArtifact().downloadArtifact(Name, Destination, { createArtifactFolder: CreateSubfolder }).catch(errorHandle);
1310
console.log(delimiter);
1411
console.log(JSON.stringify({
1512
Name: result.artifactName,

hugoalh.GitHubActionsToolkit/module/nodejs-wrapper/artifact/upload.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1+
import { argumentHandle, errorHandle } from "../handle.js";
12
import { create as ghactionsArtifact } from "@actions/artifact";
2-
const [inputs, delimiter] = process.argv.slice(2);
33
const {
4+
delimiter,
45
BaseRoot,
56
ContinueOnIssue,
67
Name,
78
Path,
89
RetentionTime
9-
} = JSON.parse(inputs);
10+
} = argumentHandle();
1011
const result = await ghactionsArtifact().uploadArtifact(Name, Path, BaseRoot, {
1112
continueOnError: ContinueOnIssue,
1213
retentionDays: RetentionTime
13-
})
14-
.catch((reason) => {
15-
console.error(reason);
16-
return process.exit(1);
17-
});
14+
}).catch(errorHandle);
1815
console.log(delimiter);
1916
console.log(JSON.stringify({
2017
FailedItem: result.failedItems,
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1+
import { argumentHandle, errorHandle } from "../handle.js";
12
import { restoreCache as ghactionsCacheRestoreCache } from "@actions/cache";
2-
const [inputs, delimiter] = process.argv.slice(2);
33
const {
4+
delimiter,
45
DownloadConcurrency,
56
Path,
67
PrimaryKey,
78
RestoreKey,
89
Timeout,
910
UseAzureSdk
10-
} = JSON.parse(inputs);
11+
} = argumentHandle();
1112
const result = await ghactionsCacheRestoreCache(Path, PrimaryKey, RestoreKey, {
1213
downloadConcurrency: DownloadConcurrency,
1314
timeoutInMs: Timeout,
1415
useAzureSdk: UseAzureSdk
15-
})
16-
.catch((reason) => {
17-
console.error(reason);
18-
return process.exit(1);
19-
});
16+
}).catch(errorHandle);
2017
console.log(delimiter);
2118
console.log(JSON.stringify({ CacheKey: result }));
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1+
import { argumentHandle, errorHandle } from "../handle.js";
12
import { saveCache as ghactionsCacheSaveCache } from "@actions/cache";
2-
const [inputs, delimiter] = process.argv.slice(2);
33
const {
4+
delimiter,
45
Key,
56
Path,
67
UploadChunkSizes,
78
UploadConcurrency
8-
} = JSON.parse(inputs);
9+
} = argumentHandle();
910
const result = await ghactionsCacheSaveCache(Path, Key, {
1011
uploadChunkSize: UploadChunkSizes,
1112
uploadConcurrency: UploadConcurrency
12-
})
13-
.catch((reason) => {
14-
console.error(reason);
15-
return process.exit(1);
16-
});
13+
}).catch(errorHandle);
1714
console.log(delimiter);
1815
console.log(JSON.stringify({ CacheId: result }));
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function argumentHandle() {
2+
const [inputs, delimiter] = process.argv.slice(2);
3+
return {
4+
...JSON.parse(inputs),
5+
delimiter
6+
};
7+
}
8+
function errorHandle(reason) {
9+
console.error(reason);
10+
return process.exit(1);
11+
}
12+
export {
13+
argumentHandle,
14+
errorHandle
15+
};
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
import { argumentHandle, errorHandle } from "../handle.js";
12
import { getIDToken as ghactionsGetOpenIDConnectToken } from "@actions/core";
2-
const [inputs, delimiter] = process.argv.slice(2);
3-
const { Audience } = JSON.parse(inputs);
4-
const result = await ghactionsGetOpenIDConnectToken(Audience)
5-
.catch((reason) => {
6-
console.error(reason);
7-
return process.exit(1);
8-
});
3+
const {
4+
delimiter,
5+
Audience
6+
} = argumentHandle();
7+
const result = await ghactionsGetOpenIDConnectToken(Audience).catch(errorHandle);
98
console.log(delimiter);
109
console.log(JSON.stringify({ Token: result }));

0 commit comments

Comments
 (0)