2
[Parameter(Mandatory=$true)][string] $Operation,
6
[switch] $IsFeedPrivate
9
$ErrorActionPreference = 'Stop'
10
Set-StrictMode -Version 2.0
11
. $PSScriptRoot\tools.ps1
13
# Sets VSS_NUGET_EXTERNAL_FEED_ENDPOINTS based on the "darc-int-*" feeds defined in NuGet.config. This is needed
14
# in build agents by CredProvider to authenticate the restore requests to internal feeds as specified in
15
# https://github.com/microsoft/artifacts-credprovider/blob/0f53327cd12fd893d8627d7b08a2171bf5852a41/README.md#environment-variables. This should ONLY be called from identified
17
function SetupCredProvider {
22
# Install the Cred Provider NuGet plugin
23
Write-Host 'Setting up Cred Provider NuGet plugin in the agent...'
24
Write-Host "Getting 'installcredprovider.ps1' from 'https://github.com/microsoft/artifacts-credprovider'..."
26
$url = 'https://raw.githubusercontent.com/microsoft/artifacts-credprovider/master/helpers/installcredprovider.ps1'
28
Write-Host "Writing the contents of 'installcredprovider.ps1' locally..."
29
Invoke-WebRequest $url -OutFile installcredprovider.ps1
31
Write-Host 'Installing plugin...'
32
.\installcredprovider.ps1 -Force
34
Write-Host "Deleting local copy of 'installcredprovider.ps1'..."
35
Remove-Item .\installcredprovider.ps1
37
if (-Not("$env:USERPROFILE\.nuget\plugins\netcore")) {
38
Write-PipelineTelemetryError -Category 'Arcade' -Message 'CredProvider plugin was not installed correctly!'
42
Write-Host 'CredProvider plugin was installed correctly!'
45
# Then, we set the 'VSS_NUGET_EXTERNAL_FEED_ENDPOINTS' environment variable to restore from the stable
48
$nugetConfigPath = Join-Path $RepoRoot "NuGet.config"
50
if (-Not (Test-Path -Path $nugetConfigPath)) {
51
Write-PipelineTelemetryError -Category 'Build' -Message 'NuGet.config file not found in repo root!'
55
$endpoints = New-Object System.Collections.ArrayList
56
$nugetConfigPackageSources = Select-Xml -Path $nugetConfigPath -XPath "//packageSources/add[contains(@key, 'darc-int-')]/@value" | foreach{$_.Node.Value}
58
if (($nugetConfigPackageSources | Measure-Object).Count -gt 0 ) {
59
foreach ($stableRestoreResource in $nugetConfigPackageSources) {
60
$trimmedResource = ([string]$stableRestoreResource).Trim()
61
[void]$endpoints.Add(@{endpoint="$trimmedResource"; password="$AuthToken"})
65
if (($endpoints | Measure-Object).Count -gt 0) {
66
$endpointCredentials = @{endpointCredentials=$endpoints} | ConvertTo-Json -Compress
68
# Create the environment variables the AzDo way
69
Write-LoggingCommand -Area 'task' -Event 'setvariable' -Data $endpointCredentials -Properties @{
70
'variable' = 'VSS_NUGET_EXTERNAL_FEED_ENDPOINTS'
74
# We don't want sessions cached since we will be updating the endpoints quite frequently
75
Write-LoggingCommand -Area 'task' -Event 'setvariable' -Data 'False' -Properties @{
76
'variable' = 'NUGET_CREDENTIALPROVIDER_SESSIONTOKENCACHE_ENABLED'
82
Write-Host 'No internal endpoints found in NuGet.config'
86
#Workaround for https://github.com/microsoft/msbuild/issues/4430
87
function InstallDotNetSdkAndRestoreArcade {
88
$dotnetTempDir = Join-Path $RepoRoot "dotnet"
89
$dotnetSdkVersion="2.1.507" # After experimentation we know this version works when restoring the SDK (compared to 3.0.*)
90
$dotnet = "$dotnetTempDir\dotnet.exe"
91
$restoreProjPath = "$PSScriptRoot\restore.proj"
93
Write-Host "Installing dotnet SDK version $dotnetSdkVersion to restore Arcade SDK..."
94
InstallDotNetSdk "$dotnetTempDir" "$dotnetSdkVersion"
96
'<Project Sdk="Microsoft.DotNet.Arcade.Sdk"/>' | Out-File "$restoreProjPath"
98
& $dotnet restore $restoreProjPath
100
Write-Host 'Arcade SDK restored!'
102
if (Test-Path -Path $restoreProjPath) {
103
Remove-Item $restoreProjPath
106
if (Test-Path -Path $dotnetTempDir) {
107
Remove-Item $dotnetTempDir -Recurse
112
Push-Location $PSScriptRoot
114
if ($Operation -like 'setup') {
115
SetupCredProvider $AuthToken
117
elseif ($Operation -like 'install-restore') {
118
InstallDotNetSdkAndRestoreArcade
121
Write-PipelineTelemetryError -Category 'Arcade' -Message "Unknown operation '$Operation'!"
126
Write-Host $_.ScriptStackTrace
127
Write-PipelineTelemetryError -Category 'Arcade' -Message $_