|
| 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 - Restore Cache |
| 10 | +.DESCRIPTION |
| 11 | +Restore cache that shared data from previous job in the same workflow. |
| 12 | +.PARAMETER Key |
| 13 | +Cache key. |
| 14 | +.PARAMETER Path |
| 15 | +Cache destination path. |
| 16 | +.PARAMETER LiteralPath |
| 17 | +Cache destination literal path. |
| 18 | +.PARAMETER NotUseAzureSdk |
| 19 | +Do not use Azure Blob SDK to download caches that are stored on Azure Blob Storage, this maybe affect reliability and performance. |
| 20 | +.PARAMETER DownloadConcurrency |
| 21 | +Number of parallel downloads (only for Azure SDK). |
| 22 | +.PARAMETER Timeout |
| 23 | +Maximum time for each download request by seconds (only for Azure SDK). |
| 24 | +.OUTPUTS |
| 25 | +[String] The key of the cache hit. |
| 26 | +#> |
| 27 | +Function Restore-Cache { |
| 28 | + [CmdletBinding(DefaultParameterSetName = 'Path', HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_restore-githubactionscache#Restore-GitHubActionsCache')] |
| 29 | + [OutputType([String])] |
| 30 | + Param ( |
| 31 | + [Parameter(Mandatory = $True, Position = 0)][ValidateScript({ |
| 32 | + Return (Test-CacheKey -InputObject $_) |
| 33 | + }, ErrorMessage = '`{0}` is not a valid GitHub Actions cache key, and/or more than 512 characters!')][Alias('Keys', 'Name', 'Names')][String[]]$Key, |
| 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('NoAzureSdk')][Switch]$NotUseAzureSdk, |
| 37 | + [ValidateRange(1, 16)][AllowNull()][Byte]$DownloadConcurrency = $Null, |
| 38 | + [ValidateRange(5, 900)][AllowNull()][UInt16]$Timeout = $Null |
| 39 | + ) |
| 40 | + Begin { |
| 41 | + [String[]]$PathsProceed = @() |
| 42 | + } |
| 43 | + Process { |
| 44 | + Switch ($PSCmdlet.ParameterSetName) { |
| 45 | + 'Path' { |
| 46 | + $PathsProceed += $Path |
| 47 | + } |
| 48 | + 'LiteralPath' { |
| 49 | + $PathsProceed += ($LiteralPath | ForEach-Object -Process { |
| 50 | + Return [WildcardPattern]::Escape($_) |
| 51 | + }) |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + End { |
| 56 | + If (!(Test-GitHubActionsEnvironment -Cache)) { |
| 57 | + Return (Write-Error -Message 'Unable to get GitHub Actions cache resources!' -Category 'ResourceUnavailable') |
| 58 | + } |
| 59 | + [String[]]$KeysProceed = @() |
| 60 | + If ($Key.Count -igt 10) { |
| 61 | + Write-Warning -Message "Keys are limit to maximum count of 10! Only first 10 keys will use." |
| 62 | + $KeysProceed += ($Key | Select-Object -First 10) |
| 63 | + } Else { |
| 64 | + $KeysProceed += $Key |
| 65 | + } |
| 66 | + If ($PathsProceed.Count -ieq 0) { |
| 67 | + Return (Write-Error -Message 'No valid path is defined!' -Category 'NotSpecified') |
| 68 | + } |
| 69 | + [Hashtable]$InputObject = @{ |
| 70 | + Path = $PathsProceed |
| 71 | + PrimaryKey = $KeysProceed[0] |
| 72 | + RestoreKey = ($KeysProceed | Select-Object -SkipIndex 0) |
| 73 | + UseAzureSdk = !$NotUseAzureSdk.IsPresent |
| 74 | + } |
| 75 | + If (!$NotUseAzureSdk.IsPresent) { |
| 76 | + If ($Null -ine $DownloadConcurrency) { |
| 77 | + $InputObject.DownloadConcurrency = $DownloadConcurrency |
| 78 | + } |
| 79 | + If ($Null -ine $Timeout) { |
| 80 | + $InputObject.Timeout = $Timeout * 1000 |
| 81 | + } |
| 82 | + } |
| 83 | + $ResultRaw = Invoke-GitHubActionsNodeJsWrapper -Path 'cache\restore.js' -InputObject ([PSCustomObject]$InputObject | ConvertTo-Json -Depth 100 -Compress) |
| 84 | + If ($ResultRaw -ieq $False) { |
| 85 | + Return |
| 86 | + } |
| 87 | + Return ($ResultRaw | ConvertFrom-Json -Depth 100).CacheKey |
| 88 | + } |
| 89 | +} |
| 90 | +Set-Alias -Name 'Import-Cache' -Value 'Restore-Cache' -Option 'ReadOnly' -Scope 'Local' |
| 91 | +<# |
| 92 | +.SYNOPSIS |
| 93 | +GitHub Actions - Save cache |
| 94 | +.DESCRIPTION |
| 95 | +Save cache to persist data and/or share with another job in the same workflow. |
| 96 | +.PARAMETER Key |
| 97 | +Cache key. |
| 98 | +.PARAMETER Path |
| 99 | +Cache path. |
| 100 | +.PARAMETER LiteralPath |
| 101 | +Cache literal path. |
| 102 | +.PARAMETER UploadChunkSizes |
| 103 | +Maximum chunk size by bytes. |
| 104 | +.PARAMETER UploadConcurrency |
| 105 | +Number of parallel uploads. |
| 106 | +.OUTPUTS |
| 107 | +[String] Cache ID. |
| 108 | +#> |
| 109 | +Function Save-Cache { |
| 110 | + [CmdletBinding(DefaultParameterSetName = 'Path', HelpUri = 'https://github.com/hugoalh-studio/ghactions-toolkit-powershell/wiki/api_function_save-githubactionscache#Save-GitHubActionsCache')] |
| 111 | + [OutputType([String])] |
| 112 | + Param ( |
| 113 | + [Parameter(Mandatory = $True, Position = 0)][ValidateScript({ |
| 114 | + Return (Test-CacheKey -InputObject $_) |
| 115 | + }, ErrorMessage = '`{0}` is not a valid GitHub Actions cache key, and/or more than 512 characters!')][Alias('Name')][String]$Key, |
| 116 | + [Parameter(Mandatory = $True, ParameterSetName = 'Path', Position = 1, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)][SupportsWildcards()][Alias('File', 'Files', 'Paths')][String[]]$Path, |
| 117 | + [Parameter(Mandatory = $True, ParameterSetName = 'LiteralPath', ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)][Alias('LiteralFile', 'LiteralFiles', 'LiteralPaths', 'LP', 'PSPath', 'PSPaths')][String[]]$LiteralPath, |
| 118 | + [ValidateRange(1KB, 1GB)][AllowNull()][UInt32]$UploadChunkSizes = $Null, |
| 119 | + [ValidateRange(1, 16)][AllowNull()][Byte]$UploadConcurrency = $Null |
| 120 | + ) |
| 121 | + Begin { |
| 122 | + [String[]]$PathsProceed = @() |
| 123 | + } |
| 124 | + Process { |
| 125 | + Switch ($PSCmdlet.ParameterSetName) { |
| 126 | + 'Path' { |
| 127 | + $PathsProceed += $Path |
| 128 | + } |
| 129 | + 'LiteralPath' { |
| 130 | + $PathsProceed += ($LiteralPath | ForEach-Object -Process { |
| 131 | + Return [WildcardPattern]::Escape($_) |
| 132 | + }) |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + End { |
| 137 | + If (!(Test-GitHubActionsEnvironment -Cache)) { |
| 138 | + Return (Write-Error -Message 'Unable to get GitHub Actions cache resources!' -Category 'ResourceUnavailable') |
| 139 | + } |
| 140 | + If ($PathsProceed.Count -ieq 0) { |
| 141 | + Return (Write-Error -Message 'No valid path is defined!' -Category 'NotSpecified') |
| 142 | + } |
| 143 | + [Hashtable]$InputObject = @{ |
| 144 | + Key = $Key |
| 145 | + Path = $PathsProceed |
| 146 | + } |
| 147 | + If ($Null -ine $UploadChunkSizes) { |
| 148 | + $InputObject.UploadChunkSizes = $UploadChunkSizes |
| 149 | + } |
| 150 | + If ($Null -ine $UploadConcurrency) { |
| 151 | + $InputObject.UploadConcurrency = $UploadConcurrency |
| 152 | + } |
| 153 | + $ResultRaw = Invoke-GitHubActionsNodeJsWrapper -Path 'cache\save.js' -InputObject ([PSCustomObject]$InputObject | ConvertTo-Json -Depth 100 -Compress) |
| 154 | + If ($ResultRaw -ieq $False) { |
| 155 | + Return |
| 156 | + } |
| 157 | + Return ($ResultRaw | ConvertFrom-Json -Depth 100).CacheId |
| 158 | + } |
| 159 | +} |
| 160 | +Set-Alias -Name 'Export-Cache' -Value 'Save-Cache' -Option 'ReadOnly' -Scope 'Local' |
| 161 | +<# |
| 162 | +.SYNOPSIS |
| 163 | +GitHub Actions (Internal) - Test Cache Key |
| 164 | +.DESCRIPTION |
| 165 | +Test GitHub Actions cache key whether is valid. |
| 166 | +.PARAMETER InputObject |
| 167 | +GitHub Actions cache key that need to test. |
| 168 | +.OUTPUTS |
| 169 | +[Boolean] Test result. |
| 170 | +#> |
| 171 | +Function Test-CacheKey { |
| 172 | + [CmdletBinding()] |
| 173 | + [OutputType([Boolean])] |
| 174 | + Param ( |
| 175 | + [Parameter(Mandatory = $True, Position = 0)][AllowEmptyString()][Alias('Input', 'Object')][String]$InputObject |
| 176 | + ) |
| 177 | + Return ($InputObject.Length -ile 512 -and $InputObject -imatch '^[^,\n\r]+$') |
| 178 | +} |
| 179 | +Export-ModuleMember -Function @( |
| 180 | + 'Restore-Cache', |
| 181 | + 'Save-Cache' |
| 182 | +) -Alias @( |
| 183 | + 'Export-Cache', |
| 184 | + 'Import-Cache' |
| 185 | +) |
0 commit comments