Azure DevOps管道 - Powershell脚本,使用变量复制文件

问题描述 投票:0回答:1

我正在使用Azure DevOps Build Pipeline,其中一项任务是将我的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)

我在我的Azure DevOps构建管道中添加了Powershell任务。

我把Type作为内联。

我在下面给出了脚本

$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)
powershell azure-devops azure-pipelines azure-pipelines-build-task
1个回答
-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'

它不会将文件复制到repos。这应该是你没有看到任何事情的原因。

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

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

因此,此变量的值指向代理而不是repos。

注意:powershell中的Copy-Item应该复制文件而不是文件夹,尝试使用*.*来包含文件夹中的所有文件。

© www.soinside.com 2019 - 2024. All rights reserved.