


























@@ -18,6 +18,11 @@ on:
1818required: false
1919default: false
2020type: boolean
21+import_ubuntu_wsl2:
22+description: "Import a throwaway Ubuntu WSL2 distro when none is installed"
23+required: false
24+default: false
25+type: boolean
21262227permissions:
2328contents: read
@@ -56,20 +61,68 @@ jobs:
56615762 - name: Probe WSL2
5863id: wsl2
64+env:
65+IMPORT_UBUNTU_WSL2: ${{ inputs.import_ubuntu_wsl2 }}
66+UBUNTU_WSL_ROOTFS_URL: https://cloud-images.ubuntu.com/wsl/releases/24.04/current/ubuntu-noble-wsl-amd64-wsl.rootfs.tar.gz
5967run: |
6068 $ErrorActionPreference = "Continue"
6169 $ok = $false
627071+ function Invoke-WslText {
72+ param([string[]] $Arguments)
73+ $output = & wsl.exe @Arguments 2>&1
74+ $code = $LASTEXITCODE
75+ $text = (($output | ForEach-Object { "$_" }) -join "`n") -replace "`0", ""
76+ [pscustomobject]@{ Code = $code; Text = $text }
77+ }
78+79+ function Get-WslDistros {
80+ $result = Invoke-WslText -Arguments @("--list", "--quiet")
81+ $result.Text -split "\r?\n" |
82+ ForEach-Object { $_.Trim() } |
83+ Where-Object {
84+ $_ -and
85+ $_ -notmatch "Windows Subsystem for Linux has no installed distributions" -and
86+ $_ -notmatch "^Use 'wsl\.exe" -and
87+ $_ -notmatch "^and 'wsl\.exe"
88+ }
89+ }
90+6391 $wsl = Get-Command wsl.exe -ErrorAction SilentlyContinue
6492 if (-not $wsl) {
6593 Write-Warning "wsl.exe is not available on this runner."
6694 } else {
6795 Write-Host "wsl.exe=$($wsl.Source)"
68- wsl.exe --status
69- Write-Host "wsl_status_exit=$LASTEXITCODE"
70- wsl.exe --list --verbose
71- Write-Host "wsl_list_exit=$LASTEXITCODE"
72- wsl.exe --exec bash -lc 'set -euo pipefail; uname -a; if [ -f /etc/os-release ]; then sed -n "1,8p" /etc/os-release; fi'
96+ $status = Invoke-WslText -Arguments @("--status")
97+ Write-Host $status.Text
98+ Write-Host "wsl_status_exit=$($status.Code)"
99+100+ $list = Invoke-WslText -Arguments @("--list", "--verbose")
101+ Write-Host $list.Text
102+ Write-Host "wsl_list_exit=$($list.Code)"
103+104+ $distros = @(Get-WslDistros)
105+ if ($distros.Count -eq 0 -and $env:IMPORT_UBUNTU_WSL2 -eq "true") {
106+ Write-Host "import_ubuntu_wsl2=true"
107+ $wslRoot = "C:\wsl\UbuntuProbe"
108+ $rootfs = "C:\wsl\ubuntu-noble-wsl.rootfs.tar.gz"
109+ New-Item -ItemType Directory -Force -Path @((Split-Path -Parent $rootfs), $wslRoot) | Out-Null
110+ Invoke-WebRequest -Uri $env:UBUNTU_WSL_ROOTFS_URL -OutFile $rootfs -UseBasicParsing
111+ wsl.exe --import UbuntuProbe $wslRoot $rootfs --version 2
112+ Write-Host "wsl_import_exit=$LASTEXITCODE"
113+ $list = Invoke-WslText -Arguments @("--list", "--verbose")
114+ Write-Host $list.Text
115+ Write-Host "wsl_list_after_import_exit=$($list.Code)"
116+ $distros = @(Get-WslDistros)
117+ }
118+119+ if ($distros.Count -gt 0) {
120+ $distro = $distros[0]
121+ Write-Host "wsl_probe_distro=$distro"
122+ wsl.exe -d $distro --exec bash -lc 'set -euo pipefail; uname -a; if [ -f /etc/os-release ]; then sed -n "1,8p" /etc/os-release; fi'
123+ } else {
124+ wsl.exe --exec bash -lc 'set -euo pipefail; uname -a; if [ -f /etc/os-release ]; then sed -n "1,8p" /etc/os-release; fi'
125+ }
73126 if ($LASTEXITCODE -eq 0) {
74127 $ok = $true
75128 }
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。