【问题标题】:Azure DevOps Pipeline - Power shell script , Copy Files using VariablesAzure DevOps Pipeline - Power shell 脚本,使用变量复制文件
【发布时间】:2019-09-18 13:45:58
【问题描述】:

我正在研究 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)

我将 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)

【问题讨论】:

  • 这是一个 XY 问题。为什么不重定向您的构建输出?如果您从 Visual Studio 构建某些东西,只需传递 MSBuild 参数 /p:OutDir=$(Build.ArtifactStagingDirectory)
  • 我已经在 TFS 中有生产代码。我们正在尝试将代码从 TFS 移动到 Azure DevOps,而不做任何更改。我只想要从 bin 文件夹到其他文件夹的 dll 和 pdb。我可以使用复制任务来做到这一点,但我有很多复制任务,因为我有很多 dll 需要移动。我想要一个运行内联脚本的 power shell 任务。我希望 shell 脚本从变量中读取值并将文件从 A 复制到 B
  • 您刚才所说的与我的评论无关。我正在谈论对您的构建过程进行更改,这显然已经无法正常工作,以将您的构建输出重定向到您想要的位置。它会解决您的问题,而无需任何额外的复制。

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


【解决方案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 默认情况下,新的构建定义仅更新更改的文件。你 可以在 Repository 选项卡上修改文件的下载方式。

所以,这个变量的值是指向代理而不是repos

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

【讨论】:

  • 是的.. 没有任何反应意味着文件没有被复制。我的 bin 文件夹中有 dll 和 pdb。我想要可以在 Power Shell 任务中像内联一样运行的 shell 脚本,以将文件从 A 移动到 B。我在变量中有 A 到 B 的值...
猜你喜欢
  • 1970-01-01
  • 2021-01-03
  • 2021-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-16
  • 1970-01-01
相关资源
最近更新 更多