SetupNugetSources.ps1 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # This script adds internal feeds required to build commits that depend on internal package sources. For instance,
  2. # dotnet6-internal would be added automatically if dotnet6 was found in the nuget.config file. In addition also enables
  3. # disabled internal Maestro (darc-int*) feeds.
  4. #
  5. # Optionally, this script also adds a credential entry for each of the internal feeds if supplied.
  6. #
  7. # See example call for this script below.
  8. #
  9. # - task: PowerShell@2
  10. # displayName: Setup Private Feeds Credentials
  11. # condition: eq(variables['Agent.OS'], 'Windows_NT')
  12. # inputs:
  13. # filePath: $(System.DefaultWorkingDirectory)/eng/common/SetupNugetSources.ps1
  14. # arguments: -ConfigFile $(System.DefaultWorkingDirectory)/NuGet.config -Password $Env:Token
  15. # env:
  16. # Token: $(dn-bot-dnceng-artifact-feeds-rw)
  17. #
  18. # Note that the NuGetAuthenticate task should be called after SetupNugetSources.
  19. # This ensures that:
  20. # - Appropriate creds are set for the added internal feeds (if not supplied to the scrupt)
  21. # - The credential provider is installed.
  22. #
  23. # This logic is also abstracted into enable-internal-sources.yml.
  24. [CmdletBinding()]
  25. param (
  26. [Parameter(Mandatory = $true)][string]$ConfigFile,
  27. $Password
  28. )
  29. $ErrorActionPreference = "Stop"
  30. Set-StrictMode -Version 2.0
  31. [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  32. . $PSScriptRoot\tools.ps1
  33. # Add source entry to PackageSources
  34. function AddPackageSource($sources, $SourceName, $SourceEndPoint, $creds, $Username, $pwd) {
  35. $packageSource = $sources.SelectSingleNode("add[@key='$SourceName']")
  36. if ($packageSource -eq $null)
  37. {
  38. $packageSource = $doc.CreateElement("add")
  39. $packageSource.SetAttribute("key", $SourceName)
  40. $packageSource.SetAttribute("value", $SourceEndPoint)
  41. $sources.AppendChild($packageSource) | Out-Null
  42. }
  43. else {
  44. Write-Host "Package source $SourceName already present."
  45. }
  46. AddCredential -Creds $creds -Source $SourceName -Username $Username -pwd $pwd
  47. }
  48. # Add a credential node for the specified source
  49. function AddCredential($creds, $source, $username, $pwd) {
  50. # If no cred supplied, don't do anything.
  51. if (!$pwd) {
  52. return;
  53. }
  54. # Looks for credential configuration for the given SourceName. Create it if none is found.
  55. $sourceElement = $creds.SelectSingleNode($Source)
  56. if ($sourceElement -eq $null)
  57. {
  58. $sourceElement = $doc.CreateElement($Source)
  59. $creds.AppendChild($sourceElement) | Out-Null
  60. }
  61. # Add the <Username> node to the credential if none is found.
  62. $usernameElement = $sourceElement.SelectSingleNode("add[@key='Username']")
  63. if ($usernameElement -eq $null)
  64. {
  65. $usernameElement = $doc.CreateElement("add")
  66. $usernameElement.SetAttribute("key", "Username")
  67. $sourceElement.AppendChild($usernameElement) | Out-Null
  68. }
  69. $usernameElement.SetAttribute("value", $Username)
  70. # Add the <ClearTextPassword> to the credential if none is found.
  71. # Add it as a clear text because there is no support for encrypted ones in non-windows .Net SDKs.
  72. # -> https://github.com/NuGet/Home/issues/5526
  73. $passwordElement = $sourceElement.SelectSingleNode("add[@key='ClearTextPassword']")
  74. if ($passwordElement -eq $null)
  75. {
  76. $passwordElement = $doc.CreateElement("add")
  77. $passwordElement.SetAttribute("key", "ClearTextPassword")
  78. $sourceElement.AppendChild($passwordElement) | Out-Null
  79. }
  80. $passwordElement.SetAttribute("value", $pwd)
  81. }
  82. function InsertMaestroPrivateFeedCredentials($Sources, $Creds, $Username, $pwd) {
  83. $maestroPrivateSources = $Sources.SelectNodes("add[contains(@key,'darc-int')]")
  84. Write-Host "Inserting credentials for $($maestroPrivateSources.Count) Maestro's private feeds."
  85. ForEach ($PackageSource in $maestroPrivateSources) {
  86. Write-Host "`tInserting credential for Maestro's feed:" $PackageSource.Key
  87. AddCredential -Creds $creds -Source $PackageSource.Key -Username $Username -pwd $pwd
  88. }
  89. }
  90. function EnablePrivatePackageSources($DisabledPackageSources) {
  91. $maestroPrivateSources = $DisabledPackageSources.SelectNodes("add[contains(@key,'darc-int')]")
  92. ForEach ($DisabledPackageSource in $maestroPrivateSources) {
  93. Write-Host "`tEnsuring private source '$($DisabledPackageSource.key)' is enabled by deleting it from disabledPackageSource"
  94. # Due to https://github.com/NuGet/Home/issues/10291, we must actually remove the disabled entries
  95. $DisabledPackageSources.RemoveChild($DisabledPackageSource)
  96. }
  97. }
  98. if (!(Test-Path $ConfigFile -PathType Leaf)) {
  99. Write-PipelineTelemetryError -Category 'Build' -Message "Eng/common/SetupNugetSources.ps1 returned a non-zero exit code. Couldn't find the NuGet config file: $ConfigFile"
  100. ExitWithExitCode 1
  101. }
  102. # Load NuGet.config
  103. $doc = New-Object System.Xml.XmlDocument
  104. $filename = (Get-Item $ConfigFile).FullName
  105. $doc.Load($filename)
  106. # Get reference to <PackageSources> or create one if none exist already
  107. $sources = $doc.DocumentElement.SelectSingleNode("packageSources")
  108. if ($sources -eq $null) {
  109. $sources = $doc.CreateElement("packageSources")
  110. $doc.DocumentElement.AppendChild($sources) | Out-Null
  111. }
  112. $creds = $null
  113. if ($Password) {
  114. # Looks for a <PackageSourceCredentials> node. Create it if none is found.
  115. $creds = $doc.DocumentElement.SelectSingleNode("packageSourceCredentials")
  116. if ($creds -eq $null) {
  117. $creds = $doc.CreateElement("packageSourceCredentials")
  118. $doc.DocumentElement.AppendChild($creds) | Out-Null
  119. }
  120. }
  121. # Check for disabledPackageSources; we'll enable any darc-int ones we find there
  122. $disabledSources = $doc.DocumentElement.SelectSingleNode("disabledPackageSources")
  123. if ($disabledSources -ne $null) {
  124. Write-Host "Checking for any darc-int disabled package sources in the disabledPackageSources node"
  125. EnablePrivatePackageSources -DisabledPackageSources $disabledSources
  126. }
  127. $userName = "dn-bot"
  128. # Insert credential nodes for Maestro's private feeds
  129. InsertMaestroPrivateFeedCredentials -Sources $sources -Creds $creds -Username $userName -pwd $Password
  130. # 3.1 uses a different feed url format so it's handled differently here
  131. $dotnet31Source = $sources.SelectSingleNode("add[@key='dotnet3.1']")
  132. if ($dotnet31Source -ne $null) {
  133. AddPackageSource -Sources $sources -SourceName "dotnet3.1-internal" -SourceEndPoint "https://pkgs.dev.azure.com/dnceng/_packaging/dotnet3.1-internal/nuget/v2" -Creds $creds -Username $userName -pwd $Password
  134. AddPackageSource -Sources $sources -SourceName "dotnet3.1-internal-transport" -SourceEndPoint "https://pkgs.dev.azure.com/dnceng/_packaging/dotnet3.1-internal-transport/nuget/v2" -Creds $creds -Username $userName -pwd $Password
  135. }
  136. $dotnetVersions = @('5','6','7','8','9')
  137. foreach ($dotnetVersion in $dotnetVersions) {
  138. $feedPrefix = "dotnet" + $dotnetVersion;
  139. $dotnetSource = $sources.SelectSingleNode("add[@key='$feedPrefix']")
  140. if ($dotnetSource -ne $null) {
  141. AddPackageSource -Sources $sources -SourceName "$feedPrefix-internal" -SourceEndPoint "https://pkgs.dev.azure.com/dnceng/internal/_packaging/$feedPrefix-internal/nuget/v2" -Creds $creds -Username $userName -pwd $Password
  142. AddPackageSource -Sources $sources -SourceName "$feedPrefix-internal-transport" -SourceEndPoint "https://pkgs.dev.azure.com/dnceng/internal/_packaging/$feedPrefix-internal-transport/nuget/v2" -Creds $creds -Username $userName -pwd $Password
  143. }
  144. }
  145. $doc.Save($filename)