SetupNugetSources.ps1 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # This file is a temporary workaround for internal builds to be able to restore from private AzDO feeds.
  2. # This file should be removed as part of this issue: https://github.com/dotnet/arcade/issues/4080
  3. #
  4. # What the script does is iterate over all package sources in the pointed NuGet.config and add a credential entry
  5. # under <packageSourceCredentials> for each Maestro managed private feed. Two additional credential
  6. # entries are also added for the two private static internal feeds: dotnet3-internal and dotnet3-internal-transport.
  7. #
  8. # This script needs to be called in every job that will restore packages and which the base repo has
  9. # private AzDO feeds in the NuGet.config.
  10. #
  11. # See example YAML call for this script below. Note the use of the variable `$(dn-bot-dnceng-artifact-feeds-rw)`
  12. # from the AzureDevOps-Artifact-Feeds-Pats variable group.
  13. #
  14. # Any disabledPackageSources entries which start with "darc-int" will be re-enabled as part of this script executing
  15. #
  16. # - task: PowerShell@2
  17. # displayName: Setup Private Feeds Credentials
  18. # condition: eq(variables['Agent.OS'], 'Windows_NT')
  19. # inputs:
  20. # filePath: $(Build.SourcesDirectory)/eng/common/SetupNugetSources.ps1
  21. # arguments: -ConfigFile $(Build.SourcesDirectory)/NuGet.config -Password $Env:Token
  22. # env:
  23. # Token: $(dn-bot-dnceng-artifact-feeds-rw)
  24. [CmdletBinding()]
  25. param (
  26. [Parameter(Mandatory = $true)][string]$ConfigFile,
  27. [Parameter(Mandatory = $true)][string]$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, $Password) {
  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 -Password $Password
  47. }
  48. # Add a credential node for the specified source
  49. function AddCredential($creds, $source, $username, $password) {
  50. # Looks for credential configuration for the given SourceName. Create it if none is found.
  51. $sourceElement = $creds.SelectSingleNode($Source)
  52. if ($sourceElement -eq $null)
  53. {
  54. $sourceElement = $doc.CreateElement($Source)
  55. $creds.AppendChild($sourceElement) | Out-Null
  56. }
  57. # Add the <Username> node to the credential if none is found.
  58. $usernameElement = $sourceElement.SelectSingleNode("add[@key='Username']")
  59. if ($usernameElement -eq $null)
  60. {
  61. $usernameElement = $doc.CreateElement("add")
  62. $usernameElement.SetAttribute("key", "Username")
  63. $sourceElement.AppendChild($usernameElement) | Out-Null
  64. }
  65. $usernameElement.SetAttribute("value", $Username)
  66. # Add the <ClearTextPassword> to the credential if none is found.
  67. # Add it as a clear text because there is no support for encrypted ones in non-windows .Net SDKs.
  68. # -> https://github.com/NuGet/Home/issues/5526
  69. $passwordElement = $sourceElement.SelectSingleNode("add[@key='ClearTextPassword']")
  70. if ($passwordElement -eq $null)
  71. {
  72. $passwordElement = $doc.CreateElement("add")
  73. $passwordElement.SetAttribute("key", "ClearTextPassword")
  74. $sourceElement.AppendChild($passwordElement) | Out-Null
  75. }
  76. $passwordElement.SetAttribute("value", $Password)
  77. }
  78. function InsertMaestroPrivateFeedCredentials($Sources, $Creds, $Username, $Password) {
  79. $maestroPrivateSources = $Sources.SelectNodes("add[contains(@key,'darc-int')]")
  80. Write-Host "Inserting credentials for $($maestroPrivateSources.Count) Maestro's private feeds."
  81. ForEach ($PackageSource in $maestroPrivateSources) {
  82. Write-Host "`tInserting credential for Maestro's feed:" $PackageSource.Key
  83. AddCredential -Creds $creds -Source $PackageSource.Key -Username $Username -Password $Password
  84. }
  85. }
  86. function EnablePrivatePackageSources($DisabledPackageSources) {
  87. $maestroPrivateSources = $DisabledPackageSources.SelectNodes("add[contains(@key,'darc-int')]")
  88. ForEach ($DisabledPackageSource in $maestroPrivateSources) {
  89. Write-Host "`tEnsuring private source '$($DisabledPackageSource.key)' is enabled by deleting it from disabledPackageSource"
  90. # Due to https://github.com/NuGet/Home/issues/10291, we must actually remove the disabled entries
  91. $DisabledPackageSources.RemoveChild($DisabledPackageSource)
  92. }
  93. }
  94. if (!(Test-Path $ConfigFile -PathType Leaf)) {
  95. Write-PipelineTelemetryError -Category 'Build' -Message "Eng/common/SetupNugetSources.ps1 returned a non-zero exit code. Couldn't find the NuGet config file: $ConfigFile"
  96. ExitWithExitCode 1
  97. }
  98. if (!$Password) {
  99. Write-PipelineTelemetryError -Category 'Build' -Message 'Eng/common/SetupNugetSources.ps1 returned a non-zero exit code. Please supply a valid PAT'
  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. # Looks for a <PackageSourceCredentials> node. Create it if none is found.
  113. $creds = $doc.DocumentElement.SelectSingleNode("packageSourceCredentials")
  114. if ($creds -eq $null) {
  115. $creds = $doc.CreateElement("packageSourceCredentials")
  116. $doc.DocumentElement.AppendChild($creds) | Out-Null
  117. }
  118. # Check for disabledPackageSources; we'll enable any darc-int ones we find there
  119. $disabledSources = $doc.DocumentElement.SelectSingleNode("disabledPackageSources")
  120. if ($disabledSources -ne $null) {
  121. Write-Host "Checking for any darc-int disabled package sources in the disabledPackageSources node"
  122. EnablePrivatePackageSources -DisabledPackageSources $disabledSources
  123. }
  124. $userName = "dn-bot"
  125. # Insert credential nodes for Maestro's private feeds
  126. InsertMaestroPrivateFeedCredentials -Sources $sources -Creds $creds -Username $userName -Password $Password
  127. $dotnet31Source = $sources.SelectSingleNode("add[@key='dotnet3.1']")
  128. if ($dotnet31Source -ne $null) {
  129. AddPackageSource -Sources $sources -SourceName "dotnet3.1-internal" -SourceEndPoint "https://pkgs.dev.azure.com/dnceng/_packaging/dotnet3.1-internal/nuget/v2" -Creds $creds -Username $userName -Password $Password
  130. 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 -Password $Password
  131. }
  132. $dotnet5Source = $sources.SelectSingleNode("add[@key='dotnet5']")
  133. if ($dotnet5Source -ne $null) {
  134. AddPackageSource -Sources $sources -SourceName "dotnet5-internal" -SourceEndPoint "https://pkgs.dev.azure.com/dnceng/internal/_packaging/dotnet5-internal/nuget/v2" -Creds $creds -Username $userName -Password $Password
  135. AddPackageSource -Sources $sources -SourceName "dotnet5-internal-transport" -SourceEndPoint "https://pkgs.dev.azure.com/dnceng/internal/_packaging/dotnet5-internal-transport/nuget/v2" -Creds $creds -Username $userName -Password $Password
  136. }
  137. $doc.Save($filename)