【问题标题】:Is it possible to download files during the build pipeline on Azure DevOps?是否可以在 Azure DevOps 的构建管道期间下载文件?
【发布时间】:2019-10-31 03:35:22
【问题描述】:

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

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

池: vmImage: 'windows-latest'

变量: 解决方案:'**/*.sln' buildPlatform:'任何 CPU' buildConfiguration: '发布'

步骤: - 任务:NuGetToolInstaller@0

  • 任务:NuGetCommand@2 输入: restoreSolution: '$(solution)'

  • 任务:Npm@1 输入: 命令:“安装” workingDir: 'applicationFolder/app'

  • 任务:VSBuild@1 输入: 解决方案:'$(解决方案)' msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)"' 平台:'$(buildPlatform)' 配置:'$(buildConfiguration)'

  • 任务:PublishBuildArtifacts@1 输入: PathtoPublish: '$(Build.ArtifactStagingDirectory)' ArtifactName: '掉落' 发布位置:'容器'

【问题讨论】:

  • 您要下载图像并将其添加到工件中吗?
  • 正是这个。我们将所有图像放在一个文件夹中。但我们不会将这些图像上传到我们的 git 存储库。我们在 FTP 服务器上也有文件,我想从中下载图像。
  • 您使用 Microsoft 托管代理还是自托管代理?
  • 我正在使用自托管代理。但我可能很快会将其更改为 Microsoft Hosted。
  • 在您的自托管代理中,您可以从文件夹中复制,不是吗?

标签: build azure-devops pipeline devops


【解决方案1】:

是否可以在 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。

希望这会有所帮助。

【讨论】:

  • 你好。我在此脚本上收到以下错误:您不能在空值表达式上调用方法。在 C:\ftptest.ps1:18 char:9 + $reader.ReadLine() + ~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
  • 我现在让它工作了。你知道我如何获取某个文件夹中的所有文件夹和文件吗?
  • 如何使用 PublishBuildArtifacts 将保存在 C:\Folder\Folder1(或任何其他路径)中的文件发布到工件?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多