简体   繁体   中英

Using AzureFileCopy task for selected files

I have a pretty simple pipeline in Azure DevOps. When a commit is made to a development branch, the files in the repo are checked out and then pushed to a blob storage container using the AzureFileCopy task. When I currently run the pipeline all the files in blob show a modified date including ones which were already in the repo.

Our devs have asked if we can change it so only the new or modified files commited to the repo are updated and all other files are not overwritten. I have tried using the overwrite argument set to false but this ignores any changes to the contents of the files.

I have considered using powershell instead but looking for suggestions on the best way to do this?

You could use Azure PowerShell task to run your script to find the new or modified files committed to the repo and then copy them to your blob storage container. The following is the code snippet.

#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
        
    }
}

Another easier workaround is that you could use the PowerShell task to find the new or modified files committed to the repo and then copy them to a new $(Build.SourcesDirectory)/temp folder with corresponding path structure, and then still use the Azure File Copy task to copy files under the $(Build.SourcesDirectory)/temp folder to your blob storage container.

# 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM