[master] Update dependencies from dotnet/arcade dotnet/core-setup dotnet/corefx ...
[mono-project.git] / eng / common / internal-feed-operations.ps1
blob8b8bafd6a896e13d618db7f815fa8615dbd54908
1 param(
2 [Parameter(Mandatory=$true)][string] $Operation,
3 [string] $AuthToken,
4 [string] $CommitSha,
5 [string] $RepoName,
6 [switch] $IsFeedPrivate
9 $ErrorActionPreference = "Stop"
10 Set-StrictMode -Version 2.0
12 . $PSScriptRoot\tools.ps1
14 # Sets VSS_NUGET_EXTERNAL_FEED_ENDPOINTS based on the "darc-int-*" feeds defined in NuGet.config. This is needed
15 # in build agents by CredProvider to authenticate the restore requests to internal feeds as specified in
16 # https://github.com/microsoft/artifacts-credprovider/blob/0f53327cd12fd893d8627d7b08a2171bf5852a41/README.md#environment-variables. This should ONLY be called from identified
17 # internal builds
18 function SetupCredProvider {
19 param(
20 [string] $AuthToken
23 # Install the Cred Provider NuGet plugin
24 Write-Host "Setting up Cred Provider NuGet plugin in the agent..."
25 Write-Host "Getting 'installcredprovider.ps1' from 'https://github.com/microsoft/artifacts-credprovider'..."
27 $url = 'https://raw.githubusercontent.com/microsoft/artifacts-credprovider/master/helpers/installcredprovider.ps1'
29 Write-Host "Writing the contents of 'installcredprovider.ps1' locally..."
30 Invoke-WebRequest $url -OutFile installcredprovider.ps1
32 Write-Host "Installing plugin..."
33 .\installcredprovider.ps1 -Force
35 Write-Host "Deleting local copy of 'installcredprovider.ps1'..."
36 Remove-Item .\installcredprovider.ps1
38 if (-Not("$env:USERPROFILE\.nuget\plugins\netcore")) {
39 Write-Host "CredProvider plugin was not installed correctly!"
40 ExitWithExitCode 1
42 else {
43 Write-Host "CredProvider plugin was installed correctly!"
46 # Then, we set the 'VSS_NUGET_EXTERNAL_FEED_ENDPOINTS' environment variable to restore from the stable
47 # feeds successfully
49 $nugetConfigPath = "$RepoRoot\NuGet.config"
51 if (-Not (Test-Path -Path $nugetConfigPath)) {
52 Write-Host "NuGet.config file not found in repo's root!"
53 ExitWithExitCode 1
56 $endpoints = New-Object System.Collections.ArrayList
57 $nugetConfigPackageSources = Select-Xml -Path $nugetConfigPath -XPath "//packageSources/add[contains(@key, 'darc-int-')]/@value" | foreach{$_.Node.Value}
59 if (($nugetConfigPackageSources | Measure-Object).Count -gt 0 ) {
60 foreach ($stableRestoreResource in $nugetConfigPackageSources) {
61 $trimmedResource = ([string]$stableRestoreResource).Trim()
62 [void]$endpoints.Add(@{endpoint="$trimmedResource"; password="$AuthToken"})
66 if (($endpoints | Measure-Object).Count -gt 0) {
67 # Create the JSON object. It should look like '{"endpointCredentials": [{"endpoint":"http://example.index.json", "username":"optional", "password":"accesstoken"}]}'
68 $endpointCredentials = @{endpointCredentials=$endpoints} | ConvertTo-Json -Compress
70 # Create the environment variables the AzDo way
71 Write-LoggingCommand -Area 'task' -Event 'setvariable' -Data $endpointCredentials -Properties @{
72 'variable' = 'VSS_NUGET_EXTERNAL_FEED_ENDPOINTS'
73 'issecret' = 'false'
76 # We don't want sessions cached since we will be updating the endpoints quite frequently
77 Write-LoggingCommand -Area 'task' -Event 'setvariable' -Data 'False' -Properties @{
78 'variable' = 'NUGET_CREDENTIALPROVIDER_SESSIONTOKENCACHE_ENABLED'
79 'issecret' = 'false'
82 else
84 Write-Host "No internal endpoints found in NuGet.config"
88 #Workaround for https://github.com/microsoft/msbuild/issues/4430
89 function InstallDotNetSdkAndRestoreArcade {
90 $dotnetTempDir = "$RepoRoot\dotnet"
91 $dotnetSdkVersion="2.1.507" # After experimentation we know this version works when restoring the SDK (compared to 3.0.*)
92 $dotnet = "$dotnetTempDir\dotnet.exe"
93 $restoreProjPath = "$PSScriptRoot\restore.proj"
95 Write-Host "Installing dotnet SDK version $dotnetSdkVersion to restore Arcade SDK..."
96 InstallDotNetSdk "$dotnetTempDir" "$dotnetSdkVersion"
98 '<Project Sdk="Microsoft.DotNet.Arcade.Sdk"/>' | Out-File "$restoreProjPath"
100 & $dotnet restore $restoreProjPath
102 Write-Host "Arcade SDK restored!"
104 if (Test-Path -Path $restoreProjPath) {
105 Remove-Item $restoreProjPath
108 if (Test-Path -Path $dotnetTempDir) {
109 Remove-Item $dotnetTempDir -Recurse
113 try {
114 Push-Location $PSScriptRoot
116 if ($Operation -like "setup") {
117 SetupCredProvider $AuthToken
119 elseif ($Operation -like "install-restore") {
120 InstallDotNetSdkAndRestoreArcade
122 else {
123 Write-Host "Unknown operation '$Operation'!"
124 ExitWithExitCode 1
127 catch {
128 Write-Host $_
129 Write-Host $_.Exception
130 Write-Host $_.ScriptStackTrace
131 ExitWithExitCode 1
133 finally {
134 Pop-Location