Azure DevOps – How to Download Files During Build Pipeline

azure-devopsbuilddevopspipeline

We're starting to use Azure DevOps to build and deploy my application. Currently, we do not upload the application images to our repo. I Would like to know if I could download all the images to the artifact that is going to be generated during the build pipeline.

My yml pipeline :
trigger:
– develop

pool:
vmImage: 'windows-latest'

variables:
solution: '**/*.sln'
buildPlatform: 'Any CPU'
buildConfiguration: 'Release'

steps:
– task: NuGetToolInstaller@0

  • task: NuGetCommand@2
    inputs:
    restoreSolution: '$(solution)'

  • task: Npm@1
    inputs:
    command: 'install'
    workingDir: 'applicationFolder/app'

  • task: VSBuild@1
    inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

  • task: PublishBuildArtifacts@1
    inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'

Best Answer

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

The short answer is yes.

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.

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

Scripts like:

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

Certificate comes from: PowerShell Connect to FTP server and get files.

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

Hope this helps.

Related Question