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
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.
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.
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.
14 # - task: PowerShell@2
15 # displayName: Setup Private Feeds Credentials
16 # condition: eq(variables['Agent.OS'], 'Windows_NT')
18 # filePath: $(Build.SourcesDirectory)/eng/common/SetupNugetSources.ps1
19 # arguments: -ConfigFile $(Build.SourcesDirectory)/NuGet.config -Password $Env:Token
21 # Token: $(dn-bot-dnceng-artifact-feeds-rw)
25 [Parameter
(Mandatory
= $true)][string
]$ConfigFile,
26 [Parameter
(Mandatory
= $true)][string
]$Password
29 $ErrorActionPreference = "Stop"
30 Set-StrictMode
-Version
2.0
31 [Net
.ServicePointManager
]::SecurityProtocol
= [Net
.SecurityProtocolType
]::Tls12
33 . $PSScriptRoot\tools
.ps1
35 # Add source entry to PackageSources
36 function AddPackageSource
($sources, $SourceName, $SourceEndPoint, $creds, $Username, $Password) {
37 $packageSource = $sources.SelectSingleNode
("add[@key='$SourceName']")
39 if ($packageSource -eq
$null)
41 $packageSource = $doc.CreateElement
("add")
42 $packageSource.SetAttribute
("key", $SourceName)
43 $packageSource.SetAttribute
("value", $SourceEndPoint)
44 $sources.AppendChild
($packageSource) | Out-Null
47 Write-Host "Package source $SourceName already present."
50 AddCredential
-Creds
$creds -Source
$SourceName -Username
$Username -Password
$Password
53 # Add a credential node for the specified source
54 function AddCredential
($creds, $source, $username, $password) {
55 # Looks for credential configuration for the given SourceName. Create it if none is found.
56 $sourceElement = $creds.SelectSingleNode
($Source)
57 if ($sourceElement -eq
$null)
59 $sourceElement = $doc.CreateElement
($Source)
60 $creds.AppendChild
($sourceElement) | Out-Null
63 # Add the <Username> node to the credential if none is found.
64 $usernameElement = $sourceElement.SelectSingleNode
("add[@key='Username']")
65 if ($usernameElement -eq
$null)
67 $usernameElement = $doc.CreateElement
("add")
68 $usernameElement.SetAttribute
("key", "Username")
69 $sourceElement.AppendChild
($usernameElement) | Out-Null
71 $usernameElement.SetAttribute
("value", $Username)
73 # Add the <ClearTextPassword> to the credential if none is found.
74 # Add it as a clear text because there is no support for encrypted ones in non-windows .Net SDKs.
75 # -> https://github.com/NuGet/Home/issues/5526
76 $passwordElement = $sourceElement.SelectSingleNode
("add[@key='ClearTextPassword']")
77 if ($passwordElement -eq
$null)
79 $passwordElement = $doc.CreateElement
("add")
80 $passwordElement.SetAttribute
("key", "ClearTextPassword")
81 $sourceElement.AppendChild
($passwordElement) | Out-Null
83 $passwordElement.SetAttribute
("value", $Password)
86 function InsertMaestroPrivateFeedCredentials
($Sources, $Creds, $Password) {
87 $maestroPrivateSources = $Sources.SelectNodes
("add[contains(@key,'darc-int')]")
89 Write-Host "Inserting credentials for $($maestroPrivateSources.Count) Maestro's private feeds."
91 ForEach
($PackageSource in $maestroPrivateSources) {
92 Write-Host "`tInserting credential for Maestro's feed:" $PackageSource.Key
93 AddCredential
-Creds
$creds -Source
$PackageSource.Key
-Username
$Username -Password
$Password
97 if (!(Test-Path $ConfigFile -PathType Leaf
)) {
98 Write-PipelineTelemetryError
-Category
'Build' -Message
"Eng/common/SetupNugetSources.ps1 returned a non-zero exit code. Couldn't find the NuGet config file: $ConfigFile"
103 Write-PipelineTelemetryError
-Category
'Build' -Message
'Eng/common/SetupNugetSources.ps1 returned a non-zero exit code. Please supply a valid PAT'
108 $doc = New-Object System
.Xml
.XmlDocument
109 $filename = (Get-Item $ConfigFile).FullName
112 # Get reference to <PackageSources> or create one if none exist already
113 $sources = $doc.DocumentElement
.SelectSingleNode
("packageSources")
114 if ($sources -eq
$null) {
115 $sources = $doc.CreateElement
("packageSources")
116 $doc.DocumentElement
.AppendChild
($sources) | Out-Null
119 # Looks for a <PackageSourceCredentials> node. Create it if none is found.
120 $creds = $doc.DocumentElement
.SelectSingleNode
("packageSourceCredentials")
121 if ($creds -eq
$null) {
122 $creds = $doc.CreateElement
("packageSourceCredentials")
123 $doc.DocumentElement
.AppendChild
($creds) | Out-Null
126 # Insert credential nodes for Maestro's private feeds
127 InsertMaestroPrivateFeedCredentials
-Sources
$sources -Creds
$creds -Password
$Password
129 $dotnet3Source = $sources.SelectSingleNode
("add[@key='dotnet3']")
130 if ($dotnet3Source -ne
$null) {
131 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
132 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
135 $dotnet31Source = $sources.SelectSingleNode
("add[@key='dotnet3.1']")
136 if ($dotnet31Source -ne
$null) {
137 AddPackageSource
-Sources
$sources -SourceName
"dotnet3.1-internal" -SourceEndPoint
"https://pkgs.dev.azure.com/dnceng/_packaging/dotnet3.1-internal/nuget/v2" -Creds
$creds -Username
"dn-bot" -Password
$Password
138 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
"dn-bot" -Password
$Password