|
4 | 4 | # https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core |
5 | 5 |
|
6 | 6 | trigger: |
7 | | -- master |
| 7 | + - master |
8 | 8 |
|
| 9 | +# 🎯 使用自托管代理池,加快 CI/CD 速度(与 Senparc.AI / NcfPackageSources 共享同一 Agent Pool 与变量组) |
9 | 10 | pool: |
10 | | - vmImage: 'windows-latest' |
| 11 | + name: 'SenparcWeixinSDK' |
| 12 | + demands: |
| 13 | + - Agent.OS -equals Windows_NT |
11 | 14 |
|
12 | 15 | variables: |
13 | | - solution: '**/tools/Senparc.Weixin.CLI/*.sln' |
14 | | - buildPlatform: 'Any CPU' |
15 | | - buildConfiguration: 'Release' |
| 16 | + - group: Nuget_Key # Library 变量组,内含变量 NUGET_API_KEY |
| 17 | + - name: solution |
| 18 | + value: '**/tools/Senparc.Weixin.CLI/*.sln' |
| 19 | + - name: buildPlatform |
| 20 | + value: 'Any CPU' |
| 21 | + - name: buildConfiguration |
| 22 | + value: 'Release' |
| 23 | + - name: NUGET_PACKAGES |
| 24 | + value: '$(Agent.ToolsDirectory)/nuget/packages' |
| 25 | + - name: NuGetPushSource |
| 26 | + value: 'https://api.nuget.org/v3/index.json' |
16 | 27 |
|
17 | 28 | steps: |
18 | 29 | - task: NuGetToolInstaller@1 |
| 30 | + displayName: 'Use Cached NuGet 6.3.0' |
| 31 | + inputs: |
| 32 | + versionSpec: '6.3.0' |
| 33 | + |
| 34 | +- task: UseDotNet@2 |
| 35 | + displayName: 'Use .NET SDK 10' |
| 36 | + inputs: |
| 37 | + version: '10.0.x' |
| 38 | + packageType: 'sdk' |
| 39 | + performMultiLevelLookup: true |
| 40 | + includePreviewVersions: true |
19 | 41 |
|
20 | 42 | - task: UseDotNet@2 |
21 | 43 | displayName: 'Use .NET SDK 8' |
22 | 44 | inputs: |
23 | 45 | version: '8.0.x' |
24 | 46 | packageType: 'sdk' |
| 47 | + performMultiLevelLookup: true |
| 48 | + includePreviewVersions: true |
25 | 49 |
|
26 | | -# - task: NuGetCommand@2 |
27 | | -# inputs: |
28 | | -# restoreSolution: '$(solution)' |
29 | | - |
| 50 | +- script: | |
| 51 | + echo "=== Installed SDKs ===" |
| 52 | + dotnet --list-sdks |
| 53 | + echo "=== Active SDK ===" |
| 54 | + dotnet --version |
| 55 | + displayName: 'Verify .NET SDKs' |
30 | 56 |
|
31 | | -# - task: VSBuild@1 |
32 | | -# inputs: |
33 | | -# solution: '$(solution)' |
34 | | -# msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:DesktopBuildPackageLocation="$(build.artifactStagingDirectory)\WebApp.zip" /p:DeployIisAppPath="Default Web Site"' |
35 | | -# platform: '$(buildPlatform)' |
36 | | -# configuration: '$(buildConfiguration)' |
| 57 | +# 🟢 缓存 NuGet 依赖项 |
| 58 | +- task: Cache@2 |
| 59 | + inputs: |
| 60 | + key: 'nuget | "$(Agent.OS)" | **/*.csproj' |
| 61 | + path: '$(NUGET_PACKAGES)' |
| 62 | + restoreKeys: 'nuget' |
| 63 | + displayName: 'Cache NuGet Packages' |
37 | 64 |
|
| 65 | +# 🟢 缓存 NuGet 工具(减少安装时间) |
| 66 | +- task: Cache@2 |
| 67 | + inputs: |
| 68 | + key: 'nuget-tool | "$(Agent.OS)"' |
| 69 | + path: 'C:\ProgramData\nuget' |
| 70 | + restoreKeys: 'nuget-tool' |
| 71 | + displayName: 'Cache NuGet Tool' |
38 | 72 |
|
| 73 | +# ✅ .NET Framework 兼容:保留 nuget.exe restore(支持 packages.config / 旧式项目) |
39 | 74 | - task: NuGetCommand@2 |
| 75 | + displayName: 'NuGet restore (.NET Framework compatible)' |
40 | 76 | inputs: |
41 | 77 | command: 'restore' |
42 | 78 | restoreSolution: '$(solution)' |
43 | 79 | feedsToUse: 'config' |
44 | 80 | externalFeedCredentials: 'Nuget Official (V2)' |
45 | | - |
| 81 | + |
| 82 | +# - task: VSBuild@1 |
| 83 | +# inputs: |
| 84 | +# solution: '$(solution)' |
| 85 | +# msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:DesktopBuildPackageLocation="$(build.artifactStagingDirectory)\WebApp.zip" /p:DeployIisAppPath="Default Web Site"' |
| 86 | +# platform: '$(buildPlatform)' |
| 87 | +# configuration: '$(buildConfiguration)' |
| 88 | + |
46 | 89 | - task: DotNetCoreCLI@2 |
47 | 90 | displayName: Build |
48 | 91 | inputs: |
49 | 92 | command: build |
50 | 93 | projects: '$(solution)' |
51 | | - arguments: '--configuration Release' |
| 94 | + arguments: '--configuration $(buildConfiguration)' |
52 | 95 |
|
53 | | -- task: NuGetCommand@2 |
54 | | - displayName: Senparc.Weixin.CLI |
55 | | - inputs: |
56 | | - command: push |
57 | | - packagesToPush: '**/Weixin.*.nupkg' |
58 | | - nuGetFeedType: external |
59 | | - publishFeedCredentials: 'Nuget Official (V2)' |
60 | | - continueOnError: true |
61 | | - condition: succeededOrFailed() |
| 96 | +# 自动发现并推送 Senparc.Weixin.* / Senparc.WebSocket.* / Weixin.* / WeChat.* 的 .nupkg / .snupkg(--skip-duplicate) |
| 97 | +# 凭据:通过 Library 变量组 Nuget_Key 中的机密变量 NUGET_API_KEY 注入 |
| 98 | +- powershell: | |
| 99 | + $ErrorActionPreference = 'Continue' |
| 100 | + $root = "$(Build.SourcesDirectory)" |
| 101 | + $source = "$(NuGetPushSource)" |
| 102 | + $apiKey = $env:NUGET_API_KEY |
| 103 | + if ([string]::IsNullOrWhiteSpace($apiKey)) { |
| 104 | + Write-Host "##[error]缺少机密变量 NUGET_API_KEY:请在 Library 变量组 Nuget_Key 中设置(变量名须为 NUGET_API_KEY,勾选 secret)。" |
| 105 | + exit 2 |
| 106 | + } |
62 | 107 |
|
| 108 | + Add-Type -AssemblyName System.IO.Compression.FileSystem |
63 | 109 |
|
64 | | -- task: NuGetCommand@2 |
65 | | - displayName: Senparc.WeChat.CLI |
66 | | - inputs: |
67 | | - command: push |
68 | | - packagesToPush: '**/WeChat.*.nupkg' |
69 | | - nuGetFeedType: external |
70 | | - publishFeedCredentials: 'Nuget Official (V2)' |
71 | | - continueOnError: true |
72 | | - condition: succeededOrFailed() |
| 110 | + function Get-NupkgMetadataFromZip { |
| 111 | + param([string]$PackagePath) |
| 112 | + try { |
| 113 | + $zip = [System.IO.Compression.ZipFile]::OpenRead($PackagePath) |
| 114 | + try { |
| 115 | + $entry = $zip.Entries | Where-Object { $_.FullName -match '(?i)\.nuspec$' } | Select-Object -First 1 |
| 116 | + if (-not $entry) { |
| 117 | + return @{ Id = ''; Version = '' } |
| 118 | + } |
| 119 | + $r = New-Object System.IO.StreamReader($entry.Open()) |
| 120 | + try { |
| 121 | + $txt = $r.ReadToEnd() |
| 122 | + } finally { $r.Close() } |
| 123 | + $xml = New-Object System.Xml.XmlDocument |
| 124 | + $xml.PreserveWhitespace = $true |
| 125 | + $xml.LoadXml($txt) |
| 126 | + $nsUri = $xml.DocumentElement.NamespaceURI |
| 127 | + $id = $null |
| 128 | + $ver = $null |
| 129 | + if ([string]::IsNullOrEmpty($nsUri)) { |
| 130 | + $id = $xml.package.metadata.id |
| 131 | + $ver = $xml.package.metadata.version |
| 132 | + } else { |
| 133 | + $nsm = New-Object System.Xml.XmlNamespaceManager($xml.NameTable) |
| 134 | + $nsm.AddNamespace('n', $nsUri) |
| 135 | + $meta = $xml.SelectSingleNode('//n:metadata', $nsm) |
| 136 | + if ($meta) { |
| 137 | + $idN = $meta.SelectSingleNode('n:id', $nsm) |
| 138 | + $verN = $meta.SelectSingleNode('n:version', $nsm) |
| 139 | + if ($idN) { $id = $idN.InnerText } |
| 140 | + if ($verN) { $ver = $verN.InnerText } |
| 141 | + } |
| 142 | + } |
| 143 | + return @{ Id = [string]$id; Version = [string]$ver } |
| 144 | + } finally { $zip.Dispose() } |
| 145 | + } catch { |
| 146 | + return @{ Id = ''; Version = '' } |
| 147 | + } |
| 148 | + } |
| 149 | +
|
| 150 | + function Test-WeixinPackageFileName { |
| 151 | + param([string]$FileName) |
| 152 | + return ( |
| 153 | + $FileName.StartsWith('Senparc.Weixin.', [System.StringComparison]::Ordinal) -or |
| 154 | + $FileName.StartsWith('Senparc.WebSocket.', [System.StringComparison]::Ordinal) -or |
| 155 | + $FileName.StartsWith('Weixin.', [System.StringComparison]::Ordinal) -or |
| 156 | + $FileName.StartsWith('WeChat.', [System.StringComparison]::Ordinal) |
| 157 | + ) |
| 158 | + } |
| 159 | +
|
| 160 | + function Get-PackageDisplayName { |
| 161 | + param([string]$PackagePath) |
| 162 | + $meta = Get-NupkgMetadataFromZip $PackagePath |
| 163 | + if (-not [string]::IsNullOrWhiteSpace($meta.Id)) { |
| 164 | + return $meta.Id |
| 165 | + } |
| 166 | + return [System.IO.Path]::GetFileNameWithoutExtension($PackagePath) |
| 167 | + } |
| 168 | +
|
| 169 | + function Push-PackageFile { |
| 170 | + param( |
| 171 | + [string]$PackagePath, |
| 172 | + [string]$PackageKind |
| 173 | + ) |
| 174 | +
|
| 175 | + $pkgId = Get-PackageDisplayName $PackagePath |
| 176 | + $meta = Get-NupkgMetadataFromZip $PackagePath |
| 177 | + $pkgVer = if ([string]::IsNullOrWhiteSpace($meta.Version)) { '' } else { $meta.Version } |
| 178 | +
|
| 179 | + Write-Host ("##[command] dotnet nuget push ""{0}"" --source ""{1}"" --api-key *** --skip-duplicate ({2})" -f $PackagePath, $source, $PackageKind) |
| 180 | +
|
| 181 | + $stdoutPath = Join-Path $env:TEMP ("nuget-push-{0}.out.txt" -f [Guid]::NewGuid().ToString('N')) |
| 182 | + $stderrPath = Join-Path $env:TEMP ("nuget-push-{0}.err.txt" -f [Guid]::NewGuid().ToString('N')) |
| 183 | + try { |
| 184 | + $p = Start-Process -FilePath 'dotnet' -ArgumentList 'nuget', 'push', $PackagePath, '--source', $source, '--api-key', $apiKey, '--skip-duplicate', '--timeout', '3600' -NoNewWindow -Wait -PassThru ` |
| 185 | + -RedirectStandardOutput $stdoutPath -RedirectStandardError $stderrPath |
| 186 | + $out = (Get-Content -LiteralPath $stdoutPath -Raw -ErrorAction SilentlyContinue) |
| 187 | + $err = (Get-Content -LiteralPath $stderrPath -Raw -ErrorAction SilentlyContinue) |
| 188 | + $code = $p.ExitCode |
| 189 | + } finally { |
| 190 | + Remove-Item -LiteralPath $stdoutPath -Force -ErrorAction SilentlyContinue |
| 191 | + Remove-Item -LiteralPath $stderrPath -Force -ErrorAction SilentlyContinue |
| 192 | + } |
73 | 193 |
|
| 194 | + $combined = (@($out, $err) | Where-Object { $_ }) -join "`n" |
| 195 | + $result = 'Failed' |
| 196 | + $reason = '' |
| 197 | +
|
| 198 | + if ($code -eq 0) { |
| 199 | + $lower = if ($combined) { $combined.ToLowerInvariant() } else { '' } |
| 200 | + if ($lower -match 'skip|already exists|duplicate|409') { |
| 201 | + $result = 'Skipped (exists)' |
| 202 | + if ($combined) { |
| 203 | + $one = ($combined.Trim() -replace '\s+', ' ') |
| 204 | + if ($one.Length -gt 200) { $reason = $one.Substring(0, 200) + '...' } else { $reason = $one } |
| 205 | + } |
| 206 | + } else { |
| 207 | + $result = 'Published' |
| 208 | + } |
| 209 | + } else { |
| 210 | + $lower = if ($combined) { $combined.ToLowerInvariant() } else { '' } |
| 211 | + if ($lower -match '409|conflict|already exists|duplicate|cannot publish.*exists') { |
| 212 | + $result = 'Skipped (exists)' |
| 213 | + if ($combined) { |
| 214 | + $one = ($combined.Trim() -replace '\s+', ' ') |
| 215 | + if ($one.Length -gt 200) { $reason = $one.Substring(0, 200) + '...' } else { $reason = $one } |
| 216 | + } |
| 217 | + } else { |
| 218 | + $script:anyUnexpectedFailure = $true |
| 219 | + $result = 'Failed' |
| 220 | + $reason = if ($combined) { ($combined.Trim() -replace '\s+', ' ') } else { ("dotnet exit code {0}" -f $code) } |
| 221 | + if ($reason.Length -gt 400) { $reason = $reason.Substring(0, 400) + '...' } |
| 222 | + } |
| 223 | + } |
| 224 | +
|
| 225 | + return [pscustomobject]@{ |
| 226 | + Result = $result |
| 227 | + PackageId = $pkgId |
| 228 | + Version = $pkgVer |
| 229 | + Kind = $PackageKind |
| 230 | + Path = $PackagePath |
| 231 | + Detail = $reason |
| 232 | + } |
| 233 | + } |
| 234 | +
|
| 235 | + $anyUnexpectedFailure = $false |
| 236 | + $rows = New-Object System.Collections.Generic.List[object] |
| 237 | +
|
| 238 | + $nupkgCandidates = Get-ChildItem -Path $root -Recurse -File -Filter '*.nupkg' -ErrorAction SilentlyContinue | |
| 239 | + Where-Object { Test-WeixinPackageFileName $_.Name } | |
| 240 | + Sort-Object FullName -Unique |
| 241 | +
|
| 242 | + $snupkgCandidates = Get-ChildItem -Path $root -Recurse -File -Filter '*.snupkg' -ErrorAction SilentlyContinue | |
| 243 | + Where-Object { Test-WeixinPackageFileName $_.Name } | |
| 244 | + Sort-Object FullName -Unique |
| 245 | +
|
| 246 | + Write-Host "== WeiXinMPSDK NuGet push: discovered $($nupkgCandidates.Count) .nupkg and $($snupkgCandidates.Count) .snupkg under $root ==" |
| 247 | +
|
| 248 | + foreach ($f in $nupkgCandidates) { |
| 249 | + $rows.Add((Push-PackageFile -PackagePath $f.FullName -PackageKind 'nupkg')) |
| 250 | + } |
| 251 | +
|
| 252 | + foreach ($f in $snupkgCandidates) { |
| 253 | + $rows.Add((Push-PackageFile -PackagePath $f.FullName -PackageKind 'snupkg')) |
| 254 | + } |
| 255 | +
|
| 256 | + Write-Host '' |
| 257 | + Write-Host 'Legend: ✅ Published | ❌ Failed | ⏭️ Skipped (exists)' |
| 258 | + Write-Host '========== NuGet push summary (Result | PackageId | Version | Kind | Path) ==========' |
| 259 | + foreach ($r in $rows) { |
| 260 | + $pathDisp = $r.Path |
| 261 | + if ($pathDisp.Length -gt 120) { $pathDisp = '...' + $pathDisp.Substring([Math]::Max(0, $pathDisp.Length - 117)) } |
| 262 | + $resultDisp = switch ($r.Result) { |
| 263 | + 'Published' { '✅ Published' } |
| 264 | + 'Failed' { '❌ Failed' } |
| 265 | + 'Skipped (exists)' { '⏭️ Skipped (exists)' } |
| 266 | + default { $r.Result } |
| 267 | + } |
| 268 | + Write-Host ('| {0} | {1} | {2} | {3} | {4} |' -f $resultDisp, $r.PackageId, $r.Version, $r.Kind, $pathDisp) |
| 269 | + if ($r.Detail) { |
| 270 | + Write-Host (' Reason: {0}' -f $r.Detail) |
| 271 | + } |
| 272 | + } |
| 273 | + Write-Host '===============================================================================' |
| 274 | + $published = ($rows | Where-Object { $_.Result -eq 'Published' }).Count |
| 275 | + $skipped = ($rows | Where-Object { $_.Result -eq 'Skipped (exists)' }).Count |
| 276 | + $failed = ($rows | Where-Object { $_.Result -eq 'Failed' }).Count |
| 277 | + Write-Host ("Total: {0} | Published: {1} | Skipped: {2} | Failed: {3}" -f $rows.Count, $published, $skipped, $failed) |
| 278 | + Write-Host 'Job exit: 发现与推送循环已执行完毕;若存在非重复性错误则失败。' |
| 279 | +
|
| 280 | + if ($anyUnexpectedFailure) { |
| 281 | + Write-Host "##[error]One or more package pushes failed (see summary table)." |
| 282 | + exit 1 |
| 283 | + } |
| 284 | + exit 0 |
| 285 | + displayName: 'Push WeiXinMPSDK .nupkg/.snupkg (auto-discover, skip duplicates)' |
| 286 | + condition: succeededOrFailed() |
| 287 | + env: |
| 288 | + NUGET_API_KEY: $(NUGET_API_KEY) |
74 | 289 |
|
75 | 290 | # - task: VSTest@2 |
76 | 291 | # inputs: |
|
0 commit comments