如何复制在 azure 管道中作为 git diff 的一部分检索的修改后的文件

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

借助 git diff 命令,我们能够获取已修改文件的列表。 获得列表后,我想将修改后的文件复制到开发服务器。谁能帮助我如何将修改后的文件复制到具有相同文件夹结构的服务器。

git azure-pipelines-yaml
1个回答
0
投票

您可以尝试使用 PowerShell 任务执行以下脚本,将修改后的文件从代理计算机上的一个目录(源目录)复制到另一个目录(目标目录)。目标目录可以是代理计算机的本地文件系统中的目录,也可以是可从代理计算机访问的文件共享中的目录。

steps:
- task: PowerShell@2
  displayName: 'Show Infor'
  env:
    SYSTEM_ACCESSTOKEN: $(System.AccessToken)
  inputs:
    pwsh: true
    targetType: inline
    script: |
      Write-Host "Getting the list of modified files from all commits associated with this build..."
      $prefix_url = "$(System.CollectionUri)"
      $project = "$(System.TeamProject)"
      $buildId = $(Build.BuildId)
      $url = "${prefix_url}${project}/_apis/build/builds/${buildId}/changes?includeSourceChange=true&api-version=7.0"
      $headers = @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}
      $response=Invoke-RestMethod -Uri $url -Headers $headers
      $changesCount = $response.count

      $modifiedFiles= git diff HEAD HEAD~$changesCount --name-only

      Write-Host "Start copying the modified files..."
      $srcDir = "{Source directory that contains all the modified files}"
      $destDir = "{Destination directory that the files are copied to}"

      foreach ($file in $modifiedFiles)
      {
          Write-Host "------------------------------"
          $srcFile = "$srcDir/$file"
          $destFile = "$destDir/$file"
          Write-Host "Copying file: $srcFile ---> $destFile"

          $parentDir = Split-Path -Parent $destFile
          if (-not (Test-Path $parentDir))
          {
              # If the directory does not exist, create it.
              New-Item -Path $parentDir -ItemType Directory
          }

          Copy-Item "$srcFile" -Destination "$destFile"
      }

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