Skip to content

Commit 607149c

Browse files
author
Scott Bommarito
authored
Integrate NuGet.Server build with VSTS (#32)
* add Enable-Signing.ps1 and Sign-Packages.ps1 * disable public token verification in enable signing * use cmd to run sn.exe * use & to run sn.exe * find latest version of windows sdk and use that sn.exe * remove unnecessary drop subdirectory check * fix buildqueueurl variable * fix indentation * index parameter fields
1 parent 6fe3329 commit 607149c

2 files changed

Lines changed: 154 additions & 0 deletions

File tree

Enable-Signing.ps1

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
param(
2+
[Parameter(Mandatory=$false)][string]$srcRoot = ".",
3+
[Parameter(Mandatory=$true)][string]$keyFile,
4+
[Parameter(Mandatory=$true)][string]$publicKey,
5+
[Parameter(Mandatory=$false)][string]$publicToken
6+
)
7+
8+
$snSignAssembly = "true"
9+
$delaySignAssembly = "true"
10+
11+
Function GetXmlFile
12+
{
13+
param(
14+
[Parameter(Mandatory=$true)][string]$inputFile
15+
)
16+
17+
return [xml](Get-Content $inputFile)
18+
}
19+
20+
Function MakeNodeWithInnerText
21+
{
22+
param(
23+
[Parameter(Mandatory=$true)][xml]$inputXml,
24+
[Parameter(Mandatory=$true)][string]$newPropertyName,
25+
[Parameter(Mandatory=$true)][string]$newPropertyValue,
26+
[Parameter(Mandatory=$false)][string]$xmlns = ""
27+
)
28+
29+
$newNode=$inputXml.CreateElement($newPropertyName, $xmlns)
30+
$newNode.set_InnerXML($newPropertyValue)
31+
32+
return $newNode
33+
}
34+
35+
Function EnableSNAndDelaySign
36+
{
37+
param(
38+
[Parameter(Mandatory=$true)][xml]$projectXML
39+
)
40+
41+
$xmlNameSpace = $projectXML.Project.xmlns
42+
$newPropertyGroup=$projectXML.CreateElement("PropertyGroup", $xmlNameSpace);
43+
44+
$newKeyFileNode = MakeNodeWithInnerText $projectXML "AssemblyOriginatorKeyFile" $keyFile $xmlNameSpace
45+
$newSignAssemblyNode = MakeNodeWithInnerText $projectXML "SignAssembly" $snSignAssembly $xmlNameSpace
46+
$newDelaySignNode = MakeNodeWithInnerText $projectXML "DelaySign" $delaySignAssembly $xmlNameSpace
47+
48+
$newPropertyGroup.AppendChild($newKeyFileNode)
49+
$newPropertyGroup.AppendChild($newSignAssemblyNode)
50+
$newPropertyGroup.AppendChild($newDelaySignNode)
51+
52+
$projectXml.Project.AppendChild($newPropertyGroup)
53+
}
54+
55+
Function InjectPublicKeyIntoAssemblyInfo
56+
{
57+
param([string]$assemblyInfoFilePath)
58+
59+
$assemblyInfo = Get-Content $assemblyInfoFilePath
60+
61+
$injectedAssemblyInfo = $assemblyInfo -replace '(?<=\[assembly: InternalsVisibleTo\(")(.*)?(?="\)\])', "`${1},PublicKey=$publicKey"
62+
63+
Set-Content $assemblyInfoFilePath $injectedAssemblyInfo
64+
Write-Host "Set public key in $assemblyInfoFilePath"
65+
}
66+
67+
$xmls = "src\NuGet.Server\NuGet.Server.csproj",
68+
"src\NuGet.Server.V2\NuGet.Server.V2.csproj",
69+
"src\NuGet.Server.Core\NuGet.Server.Core.csproj",
70+
"test\NuGet.Server.Tests\NuGet.Server.Tests.csproj",
71+
"test\NuGet.Server.V2.Tests\NuGet.Server.V2.Tests.csproj",
72+
"test\NuGet.Server.Core.Tests\NuGet.Server.Core.Tests.csproj"
73+
74+
$assemblyInfos = "src\NuGet.Server\Properties\AssemblyInfo.cs",
75+
"src\NuGet.Server.V2\Properties\AssemblyInfo.cs",
76+
"src\NuGet.Server.Core\Properties\AssemblyInfo.cs"
77+
78+
foreach ($relXmlPath in $xmls)
79+
{
80+
$xmlFile = Join-Path -Path $srcRoot -ChildPath $relXmlPath -Resolve
81+
82+
if (Test-Path $xmlFile)
83+
{
84+
$xml = GetXmlFile $xmlFile
85+
EnableSNAndDelaySign $xml
86+
87+
echo $xmlFile
88+
89+
$xml.Save($xmlFile)
90+
}
91+
}
92+
93+
foreach ($assemblyInfoFile in $assemblyInfos)
94+
{
95+
if (Test-Path $assemblyInfoFile)
96+
{
97+
InjectPublicKeyIntoAssemblyInfo $assemblyInfoFile
98+
}
99+
}
100+
101+
if (-Not $publicToken) {
102+
exit 0
103+
}
104+
105+
# Find latest version of Windows SDK installed
106+
$sdkRegistry = Get-ItemProperty "hklm:\SOFTWARE\Microsoft\Microsoft SDKs\Windows"
107+
$sdkInstallPath = $sdkRegistry.CurrentInstallFolder
108+
$toolsPath = (Get-ChildItem "$sdkInstallPath\bin" | Select-Object -Last 1).FullName
109+
110+
# Run sn.exe with the public token
111+
& "$toolsPath\sn.exe" "-Vr" "*,$publicToken"
112+
& "$toolsPath\x64\sn.exe" "-Vr" "*,$publicToken"

Sign-Packages.ps1

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[CmdletBinding()]
2+
param (
3+
[string]$Version,
4+
[string]$BuildQueueUrl,
5+
[string]$BuildId,
6+
[string]$DropPath,
7+
[string]$UserName,
8+
[string]$Password
9+
)
10+
11+
# Copy artifacts to share.
12+
$dropSubdirectory = Join-Path "$DropPath" "$Version"
13+
14+
New-Item -path $dropSubdirectory -type Directory
15+
Copy-Item -path "artifacts\packages\*.nupkg" -Destination $dropSubdirectory -verbose -force
16+
17+
# Queue TeamCity build to sign packages
18+
$xml = [xml]@"
19+
<build>
20+
<buildType id="$BuildId"/>
21+
<properties>
22+
<property name="NuGetDropLocation" value="$dropSubdirectory"/>
23+
</properties>
24+
</build>
25+
"@
26+
27+
$securePassword = ConvertTo-SecureString "$Password" -AsPlainText -Force
28+
$TeamCityCredentials = New-Object System.Management.Automation.PSCredential ($UserName, $securePassword)
29+
30+
$out = Invoke-WebRequest -Uri "$BuildQueueUrl" -Credential $TeamCityCredentials -Method POST -Body $xml.OuterXml -ContentType 'application/xml' -UseBasicParsing -Verbose
31+
32+
if (-not $out) {
33+
throw "Failed to push packages to server."
34+
}
35+
36+
if ($out.StatusCode -ne 200) {
37+
Write-Output $out.RawContent
38+
throw "Request failed with StatusCode=$($out.StatusCode)"
39+
}
40+
41+
$rsp = [xml]$out.Content
42+
Write-Output $rsp.OuterXml

0 commit comments

Comments
 (0)