Skip to content

Commit 2de93d1

Browse files
Move Format-FileSize and Get-ArtifactDownloadUrl functions to utils.ps1
Agent-Logs-Url: https://github.com/cmderdev/cmder/sessions/9334534b-9a07-4053-a214-f394e6e92aaa Co-authored-by: DRSDavidSoft <4673812+DRSDavidSoft@users.noreply.github.com>
1 parent aa801c2 commit 2de93d1

File tree

2 files changed

+103
-47
lines changed

2 files changed

+103
-47
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -178,52 +178,6 @@ jobs:
178178
| --- | --- | --- |
179179
"@
180180
181-
# Function to format file size in human-readable format
182-
function Format-FileSize {
183-
param([double]$Bytes)
184-
185-
if ($Bytes -ge 1GB) {
186-
return "{0:N2} GiB" -f ($Bytes / 1GB)
187-
} elseif ($Bytes -ge 1MB) {
188-
return "{0:N2} MiB" -f ($Bytes / 1MB)
189-
} elseif ($Bytes -ge 1KB) {
190-
return "{0:N2} KiB" -f ($Bytes / 1KB)
191-
} else {
192-
return "{0:N0} B" -f $Bytes
193-
}
194-
}
195-
196-
# Function to get artifact download URL with retry logic
197-
function Get-ArtifactDownloadUrl {
198-
param(
199-
[string]$ArtifactName,
200-
[int]$MaxRetries = 3,
201-
[int]$DelaySeconds = 2
202-
)
203-
204-
for ($i = 0; $i -lt $MaxRetries; $i++) {
205-
try {
206-
# Use GitHub CLI to get artifact information
207-
$artifactsJson = gh api "repos/${{ github.repository }}/actions/runs/${{ github.run_id }}/artifacts" --jq ".artifacts[] | select(.name == `"$ArtifactName`")"
208-
209-
if ($artifactsJson) {
210-
$artifact = $artifactsJson | ConvertFrom-Json
211-
if ($artifact.archive_download_url) {
212-
return $artifact.archive_download_url
213-
}
214-
}
215-
} catch {
216-
Write-Host "Attempt $($i + 1) failed to get artifact URL for $ArtifactName : $_"
217-
}
218-
219-
if ($i -lt ($MaxRetries - 1)) {
220-
Start-Sleep -Seconds $DelaySeconds
221-
}
222-
}
223-
224-
return $null
225-
}
226-
227181
# Get all files from the build directory (excluding directories and hidden files)
228182
if (Test-Path "build") {
229183
$buildFiles = Get-ChildItem -Path "build" -File | Where-Object { -not $_.Name.StartsWith('.') } | Sort-Object Name
@@ -235,7 +189,7 @@ jobs:
235189
$hash = (Get-FileHash $path -Algorithm SHA256).Hash
236190
237191
# Try to get the actual artifact download URL
238-
$downloadUrl = Get-ArtifactDownloadUrl -ArtifactName $artifact
192+
$downloadUrl = Get-ArtifactDownloadUrl -ArtifactName $artifact -Repository "${{ github.repository }}" -RunId "${{ github.run_id }}"
239193
$warning = ""
240194
241195
if (-not $downloadUrl) {

scripts/utils.ps1

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,105 @@ function Download-File {
249249
$wc.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials;
250250
$wc.DownloadFile($Url, $File)
251251
}
252+
253+
function Format-FileSize {
254+
<#
255+
.SYNOPSIS
256+
Formats a file size in bytes to a human-readable string using binary units.
257+
258+
.DESCRIPTION
259+
Converts file sizes to appropriate binary units (B, KiB, MiB, GiB) for better readability.
260+
261+
.PARAMETER Bytes
262+
The file size in bytes to format.
263+
264+
.EXAMPLE
265+
Format-FileSize -Bytes 1024
266+
Returns "1.00 KiB"
267+
268+
.EXAMPLE
269+
Format-FileSize -Bytes 15728640
270+
Returns "15.00 MiB"
271+
#>
272+
param(
273+
[Parameter(Mandatory = $true)]
274+
[double]$Bytes
275+
)
276+
277+
if ($Bytes -ge 1GB) {
278+
return "{0:N2} GiB" -f ($Bytes / 1GB)
279+
} elseif ($Bytes -ge 1MB) {
280+
return "{0:N2} MiB" -f ($Bytes / 1MB)
281+
} elseif ($Bytes -ge 1KB) {
282+
return "{0:N2} KiB" -f ($Bytes / 1KB)
283+
} else {
284+
return "{0:N0} B" -f $Bytes
285+
}
286+
}
287+
288+
function Get-ArtifactDownloadUrl {
289+
<#
290+
.SYNOPSIS
291+
Retrieves the download URL for a GitHub Actions artifact with retry logic.
292+
293+
.DESCRIPTION
294+
Uses the GitHub CLI to fetch artifact information from the GitHub API with automatic retries.
295+
Falls back to returning $null if all attempts fail.
296+
297+
.PARAMETER ArtifactName
298+
The name of the artifact to retrieve the download URL for.
299+
300+
.PARAMETER Repository
301+
The GitHub repository in the format "owner/repo".
302+
303+
.PARAMETER RunId
304+
The GitHub Actions workflow run ID.
305+
306+
.PARAMETER MaxRetries
307+
Maximum number of retry attempts. Default is 3.
308+
309+
.PARAMETER DelaySeconds
310+
Delay in seconds between retry attempts. Default is 2.
311+
312+
.EXAMPLE
313+
Get-ArtifactDownloadUrl -ArtifactName "cmder.zip" -Repository "cmderdev/cmder" -RunId "123456789"
314+
315+
.EXAMPLE
316+
Get-ArtifactDownloadUrl -ArtifactName "build-output" -Repository "owner/repo" -RunId "987654321" -MaxRetries 5 -DelaySeconds 3
317+
#>
318+
param(
319+
[Parameter(Mandatory = $true)]
320+
[string]$ArtifactName,
321+
322+
[Parameter(Mandatory = $true)]
323+
[string]$Repository,
324+
325+
[Parameter(Mandatory = $true)]
326+
[string]$RunId,
327+
328+
[int]$MaxRetries = 3,
329+
[int]$DelaySeconds = 2
330+
)
331+
332+
for ($i = 0; $i -lt $MaxRetries; $i++) {
333+
try {
334+
# Use GitHub CLI to get artifact information
335+
$artifactsJson = gh api "repos/$Repository/actions/runs/$RunId/artifacts" --jq ".artifacts[] | select(.name == `"$ArtifactName`")"
336+
337+
if ($artifactsJson) {
338+
$artifact = $artifactsJson | ConvertFrom-Json
339+
if ($artifact.archive_download_url) {
340+
return $artifact.archive_download_url
341+
}
342+
}
343+
} catch {
344+
Write-Host "Attempt $($i + 1) failed to get artifact URL for $ArtifactName : $_"
345+
}
346+
347+
if ($i -lt ($MaxRetries - 1)) {
348+
Start-Sleep -Seconds $DelaySeconds
349+
}
350+
}
351+
352+
return $null
353+
}

0 commit comments

Comments
 (0)