Azure DevOps Pipeline-Power Shell脚本,使用变量复制文件

时间:2019-04-29 22:48:34

标签: powershell azure-devops azure-pipelines azure-pipelines-build-task

我正在研究Azure DevOps构建管道,其中一项任务是将dll和pdb文件复制到临时文件夹中

Code
  MyProject
     Bin
       Debug
          MyProject.dll
          MyProject.pdb

Staging
   Client
     Libraries

我想使用PowerShell脚本任务,并且正在使用内联脚本。 当我在下面给它时不起作用

Copy-Item $(Build.Repository.LocalPath)\Code\MyProject\Bin\$(DebugBuildConfiguration) 

-Destination $(ClientLibrariesFolder)

下面是我的变量

Variable Name                  Variable Value
StagingFolder              $(Build.Repository.LocalPath)\Staging
DebugBuildConfiguration           Debug
ClientLibrariesFolder        $(StagingFolder)\Client\Libraries

我没有收到任何错误。但是什么也没发生。

解决方案:

我在下面解决了我的问题

我添加了如下新变量

CodeLocalPath : $(Build.Repository.LocalPath)

我将Powershell任务添加到我的Azure DevOps构建管道中。

我将Type设为 Inline

在下面的脚本中,

$destination = "{0}" -f $env:ClientLibrariesFolder

# Copy MyProject.dll to Staging\Client\Libraries
$sourcefolder = "{0}\Code\MyProject\Bin\{1}\MyProject.dll" -f $env:CodeLocalPath, $env:DebugBuildConfiguration
"Source : {0} and Destination : {1} " -f $($sourcefolder), $($destination)
Copy-Item $($sourcefolder) -Destination $($destination)

# Copy MyProject.pdb to Staging\Client\Libraries
$sourcefolder = "{0}\Code\MyProject\Bin\{1}\MyProject.pdb" -f $env:CodeLocalPath, $env:DebugBuildConfiguration
"Source : {0} and Destination : {1} " -f $($sourcefolder), $($destination)
Copy-Item $($sourcefolder) -Destination $($destination)

1 个答案:

答案 0 :(得分:-1)

  

我没有收到任何错误。但是什么也没发生。

您是什么意思“ 但是什么也没发生”?您是说没有文件被复制到您的Repos中吗?

如果是,那是Devops的正确行为。因为不建议您将任何文件上传回您的仓库。

如果您在变量标签中设置了system.debug=true,则会发现以下日志:

##[debug]Copy-Item C:\VS2017Agent\_work\8\s\TestSample\TestSample\Bin\Debug\*.* -Destination C:\VS2017Agent\_work\8\s\TestSample\Staging\Client\Libraries'

它不会将文件复制到存储库。这应该是您什么都看不见的原因。

此外,查看Microsoft's documentation的描述如下:

  

$(Build.Repository.LocalPath)代理上的本地路径   您的源代码文件已下载。例如:c:\ agent_work \ 1 \ s   默认情况下,新的构建定义仅更新更改的文件。您   可以在“存储库”选项卡上修改文件的下载方式。

因此,此变量的值指向的是代理而不是存储库

注意:Powershell中的Copy-Item应该复制文件而不是文件夹,请尝试使用*.*将所有文件包括在文件夹中。