SetupNugetSources.ps1 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. # - task: PowerShell@2
  15. # displayName: Setup Private Feeds Credentials
  16. # condition: eq(variables['Agent.OS'], 'Windows_NT')
  17. # inputs:
  18. # filePath: $(Build.SourcesDirectory)/eng/common/SetupNugetSources.ps1
  19. # arguments: -ConfigFile ${Env:BUILD_SOURCESDIRECTORY}/NuGet.config -Password $Env:Token
  20. # env:
  21. # Token: $(dn-bot-dnceng-artifact-feeds-rw)
  22. [CmdletBinding()]
  23. param (
  24. [Parameter(Mandatory = $true)][string]$ConfigFile,
  25. [Parameter(Mandatory = $true)][string]$Password
  26. )
  27. $ErrorActionPreference = "Stop"
  28. Set-StrictMode -Version 2.0
  29. [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  30. . $PSScriptRoot\tools.ps1
  31. # Add source entry to PackageSources
  32. function AddPackageSource($sources, $SourceName, $SourceEndPoint, $creds, $Username, $Password) {
  33. $packageSource = $sources.SelectSingleNode("add[@key='$SourceName']")
  34. if ($packageSource -eq $null)
  35. {
  36. $packageSource = $doc.CreateElement("add")
  37. $packageSource.SetAttribute("key", $SourceName)
  38. $packageSource.SetAttribute("value", $SourceEndPoint)
  39. $sources.AppendChild($packageSource) | Out-Null
  40. }
  41. else {
  42. Write-Host "Package source $SourceName already present."
  43. }
  44. AddCredential -Creds $creds -Source $SourceName -Username $Username -Password $Password
  45. }
  46. # Add a credential node for the specified source
  47. function AddCredential($creds, $source, $username, $password) {
  48. # Looks for credential configuration for the given SourceName. Create it if none is found.
  49. $sourceElement = $creds.SelectSingleNode($Source)
  50. if ($sourceElement -eq $null)
  51. {
  52. $sourceElement = $doc.CreateElement($Source)
  53. $creds.AppendChild($sourceElement) | Out-Null
  54. }
  55. # Add the <Username> node to the credential if none is found.
  56. $usernameElement = $sourceElement.SelectSingleNode("add[@key='Username']")
  57. if ($usernameElement -eq $null)
  58. {
  59. $usernameElement = $doc.CreateElement("add")
  60. $usernameElement.SetAttribute("key", "Username")
  61. $sourceElement.AppendChild($usernameElement) | Out-Null
  62. }
  63. $usernameElement.SetAttribute("value", $Username)
  64. # Add the <ClearTextPassword> to the credential if none is found.
  65. # Add it as a clear text because there is no support for encrypted ones in non-windows .Net SDKs.
  66. # -> https://github.com/NuGet/Home/issues/5526
  67. $passwordElement = $sourceElement.SelectSingleNode("add[@key='ClearTextPassword']")
  68. if ($passwordElement -eq $null)
  69. {
  70. $passwordElement = $doc.CreateElement("add")
  71. $passwordElement.SetAttribute("key", "ClearTextPassword")
  72. $sourceElement.AppendChild($passwordElement) | Out-Null
  73. }
  74. $passwordElement.SetAttribute("value", $Password)
  75. }
  76. function InsertMaestroPrivateFeedCredentials($Sources, $Creds, $Password) {
  77. $maestroPrivateSources = $Sources.SelectNodes("add[contains(@key,'darc-int')]")
  78. Write-Host "Inserting credentials for $($maestroPrivateSources.Count) Maestro's private feeds."
  79. ForEach ($PackageSource in $maestroPrivateSources) {
  80. Write-Host "`tInserting credential for Maestro's feed:" $PackageSource.Key
  81. AddCredential -Creds $creds -Source $PackageSource.Key -Username $Username -Password $Password
  82. }
  83. }
  84. try {
  85. if (!(Test-Path $ConfigFile -PathType Leaf)) {
  86. Write-PipelineTelemetryError -Category 'Build' -Message "Couldn't find the file NuGet config file: $ConfigFile"
  87. ExitWithExitCode 1
  88. }
  89. # Load NuGet.config
  90. $doc = New-Object System.Xml.XmlDocument
  91. $filename = (Get-Item $ConfigFile).FullName
  92. $doc.Load($filename)
  93. # Get reference to <PackageSources> or create one if none exist already
  94. $sources = $doc.DocumentElement.SelectSingleNode("packageSources")
  95. if ($sources -eq $null) {
  96. $sources = $doc.CreateElement("packageSources")
  97. $doc.DocumentElement.AppendChild($sources) | Out-Null
  98. }
  99. # Looks for a <PackageSourceCredentials> node. Create it if none is found.
  100. $creds = $doc.DocumentElement.SelectSingleNode("packageSourceCredentials")
  101. if ($creds -eq $null) {
  102. $creds = $doc.CreateElement("packageSourceCredentials")
  103. $doc.DocumentElement.AppendChild($creds) | Out-Null
  104. }
  105. # Insert credential nodes for Maestro's private feeds
  106. InsertMaestroPrivateFeedCredentials -Sources $sources -Creds $creds -Password $Password
  107. AddPackageSource -Sources $sources -SourceName "dotnet3-internal" -SourceEndPoint "https://pkgs.dev.azure.com/dnceng/_packaging/dotnet3-internal/nuget/v2" -Creds $creds -Username "dn-bot" -Password $Password
  108. AddPackageSource -Sources $sources -SourceName "dotnet3-internal-transport" -SourceEndPoint "https://pkgs.dev.azure.com/dnceng/_packaging/dotnet3-internal-transport/nuget/v2" -Creds $creds -Username "dn-bot" -Password $Password
  109. $doc.Save($filename)
  110. }
  111. catch {
  112. Write-Host $_.ScriptStackTrace
  113. Write-PipelineTelemetryError -Category 'InitializeToolset' -Message $_
  114. ExitWithExitCode 1
  115. }