且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-04 10:48:52

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

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

简短的答案是肯定的.

没有开箱即用的任务可以从FTP服务器下载文件.仅 FTP上传任务不能将文件上传到FTP服务器.

There is no out of box task to download the file from FTP server. Only FTP Upload task to upload file to the FTP server not download.

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

So, to resolve it, we could use powershell scripts to connect to FTP server and download files:

类似的脚本:

#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连接到FTP服务器并获取文件.

然后通过任务 PublishBuildArtifacts 将这些下载文件发布到Artifacts.

Then publish those download files to the Artifacts by the task PublishBuildArtifacts.

希望这会有所帮助.