交换天蓝色管道之前,如何将文件从生产环境复制到暂存槽

时间:2020-05-15 09:51:07

标签: azure-pipelines swap

我们有一个网站,该网站具有CMS的上传功能。执行发行时,我们配置的Azure管道将该发行版本部署到暂存插槽,然后交换暂存和生产。但是,所有上载的文件现在都处于暂存阶段,而不是正式生产。我想上传到CDN可以解决此问题,但是目前这不是一种选择。所以我想也许我可以在交换之前将该文件夹下载到暂存槽中。这可能吗,以及有关如何做到的任何想法?

编辑: 这是构建管道:

trigger:
- development
- release

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  projects: '**/*.csproj'
  buildPlatform: 'any cpu'
  buildConfiguration: 'Release'

steps:

- task: UseDotNet@2
  displayName: 'Use .NET Core sdk'
  inputs:
    packageType: sdk
    version: 2.2.401
    installationPath: $(Agent.ToolsDirectory)/dotnet

- task: DotNetCoreCLI@2
  displayName: Restore
  inputs:
    command: 'restore'
    projects: '$(projects)'
    feedsToUse: 'config'
    nugetConfigPath: '$(build.sourcesDirectory)\nuget.config'

- task: DotNetCoreCLI@2
  displayName: Build
  inputs:
    command: 'build'
    projects: '$(solution)'
    arguments: '--configuration $(BuildConfiguration) --no-incremental'

- task: DotNetCoreCLI@2
  displayName: Test
  inputs:
    command: 'test'
    projects: '**/*Tests/*.csproj'
    arguments: '--configuration $(buildConfiguration)'

- task: DotNetCoreCLI@2
  displayName: Publish
  inputs:
    command: publish
    publishWebProjects: True
    arguments: '--configuration $(BuildConfiguration) --output $(build.artifactstagingdirectory)'

- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact'
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'

这是发布管道: Release Pipeline

基本上,在发布到接受插槽之后,有人可以批准通过阶段将发布部署到生产插槽。一旦获得批准,请执行以下步骤。

Azure App Service使用具有以下自定义设置的Web部署进行部署: Azure App Service deploy here

然后使用Azure App Service使用默认选项交换暂存和生产版位

所以我想从生产插槽中下载上载的文件文件夹,并将其上传到登台插槽中的相同文件夹中

编辑2,到目前为止,这是我的脚本。

    param(
        [string]$resourceGroupName,
        [string]$webAppName,
        [string]$appPath="wwwroot",
        [string]$slotName="staging", 
        [string]$kuduPath,
        [string]$localPath,
        [bool]$recursive=$true
    )
    function Get-AzureRmWebAppPublishingCredentials($resourceGroupName, $webAppName, $slotName = $null) {
        if ([string]::IsNullOrWhiteSpace($slotName)) {
            $resourceType = "Microsoft.Web/sites/config"
            $resourceName = "$webAppName/publishingcredentials"
        }
        else {
            $resourceType = "Microsoft.Web/sites/slots/config"
            $resourceName = "$webAppName/$slotName/publishingcredentials"
        }
        $publishingCredentials = Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroupName -ResourceType $resourceType -ResourceName $resourceName -Action list -ApiVersion 2015-08-01 -Force
        Write-Host $publishingCredentials   
        return $publishingCredentials
    }
    function Get-KuduApiAuthorisationHeaderValue($resourceGroupName, $webAppName, $slotName = $null) {
        $publishingCredentials = Get-AzureRmWebAppPublishingCredentials $resourceGroupName $webAppName $slotName
        Write-Host $publishingCredentials.Properties.PublishingUserName
        return ("Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $publishingCredentials.Properties.PublishingUserName, $publishingCredentials.Properties.PublishingPassword))))
    }
    function Get-ApiUrl($webAppName, $appPath, $slotName, $kuduPath, $recursive, $api = "vfs") {
        if ($recursive -eq $true) {
            if (-not ($kuduPath.endswith("recursive=true"))) {
               if (-not ($kuduPath.endswith("/"))) {
                  $kuduPath += "/"
               }
               $kuduPath += "?recursive=true"
            }
        }
        if ($slotName -eq "") {
            $kuduApiUrl = "https://$webAppName.scm.azurewebsites.net/api/$api/site/$appPath/$kuduPath"
        }
        else {
            $kuduApiUrl = "https://$webAppName`-$slotName.scm.azurewebsites.net/api/$api/site/$appPath/$kuduPath"
        }
        Write-Host $kuduApiUrl
        return $kuduApiUrl
    }
    function Get-KuduZipFile($resourceGroupName, $webAppName, $appPath, $slotName, $kuduPath, $recursive, $localPath) {
        $kuduApiAuthorisationToken = Get-KuduApiAuthorisationHeaderValue $resourceGroupName $webAppName $slotName
        $kuduApiUrl = Get-ApiUrl $webAppName, $appPath, $slotName, $kuduPath, $recursive, "zip"
        try
        {
            Invoke-RestMethod -Uri $kuduApiUrl `
                            -Headers @{"Authorization"=$kuduApiAuthorisationToken;"If-Match"="*"} `
                            -Method GET `
                            -OutFile $localPath `
                            -ContentType "multipart/form-data"
        } catch {
            Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__ 
            Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
            if (-not ($_.Exception.Response.StatusCode.value__ -eq 404)) {
                throw $PSItem
            }
        }    
    }
    function Put-KuduZipFile($resourceGroupName, $webAppName, $appPath, $slotName, $kuduPath, $recursive, $localPath){
        $kuduApiAuthorisationToken = Get-KuduApiAuthorisationHeaderValue $resourceGroupName $webAppName $slotName
        $kuduApiUrl = Get-ApiUrl $webAppName $appPath $slotName $kuduPath $recursive "zip"
        try
        {
            Invoke-RestMethod -Uri $kuduApiUrl `
                            -Headers @{"Authorization"=$kuduApiAuthorisationToken;"If-Match"="*"} `
                            -Method PUT `
                            -InFile $localPath `
                            -ContentType "multipart/form-data"
        } catch {
            Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__ 
            Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
            if (-not ($_.Exception.Response.StatusCode.value__ -eq 404)) {
                throw $PSItem
            }
        }    
    }

    Write-Host "resourceGroupName: $resourceGroupName"
    Write-Host "webAppName: $webAppName"
    Write-Host "appPath: $appPath"
    Write-Host "slotName: $slotName"
    Write-Host "kuduPath: $kuduPath"
    Write-Host "localPath: $localPath"
    Write-Host "recursive: $recursive"

    Get-KuduZipFile $resourceGroupName $webAppName $appPath "" $kuduPath $recursive $localPath
    Put-KuduZipFile $resourceGroupName $webAppName $appPath $slotName $kuduPath $recursive $localPath

输出为(删除了一些敏感部分):

resourceGroupName: resourceGroupName
webAppName: webAppName
appPath: wwwroot
slotName: staging
kuduPath: Downloads
localPath: d:\a\r1\a
recursive: True


https://webAppName wwwroot  Downloads True zip-.scm.azurewebsites.net/api/vfs/site//

但是如果我在本地shell中尝试,则会得到以下内容

PS N:\> Get-ApiUrl "webAppName" "appPath" "staging" "kuduPath" $true "zip"
https://webAppName-staging.scm.azurewebsites.net/api/zip/site/appPath/kuduPath/?recursive=true

所以我不明白为什么在pipline中结果是错误的。我在管道中使用以下任务: https://marketplace.visualstudio.com/items?itemName=petergroenewegen.PeterGroenewegen-Xpirit-Vsts-Build-InlinePowershell

0 个答案:

没有答案