簡體   English   中英

在 ADO 中使用 PowerShell 任務中的 NuGet

[英]Using NuGet from a PowerShell task in ADO

由於多種原因,我需要能夠在 Azure DevOps 管道的 PowerShell 腳本中運行 NuGet。

我已經能夠將 NuGet 放在路徑上並讓可執行文件運行,但我無法讓 NuGet 恢復指定的包。

I thought it might be an authentication issue, but nuget list appears to list the package in question, while nuget restore errors out saying the package doesn't exist.

如果我在本地運行腳本,一切正常; package 按預期恢復。

我的 ADO 管道如下所示:

  - task: NuGetToolInstaller@1
    inputs:
      versionSpec: 5.11.0
  - task: NugetAuthenticate@1
  - task: PowerShell@2
    displayName: 'Run script'
    inputs:
      targetType: 'filePath'
      filePath: $(Build.SourcesDirectory)\my_script.ps1
      workingDirectory: $(Build.SourcesDirectory)

我的 PowerShell 腳本看起來像這樣(為簡潔起見進行了大量編輯):

function RestoreNugetModel($SourceDir, $StagingDir, $Lang, $Version)
{
   Set-Location $StagingDir
   $packageConfig = BuildPackageConfig $StagingDir $Lang $Version

   $CMD = "nuget"
   $arg1 = "restore"
   $arg2 = "-Source"
   $arg3 = "${packageConfig}"
   $arg4 = "-PackagesDirectory"
   $arg5 = Join-Path -Path $SourceDir -ChildPath "src/external"
   $arg6 = "-ConfigFile"
   $arg7 = Join-Path -Path $SourceDir -ChildPath "src/modelcheck.test/nuget.config"
   $arg8 = "-Verbosity"
   $arg9 = "detailed"
   $arg10 = "-NonInteractive"
   $output = & $CMD $arg1 $arg2 $arg3 $arg4 $arg5 $arg6 $arg7 $arg8 $arg9 $arg10
   Write-Host $output -join "`n"
   $exitCode = $LASTEXITCODE
   if ($exitCode -eq 0)
   {
       Write-Host "##[command] Restore Complete"
   }
   else
   {
       Write-Host  "##[error] Restore Failed with ${exitCode}"
   }
   return $exitCode
}

事實證明,由於 nuget 緩存,nuget 命令僅在我的本地計算機上有效。

在我清除緩存后,nuget 命令在我的本地機器上失敗,就像在 ADO 構建機器上一樣,根本原因是我有一個錯誤的 nuget 命令開始; 我不應該指定-Source標志,並且 nuget 命令應該如下所示:

function RestoreNugetModel($SourceDir, $StagingDir, $Lang, $Version)
{
   Set-Location $StagingDir
   $packageConfig = BuildPackageConfig $StagingDir $Lang $Version

   $CMD = "nuget"
   $arg1 = "restore"
   $arg2 = "${packageConfig}"
   $arg3 = "-PackagesDirectory"
   $arg4 = Join-Path -Path $SourceDir -ChildPath "src/external"
   $arg5 = "-ConfigFile"
   $arg6 = Join-Path -Path $SourceDir -ChildPath "src/modelcheck.test/nuget.config"
   $arg7 = "-Verbosity"
   $arg8 = "detailed"
   $arg9 = "-NonInteractive"
   $output = & $CMD $arg1 $arg2 $arg3 $arg4 $arg5 $arg6 $arg7 $arg8 $arg9
   Write-Host $output -join "`n"
   $exitCode = $LASTEXITCODE
   if ($exitCode -eq 0)
   {
       Write-Host "##[command] Restore Complete"
   }
   else
   {
       Write-Host  "##[error] Restore Failed with ${exitCode}"
   }
   return $exitCode
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM