msbuild

Форк
0
/
internal-feed-operations.ps1 
132 строки · 4.5 Кб
1
param(
2
  [Parameter(Mandatory=$true)][string] $Operation,
3
  [string] $AuthToken,
4
  [string] $CommitSha,
5
  [string] $RepoName,
6
  [switch] $IsFeedPrivate
7
)
8

9
$ErrorActionPreference = 'Stop'
10
Set-StrictMode -Version 2.0
11
. $PSScriptRoot\tools.ps1
12

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
16
# internal builds
17
function SetupCredProvider {
18
  param(
19
    [string] $AuthToken
20
  )    
21

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'..."
25

26
  $url = 'https://raw.githubusercontent.com/microsoft/artifacts-credprovider/master/helpers/installcredprovider.ps1'
27
  
28
  Write-Host "Writing the contents of 'installcredprovider.ps1' locally..."
29
  Invoke-WebRequest $url -OutFile installcredprovider.ps1
30
  
31
  Write-Host 'Installing plugin...'
32
  .\installcredprovider.ps1 -Force
33
  
34
  Write-Host "Deleting local copy of 'installcredprovider.ps1'..."
35
  Remove-Item .\installcredprovider.ps1
36

37
  if (-Not("$env:USERPROFILE\.nuget\plugins\netcore")) {
38
    Write-PipelineTelemetryError -Category 'Arcade' -Message 'CredProvider plugin was not installed correctly!'
39
    ExitWithExitCode 1  
40
  } 
41
  else {
42
    Write-Host 'CredProvider plugin was installed correctly!'
43
  }
44

45
  # Then, we set the 'VSS_NUGET_EXTERNAL_FEED_ENDPOINTS' environment variable to restore from the stable 
46
  # feeds successfully
47

48
  $nugetConfigPath = Join-Path $RepoRoot "NuGet.config"
49

50
  if (-Not (Test-Path -Path $nugetConfigPath)) {
51
    Write-PipelineTelemetryError -Category 'Build' -Message 'NuGet.config file not found in repo root!'
52
    ExitWithExitCode 1
53
  }
54
  
55
  $endpoints = New-Object System.Collections.ArrayList
56
  $nugetConfigPackageSources = Select-Xml -Path $nugetConfigPath -XPath "//packageSources/add[contains(@key, 'darc-int-')]/@value" | foreach{$_.Node.Value}
57
  
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"}) 
62
    }
63
  }
64

65
  if (($endpoints | Measure-Object).Count -gt 0) {
66
      $endpointCredentials = @{endpointCredentials=$endpoints} | ConvertTo-Json -Compress
67

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'
71
        'issecret' = 'false'
72
      } 
73

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'
77
        'issecret' = 'false'
78
      } 
79
  }
80
  else
81
  {
82
    Write-Host 'No internal endpoints found in NuGet.config'
83
  }
84
}
85

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"
92
  
93
  Write-Host "Installing dotnet SDK version $dotnetSdkVersion to restore Arcade SDK..."
94
  InstallDotNetSdk "$dotnetTempDir" "$dotnetSdkVersion"
95
  
96
  '<Project Sdk="Microsoft.DotNet.Arcade.Sdk"/>' | Out-File "$restoreProjPath"
97

98
  & $dotnet restore $restoreProjPath
99

100
  Write-Host 'Arcade SDK restored!'
101

102
  if (Test-Path -Path $restoreProjPath) {
103
    Remove-Item $restoreProjPath
104
  }
105

106
  if (Test-Path -Path $dotnetTempDir) {
107
    Remove-Item $dotnetTempDir -Recurse
108
  }
109
}
110

111
try {
112
  Push-Location $PSScriptRoot
113

114
  if ($Operation -like 'setup') {
115
    SetupCredProvider $AuthToken
116
  } 
117
  elseif ($Operation -like 'install-restore') {
118
    InstallDotNetSdkAndRestoreArcade
119
  }
120
  else {
121
    Write-PipelineTelemetryError -Category 'Arcade' -Message "Unknown operation '$Operation'!"
122
    ExitWithExitCode 1  
123
  }
124
} 
125
catch {
126
  Write-Host $_.ScriptStackTrace
127
  Write-PipelineTelemetryError -Category 'Arcade' -Message $_
128
  ExitWithExitCode 1
129
} 
130
finally {
131
  Pop-Location
132
}
133

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.