0

我在 Azure DevOps 中有一个非常简单的管道。当提交到开发分支时,将签出存储库中的文件,然后使用 AzureFileCopy 任务将其推送到 blob 存储容器。当我当前运行管道时,blob 中的所有文件都显示修改日期,包括已经在 repo 中的文件。

我们的开发人员询问我们是否可以更改它,以便仅更新提交到 repo 的新文件或修改过的文件,而不会覆盖所有其他文件。我尝试使用设置为 false 的覆盖参数,但这会忽略对文件内容的任何更改。

我已经考虑使用 powershell 来代替,但正在寻找有关最佳方法的建议?

4

1 回答 1

0

您可以使用Azure PowerShell 任务来运行脚本以查找提交到存储库的新文件或修改后的文件,然后将它们复制到您的 blob 存储容器。以下是代码片段。

#get a list of all files that are part of the commit given SHA
$result=$(git diff-tree --no-commit-id --name-status -r $(Build.SourceVersion)) 

#The result looks like "M   test/hello.txt A    today.txt"
$array=$Result.Split(" ") 

#The arraylooks like "
#M   test/hello.txt 
#A   today.txt"
foreach ($ele in $array)
{
    #Added (A), Copied (C), Deleted (D), Modified (M)
    if ($ele.Contains("M") -eq 0 -Or $ele.Contains("A") -eq 0)
    {
        #filename looks like "test/hello.txt"
        $fileName=$ele.Substring(2)
        $sourcePath="$(Build.SourcesDirectory)" + "\" + $fileName

        #your azcopy code here
        
    }
}

另一个更简单的解决方法是,您可以使用PowerShell 任务找到提交到 repo 的新文件或修改后的文件,然后将它们复制到$(Build.SourcesDirectory)/temp具有相应路径结构的新文件夹中,然后仍然使用Azure 文件复制任务复制文件夹下的$(Build.SourcesDirectory)/temp文件到您的 Blob 存储容器。

# Write your PowerShell commands here.

Write-Host "Hello World"

$result=$(git diff-tree --no-commit-id --name-status -r $(Build.SourceVersion))

$array=$Result.Split(" ")

md $(Build.SourcesDirectory)/temp

foreach ($ele in $array)
{
    if ($ele.Contains("M") -eq 0 -Or $ele.Contains("A") -eq 0)
    {
        $fileName=$ele.Substring(2)
        $source="$(Build.SourcesDirectory)" + "\" + $fileName

        $destination="$(Build.SourcesDirectory)/temp" + "\" + $fileName
        
        New-Item $destination -type file -Force

        Copy-Item -Path $source -Destination $destination
    }
}

#Get-ChildItem -Path $(Build.SourcesDirectory)/temp –Recurse

于 2021-04-08T05:44:48.833 回答