是否可以在 Azure DevOps 上的构建管道期间下载文件?

Is it possible to download files during the build pipeline on Azure DevOps?

我们开始使用 Azure DevOps 来构建和部署我的应用程序。目前,我们不会将应用程序图像上传到我们的存储库。我想知道我是否可以将所有图像下载到将在构建管道期间生成的工件中。

我的 yml 管道: 扳机: - 发展

池: vmImage: 'windows-latest'

变量: 解决方案:'**/*.sln' 构建平台:'Any CPU' 构建配置:'Release'

步骤: - 任务:NuGetToolInstaller@0

Is it possible to download files during the build pipeline on Azure DevOps?

简短的回答是肯定的。

没有从 FTP 服务器下载文件的开箱即用任务。只能FTP Upload task上传文件到FTP服务器不能下载。

因此,要解决它,我们可以使用 powershell 脚本连接到 FTP 服务器并下载文件:

像这样的脚本:

#FTP Server Information - SET VARIABLES
$ftp = "ftp://XXX.com/" 
$user = 'UserName' 
$pass = 'Password'
$folder = 'FTP_Folder'
$target = "C:\Folder\Folder1\"

#SET CREDENTIALS
$credentials = new-object System.Net.NetworkCredential($user, $pass)

function Get-FtpDir ($url,$credentials) {
    $request = [Net.WebRequest]::Create($url)
    $request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
    if ($credentials) { $request.Credentials = $credentials }
    $response = $request.GetResponse()
    $reader = New-Object IO.StreamReader $response.GetResponseStream() 
    while(-not $reader.EndOfStream) {
        $reader.ReadLine()
    }
    #$reader.ReadToEnd()
    $reader.Close()
    $response.Close()
}

#SET FOLDER PATH
$folderPath= $ftp + "/" + $folder + "/"

$files = Get-FTPDir -url $folderPath -credentials $credentials

$files 

$webclient = New-Object System.Net.WebClient 
$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass) 
$counter = 0
foreach ($file in ($files | where {$_ -like "*.txt"})){
    $source=$folderPath + $file  
    $destination = $target + $file 
    $webclient.DownloadFile($source, $target+$file)

    #PRINT FILE NAME AND COUNTER
    $counter++
    $counter
    $source
}

证书来自:PowerShell Connect to FTP server and get files.

然后通过任务PublishBuildArtifacts.

将下载的文件发布到Artifacts

希望对您有所帮助。