Skip to content

Commit 995e6fe

Browse files
committed
Add support to use workflow artifacts as deps
1 parent ceba0d8 commit 995e6fe

3 files changed

Lines changed: 166 additions & 32 deletions

File tree

.github/workflows/libxml2.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ on:
1212
description: the series stability
1313
required: false
1414
default: 'staging'
15+
workflow_run_ids:
16+
description: Comma-separated winlib-builder workflow run IDs to download dependency artifacts from
17+
required: false
18+
permissions:
19+
actions: read
20+
contents: read
1521
defaults:
1622
run:
1723
shell: cmd
@@ -41,7 +47,9 @@ jobs:
4147
arch: ${{matrix.arch}}
4248
toolset: ${{steps.virtuals.outputs.toolset}}
4349
- name: Fetch dependencies
44-
run: powershell winlib-builder/scripts/fetch-deps -lib libxml2 -version ${{github.event.inputs.php}} -vs ${{steps.virtuals.outputs.vs}} -arch ${{matrix.arch}} -stability ${{github.event.inputs.stability}}
50+
run: powershell winlib-builder/scripts/fetch-deps -lib libxml2 -version ${{github.event.inputs.php}} -vs ${{steps.virtuals.outputs.vs}} -arch ${{matrix.arch}} -stability ${{github.event.inputs.stability}} -workflow_run_ids "${{github.event.inputs.workflow_run_ids}}"
51+
env:
52+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4553
- name: Configure libxml2
4654
run: cd libxml2\win32 && cscript configure.js lib=%GITHUB_WORKSPACE%\deps\lib include=%GITHUB_WORKSPACE%\deps\include vcmanifest=yes prefix=%GITHUB_WORKSPACE%\install
4755
- name: Build libxml2

.github/workflows/libxslt.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ on:
1212
description: the series stability
1313
required: false
1414
default: 'staging'
15+
workflow_run_ids:
16+
description: Comma-separated winlib-builder workflow run IDs to download dependency artifacts from
17+
required: false
18+
permissions:
19+
actions: read
20+
contents: read
1521
defaults:
1622
run:
1723
shell: cmd
@@ -41,7 +47,9 @@ jobs:
4147
arch: ${{matrix.arch}}
4248
toolset: ${{steps.virtuals.outputs.toolset}}
4349
- name: Fetch dependencies
44-
run: powershell winlib-builder/scripts/fetch-deps -lib libxslt -version ${{github.event.inputs.php}} -vs ${{steps.virtuals.outputs.vs}} -arch ${{matrix.arch}} -stability ${{github.event.inputs.stability}}
50+
run: powershell winlib-builder/scripts/fetch-deps -lib libxslt -version ${{github.event.inputs.php}} -vs ${{steps.virtuals.outputs.vs}} -arch ${{matrix.arch}} -stability ${{github.event.inputs.stability}} -workflow_run_ids "${{github.event.inputs.workflow_run_ids}}"
51+
env:
52+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4553
- name: Build libxslt
4654
run: |
4755
cd libxslt\win32

scripts/fetch-deps.ps1

Lines changed: 148 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ param (
33
[Parameter(Mandatory)] [String] $version,
44
[Parameter(Mandatory)] [String] $vs,
55
[Parameter(Mandatory)] [String] $arch,
6-
[Parameter(Mandatory)] [String] $stability
6+
[Parameter(Mandatory)] [String] $stability,
7+
[String] $workflow_run_id = "",
8+
[String] $workflow_run_ids = "",
9+
[String] $repository = ""
710
)
811

912
$ErrorActionPreference = "Stop"
@@ -31,50 +34,165 @@ if (-not $deps) {
3134
exit
3235
}
3336

34-
$needs = @{}
35-
$response = Invoke-WebRequest -Uri "https://downloads.php.net/~windows/php-sdk/deps/series/packages-$version-$vs-$arch-$stability.txt" -UseBasicParsing
36-
foreach ($line in $response.Content -split "\r?\n") {
37-
foreach ($dep in $deps) {
38-
if ($line -match "$dep-(.+)-$vs-$arch.zip") {
39-
$needs.$dep = $matches[1]
40-
}
37+
function Get-GitHubApiHeaders {
38+
$headers = @{
39+
"Accept" = "application/vnd.github+json";
40+
"X-GitHub-Api-Version" = "2022-11-28";
41+
"User-Agent" = "winlib-builder"
42+
}
43+
44+
if ($env:GITHUB_TOKEN) {
45+
$headers.Authorization = "Bearer $env:GITHUB_TOKEN"
46+
} else {
47+
Write-Warning "GITHUB_TOKEN is not set. GitHub API requests may fail or be rate-limited."
4148
}
49+
50+
return $headers
4251
}
4352

44-
New-Item "deps" -ItemType "directory"
53+
function Resolve-GitHubArtifactDependencies {
54+
param (
55+
[String[]] $Dependencies,
56+
[Object[]] $RunIds,
57+
[String] $Repository,
58+
[String] $Destination
59+
)
4560

46-
$baseurl = "https://downloads.php.net/~windows/php-sdk/deps/$vs/$arch"
47-
foreach ($dep in $needs.GetEnumerator()) {
48-
Write-Output "Fetching $($dep.Name)-$($dep.Value)"
49-
$temp = New-TemporaryFile | Rename-Item -NewName {$_.Name + ".zip"} -PassThru
50-
$url = "$baseurl/$($dep.Name)-$($dep.Value)-$vs-$arch.zip"
51-
Invoke-WebRequest $url -OutFile $temp
52-
Expand-Archive $temp -DestinationPath "deps"
61+
$resolved = @{}
62+
$runIds = @(
63+
foreach ($runIdValue in $RunIds) {
64+
if ($null -eq $runIdValue) {
65+
continue
66+
}
67+
68+
$runIdValue.ToString() -split "[,\s]+" |
69+
ForEach-Object { $_.Trim() } |
70+
Where-Object { -not [string]::IsNullOrWhiteSpace($_) }
71+
}
72+
)
73+
if ($runIds.Count -eq 0) {
74+
return $resolved
75+
}
76+
77+
if ([string]::IsNullOrWhiteSpace($Repository)) {
78+
$Repository = $env:GITHUB_REPOSITORY
79+
}
80+
if ([string]::IsNullOrWhiteSpace($Repository)) {
81+
$Repository = "winlibs/winlib-builder"
82+
}
83+
84+
$headers = Get-GitHubApiHeaders
85+
foreach ($runId in $runIds) {
86+
Write-Host "Fetching dependency artifacts from $Repository workflow run $runId"
87+
$artifactsUrl = "https://api.github.com/repos/$Repository/actions/runs/$runId/artifacts?per_page=100"
88+
$response = Invoke-RestMethod -Uri $artifactsUrl -Headers $headers -Method Get
89+
if ($response.total_count -eq 0) {
90+
Write-Warning "No artifacts found for workflow run $runId"
91+
continue
92+
}
93+
94+
foreach ($dep in $Dependencies) {
95+
if ($resolved.ContainsKey($dep)) {
96+
continue
97+
}
98+
99+
$artifactPattern = "^$([regex]::Escape($dep))-\d.*-$([regex]::Escape($vs))-$([regex]::Escape($arch))$"
100+
$matchingArtifacts = @($response.artifacts | Where-Object { $_.name -match $artifactPattern })
101+
$artifact = @($matchingArtifacts | Where-Object { -not $_.expired } | Select-Object -First 1)
102+
103+
if ($artifact.Count -eq 0) {
104+
if ($matchingArtifacts.Count -gt 0) {
105+
Write-Warning "Matching artifact for $dep in workflow run $runId is expired"
106+
}
107+
continue
108+
}
109+
110+
Write-Host "Fetching $($artifact[0].name) from workflow run $runId"
111+
$temp = New-TemporaryFile | Rename-Item -NewName { $_.Name + ".zip" } -PassThru
112+
try {
113+
Invoke-WebRequest -Uri $artifact[0].archive_download_url -Headers $headers -OutFile $temp
114+
Expand-Archive -Path $temp -DestinationPath $Destination -Force
115+
$resolved[$dep] = $artifact[0].name
116+
} finally {
117+
Remove-Item -LiteralPath $temp.FullName -Force -ErrorAction SilentlyContinue
118+
}
119+
}
120+
}
121+
122+
return $resolved
53123
}
54124

55-
$deps = $deps | Where-Object {$needs.Keys -NotContains $_}
56-
if ($deps.Count -gt 0) {
57-
$needs = @{}
58-
$response = Invoke-WebRequest -Uri "https://downloads.php.net/~windows/pecl/deps/packages.txt" -UseBasicParsing
125+
function Resolve-DownloadDependencies {
126+
param (
127+
[String[]] $Dependencies,
128+
[String] $PackagesUrl,
129+
[String] $BaseUrl
130+
)
131+
132+
$resolved = @{}
133+
$response = Invoke-WebRequest -Uri $PackagesUrl -UseBasicParsing
59134
foreach ($line in $response.Content -split "\r?\n") {
60-
foreach ($dep in $deps) {
61-
if ($line -match "$dep-(.+)-$vs-$arch.zip") {
62-
$needs.$dep = $matches[1]
135+
foreach ($dep in $Dependencies) {
136+
if ($resolved.ContainsKey($dep)) {
137+
continue
138+
}
139+
140+
if ($line -match "^$([regex]::Escape($dep))-(.+)-$([regex]::Escape($vs))-$([regex]::Escape($arch))\.zip$") {
141+
$resolved[$dep] = @{
142+
Version = $matches[1];
143+
BaseUrl = $BaseUrl
144+
}
63145
}
64146
}
65147
}
66148

67-
$baseurl = "https://downloads.php.net/~windows/pecl/deps"
68-
foreach ($dep in $needs.GetEnumerator()) {
69-
Write-Output "Fetching $($dep.Name)-$($dep.Value)"
70-
$temp = New-TemporaryFile | Rename-Item -NewName {$_.Name + ".zip"} -PassThru
71-
$url = "$baseurl/$($dep.Name)-$($dep.Value)-$vs-$arch.zip"
72-
Invoke-WebRequest $url -OutFile $temp
73-
Expand-Archive $temp -DestinationPath "deps"
149+
return $resolved
150+
}
151+
152+
New-Item "deps" -ItemType "directory" -Force | Out-Null
153+
154+
$artifactDeps = Resolve-GitHubArtifactDependencies `
155+
-Dependencies $deps `
156+
-RunIds @($workflow_run_ids, $workflow_run_id) `
157+
-Repository $repository `
158+
-Destination "deps"
159+
160+
$deps = $deps | Where-Object { -not $artifactDeps.ContainsKey($_) }
161+
if ($deps.Count -eq 0) {
162+
exit
163+
}
164+
165+
$needs = Resolve-DownloadDependencies `
166+
-Dependencies $deps `
167+
-PackagesUrl "https://downloads.php.net/~windows/php-sdk/deps/series/packages-$version-$vs-$arch-$stability.txt" `
168+
-BaseUrl "https://downloads.php.net/~windows/php-sdk/deps/$vs/$arch"
169+
170+
$deps = $deps | Where-Object {$needs.Keys -NotContains $_}
171+
if ($deps.Count -gt 0) {
172+
$peclNeeds = Resolve-DownloadDependencies `
173+
-Dependencies $deps `
174+
-PackagesUrl "https://downloads.php.net/~windows/pecl/deps/packages.txt" `
175+
-BaseUrl "https://downloads.php.net/~windows/pecl/deps"
176+
177+
foreach ($dep in $peclNeeds.Keys) {
178+
$needs[$dep] = $peclNeeds[$dep]
74179
}
75180
}
76181

77182
$deps = $deps | Where-Object {$needs.Keys -NotContains $_}
78183
if ($deps.Count -gt 0) {
79184
throw "dependencies not found: $deps"
80185
}
186+
187+
foreach ($dep in $needs.Keys) {
188+
$package = $needs[$dep]
189+
$url = "$($package.BaseUrl)/$dep-$($package.Version)-$vs-$arch.zip"
190+
Write-Output "Fetching $url"
191+
$temp = New-TemporaryFile | Rename-Item -NewName {$_.Name + ".zip"} -PassThru
192+
try {
193+
Invoke-WebRequest -Uri $url -OutFile $temp
194+
Expand-Archive -Path $temp -DestinationPath "deps" -Force
195+
} finally {
196+
Remove-Item -LiteralPath $temp.FullName -Force -ErrorAction SilentlyContinue
197+
}
198+
}

0 commit comments

Comments
 (0)