2019-06-19 21:36:02 +00:00
param (
[ Parameter ( Mandatory = $true ) ] [ string ] $Operation ,
[ string ] $AuthToken ,
[ string ] $CommitSha ,
[ string ] $RepoName ,
[ switch ] $IsFeedPrivate
)
2019-11-22 13:41:58 +00:00
$ErrorActionPreference = 'Stop'
2019-06-19 21:36:02 +00:00
Set-StrictMode -Version 2.0
. $PSScriptRoot \ tools . ps1
# Sets VSS_NUGET_EXTERNAL_FEED_ENDPOINTS based on the "darc-int-*" feeds defined in NuGet.config. This is needed
# in build agents by CredProvider to authenticate the restore requests to internal feeds as specified in
# https://github.com/microsoft/artifacts-credprovider/blob/0f53327cd12fd893d8627d7b08a2171bf5852a41/README.md#environment-variables. This should ONLY be called from identified
# internal builds
function SetupCredProvider {
param (
[ string ] $AuthToken
)
# Install the Cred Provider NuGet plugin
2019-11-22 13:41:58 +00:00
Write-Host 'Setting up Cred Provider NuGet plugin in the agent...'
2019-06-19 21:36:02 +00:00
Write-Host " Getting 'installcredprovider.ps1' from 'https://github.com/microsoft/artifacts-credprovider'... "
$url = 'https://raw.githubusercontent.com/microsoft/artifacts-credprovider/master/helpers/installcredprovider.ps1'
Write-Host " Writing the contents of 'installcredprovider.ps1' locally... "
Invoke-WebRequest $url -OutFile installcredprovider . ps1
2019-11-22 13:41:58 +00:00
Write-Host 'Installing plugin...'
2019-06-19 21:36:02 +00:00
. \ installcredprovider . ps1 -Force
Write-Host " Deleting local copy of 'installcredprovider.ps1'... "
Remove-Item . \ installcredprovider . ps1
if ( -Not ( " $env:USERPROFILE \.nuget\plugins\netcore " ) ) {
2019-11-22 13:41:58 +00:00
Write-PipelineTelemetryError -Category 'Arcade' -Message 'CredProvider plugin was not installed correctly!'
2019-06-19 21:36:02 +00:00
ExitWithExitCode 1
}
else {
2019-11-22 13:41:58 +00:00
Write-Host 'CredProvider plugin was installed correctly!'
2019-06-19 21:36:02 +00:00
}
# Then, we set the 'VSS_NUGET_EXTERNAL_FEED_ENDPOINTS' environment variable to restore from the stable
# feeds successfully
$nugetConfigPath = " $RepoRoot \NuGet.config "
if ( -Not ( Test-Path -Path $nugetConfigPath ) ) {
2019-11-22 13:41:58 +00:00
Write-PipelineTelemetryError -Category 'Build' -Message 'NuGet.config file not found in repo root!'
2019-06-19 21:36:02 +00:00
ExitWithExitCode 1
}
$endpoints = New-Object System . Collections . ArrayList
$nugetConfigPackageSources = Select-Xml -Path $nugetConfigPath -XPath " //packageSources/add[contains(@key, 'darc-int-')]/@value " | foreach { $_ . Node . Value }
if ( ( $nugetConfigPackageSources | Measure-Object ) . Count -gt 0 ) {
foreach ( $stableRestoreResource in $nugetConfigPackageSources ) {
$trimmedResource = ( [ string ] $stableRestoreResource ) . Trim ( )
[ void ] $endpoints . Add ( @ { endpoint = " $trimmedResource " ; password = " $AuthToken " } )
}
}
if ( ( $endpoints | Measure-Object ) . Count -gt 0 ) {
2020-07-08 13:11:11 +00:00
# [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Endpoint code example with no real credentials.")]
2019-06-19 21:36:02 +00:00
# Create the JSON object. It should look like '{"endpointCredentials": [{"endpoint":"http://example.index.json", "username":"optional", "password":"accesstoken"}]}'
$endpointCredentials = @ { endpointCredentials = $endpoints } | ConvertTo-Json -Compress
# Create the environment variables the AzDo way
Write-LoggingCommand -Area 'task' -Event 'setvariable' -Data $endpointCredentials -Properties @ {
'variable' = 'VSS_NUGET_EXTERNAL_FEED_ENDPOINTS'
'issecret' = 'false'
}
# We don't want sessions cached since we will be updating the endpoints quite frequently
Write-LoggingCommand -Area 'task' -Event 'setvariable' -Data 'False' -Properties @ {
'variable' = 'NUGET_CREDENTIALPROVIDER_SESSIONTOKENCACHE_ENABLED'
'issecret' = 'false'
}
}
else
{
2019-11-22 13:41:58 +00:00
Write-Host 'No internal endpoints found in NuGet.config'
2019-06-19 21:36:02 +00:00
}
}
#Workaround for https://github.com/microsoft/msbuild/issues/4430
function InstallDotNetSdkAndRestoreArcade {
$dotnetTempDir = " $RepoRoot \dotnet "
$dotnetSdkVersion = " 2.1.507 " # After experimentation we know this version works when restoring the SDK (compared to 3.0.*)
$dotnet = " $dotnetTempDir \dotnet.exe "
$restoreProjPath = " $PSScriptRoot \restore.proj "
Write-Host " Installing dotnet SDK version $dotnetSdkVersion to restore Arcade SDK... "
InstallDotNetSdk " $dotnetTempDir " " $dotnetSdkVersion "
'<Project Sdk="Microsoft.DotNet.Arcade.Sdk"/>' | Out-File " $restoreProjPath "
& $dotnet restore $restoreProjPath
2019-11-22 13:41:58 +00:00
Write-Host 'Arcade SDK restored!'
2019-06-19 21:36:02 +00:00
if ( Test-Path -Path $restoreProjPath ) {
Remove-Item $restoreProjPath
}
if ( Test-Path -Path $dotnetTempDir ) {
Remove-Item $dotnetTempDir -Recurse
}
}
try {
Push-Location $PSScriptRoot
2019-11-22 13:41:58 +00:00
if ( $Operation -like 'setup' ) {
2019-06-19 21:36:02 +00:00
SetupCredProvider $AuthToken
}
2019-11-22 13:41:58 +00:00
elseif ( $Operation -like 'install-restore' ) {
2019-06-19 21:36:02 +00:00
InstallDotNetSdkAndRestoreArcade
}
else {
2019-11-22 13:41:58 +00:00
Write-PipelineTelemetryError -Category 'Arcade' -Message " Unknown operation ' $Operation '! "
2019-06-19 21:36:02 +00:00
ExitWithExitCode 1
}
}
catch {
Write-Host $_ . ScriptStackTrace
2019-11-22 13:41:58 +00:00
Write-PipelineTelemetryError -Category 'Arcade' -Message $_
2019-06-19 21:36:02 +00:00
ExitWithExitCode 1
}
finally {
2019-11-22 13:41:58 +00:00
Pop-Location
2019-06-19 21:36:02 +00:00
}