From 610eb4f20d017e5f24e87b6b78aa8bfefbbe5b80 Mon Sep 17 00:00:00 2001 From: shanshanzhong Date: Sun, 9 Nov 2025 04:03:40 -0800 Subject: [PATCH] =?UTF-8?q?feat(CI):=20=E6=B7=BB=E5=8A=A0=20NuGet=20?= =?UTF-8?q?=E5=AE=89=E8=A3=85=E8=84=9A=E6=9C=AC=E5=B9=B6=E4=BC=98=E5=8C=96?= =?UTF-8?q?=20CI=20=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加三个 NuGet 安装脚本(批处理和 PowerShell),支持直接下载和通过 Chocolatey 安装 修改 CI 工作流,优先使用 Chocolatey 安装 NuGet 并更新 PATH 路径 --- .gitea/workflows/docker.yml | 33 +++++++++--------- fix_nuget_choco.bat | 35 +++++++++++++++++++ fix_nuget_ssl.ps1 | 67 +++++++++++++++++++++++++++++++++++++ install_nuget_simple.bat | 39 +++++++++++++++++++++ 4 files changed, 158 insertions(+), 16 deletions(-) create mode 100644 fix_nuget_choco.bat create mode 100644 fix_nuget_ssl.ps1 create mode 100644 install_nuget_simple.bat diff --git a/.gitea/workflows/docker.yml b/.gitea/workflows/docker.yml index 41dd38a..09aa615 100644 --- a/.gitea/workflows/docker.yml +++ b/.gitea/workflows/docker.yml @@ -238,26 +238,27 @@ jobs: $nugetPath = Get-Command nuget -ErrorAction SilentlyContinue if ($nugetPath) { Write-Host "NuGet already installed: $($nugetPath.Source)" + $nugetVersion = nuget help | Select-String -Pattern "NuGet Version" | Select-Object -First 1 + Write-Host "NuGet version: $nugetVersion" return } - # Download NuGet CLI - Write-Host "Downloading NuGet CLI..." - try { - Invoke-WebRequest -Uri "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" -OutFile "C:\nuget.exe" -TimeoutSec 120 - } catch { - Write-Host "Download failed, trying alternative..." - # Try alternative download - Invoke-WebRequest -Uri "https://dist.nuget.org/win-x86-commandline/v6.7.0/nuget.exe" -OutFile "C:\nuget.exe" -TimeoutSec 120 - } + # Use Chocolatey to install NuGet (proven to work) + Write-Host "Installing NuGet via Chocolatey..." + choco install nuget.commandline -y - # Add to PATH - $env:PATH = "C:\;$env:PATH" - Add-Content -Path $env:GITHUB_PATH -Value "C:\" + # Update PATH to include Chocolatey + $env:PATH = "C:\ProgramData\chocolatey\bin;$env:PATH" + Add-Content -Path $env:GITHUB_PATH -Value "C:\ProgramData\chocolatey\bin" # Verify installation - $nugetVersion = C:\nuget.exe help | Select-String -Pattern "NuGet Version" | Select-Object -First 1 - Write-Host "NuGet installed: $nugetVersion" + if (Get-Command nuget -ErrorAction SilentlyContinue) { + Write-Host "NuGet installed successfully via Chocolatey" + $nugetVersion = nuget help | Select-String -Pattern "NuGet Version" | Select-Object -First 1 + Write-Host "NuGet version: $nugetVersion" + } else { + Write-Host "WARNING: NuGet installation may have failed, but continuing..." + } - name: Fix long paths shell: powershell @@ -293,13 +294,13 @@ jobs: - name: Build Windows Debug shell: powershell run: | - $env:PATH = "C:\flutter\bin;C:\;$env:PATH" + $env:PATH = "C:\flutter\bin;C:\ProgramData\chocolatey\bin;$env:PATH" flutter build windows - name: Build Windows Release shell: powershell run: | - $env:PATH = "C:\flutter\bin;C:\;$env:PATH" + $env:PATH = "C:\flutter\bin;C:\ProgramData\chocolatey\bin;$env:PATH" flutter build windows --release - name: Upload Debug build artifacts diff --git a/fix_nuget_choco.bat b/fix_nuget_choco.bat new file mode 100644 index 0000000..b7be1a7 --- /dev/null +++ b/fix_nuget_choco.bat @@ -0,0 +1,35 @@ +@echo off +REM Fix NuGet installation using Chocolatey + +echo Installing NuGet via Chocolatey... + +REM Check if Chocolatey is available +where choco >nul 2>nul +if %errorlevel% neq 0 ( + echo Chocolatey not found, installing Chocolatey first... + powershell -Command "Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))" +) + +echo Installing NuGet via Chocolatey... +choco install nuget.commandline -y + +if %errorlevel% equ 0 ( + echo SUCCESS: NuGet installed via Chocolatey + echo Updating PATH... + set PATH=C:\ProgramData\chocolatey\bin;%PATH% + setx PATH "C:\ProgramData\chocolatey\bin;%PATH%" + + echo Verifying installation... + nuget help | findstr "NuGet" + + if %errorlevel% equ 0 ( + echo NuGet is working correctly! + ) else ( + echo WARNING: NuGet installed but not accessible + ) +) else ( + echo ERROR: NuGet installation failed + exit /b 1 +) + +pause \ No newline at end of file diff --git a/fix_nuget_ssl.ps1 b/fix_nuget_ssl.ps1 new file mode 100644 index 0000000..b9e6847 --- /dev/null +++ b/fix_nuget_ssl.ps1 @@ -0,0 +1,67 @@ +# NuGet 安装脚本 - 解决 SSL 问题 +Write-Host "=== 安装 NuGet ===" -ForegroundColor Green + +# 设置 TLS 1.2 +[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 + +# 检查是否已安装 +$nugetExists = Get-Command nuget -ErrorAction SilentlyContinue +if ($nugetExists) { + Write-Host "NuGet 已安装: $($nugetExists.Source)" -ForegroundColor Green + exit 0 +} + +# 下载 NuGet +Write-Host "下载 NuGet CLI..." -ForegroundColor Yellow +$downloadUrls = @( + "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe", + "https://dist.nuget.org/win-x86-commandline/v6.7.0/nuget.exe", + "http://dist.nuget.org/win-x86-commandline/v6.7.0/nuget.exe" +) + +$downloaded = $false +foreach ($url in $downloadUrls) { + try { + Write-Host "尝试下载: $url" + $webClient = New-Object System.Net.WebClient + $webClient.DownloadFile($url, "C:\nuget.exe") + $downloaded = $true + Write-Host "下载成功!" -ForegroundColor Green + break + } catch { + Write-Host "下载失败: $($_.Exception.Message)" -ForegroundColor Red + continue + } +} + +if (-not $downloaded) { + Write-Host "所有下载都失败了,尝试使用 Chocolatey..." -ForegroundColor Yellow + try { + choco install nuget.commandline -y + if (Get-Command nuget -ErrorAction SilentlyContinue) { + Write-Host "通过 Chocolatey 安装成功!" -ForegroundColor Green + exit 0 + } + } catch { + Write-Host "Chocolatey 安装也失败了: $($_.Exception.Message)" -ForegroundColor Red + } + + Write-Host "NuGet 安装失败,但继续构建..." -ForegroundColor Yellow + exit 0 +} + +# 验证安装 +if (Test-Path "C:\nuget.exe") { + $env:PATH = "C:\;$env:PATH" + Write-Host "NuGet 安装完成" -ForegroundColor Green + + # 测试 + try { + $version = & "C:\nuget.exe" help | Select-String -Pattern "NuGet Version" | Select-Object -First 1 + Write-Host "版本信息: $version" -ForegroundColor Green + } catch { + Write-Host "NuGet 可用,但版本检查失败" -ForegroundColor Yellow + } +} else { + Write-Host "NuGet 文件不存在" -ForegroundColor Red +} \ No newline at end of file diff --git a/install_nuget_simple.bat b/install_nuget_simple.bat new file mode 100644 index 0000000..666ae40 --- /dev/null +++ b/install_nuget_simple.bat @@ -0,0 +1,39 @@ +@echo off +REM Simple NuGet installer for Windows + +echo Installing NuGet... + +REM Check if NuGet exists +where nuget >nul 2>nul +if %errorlevel% == 0 ( + echo NuGet already installed + nuget help | findstr "NuGet" + pause + exit /b 0 +) + +REM Download NuGet using PowerShell +echo Downloading NuGet CLI... +powershell -Command "(New-Object Net.WebClient).DownloadFile('http://dist.nuget.org/win-x86-commandline/v6.7.0/nuget.exe', 'C:\nuget.exe')" + +if %errorlevel% neq 0 ( + echo Download failed, trying alternative... + powershell -Command "(New-Object Net.WebClient).DownloadFile('https://dist.nuget.org/win-x86-commandline/v6.7.0/nuget.exe', 'C:\nuget.exe')" +) + +REM Add to PATH +set PATH=C:\;%PATH% +setx PATH "C:\;%PATH%" + +REM Verify installation +echo Verifying NuGet installation... +C:\nuget.exe help | findstr "NuGet" + +if %errorlevel% equ 0 ( + echo SUCCESS: NuGet installed successfully +) else ( + echo ERROR: NuGet installation failed + exit /b 1 +) + +pause \ No newline at end of file