本文介绍了如何将文件夹从TEST应用服务复制到Azure上的LIVE应用服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Azure DevOps管道中,我想复制一个文件夹,例如从一个环境/应用程序服务说测试"到另一个环境/应用程序服务说实时"的媒体.在Ci/cd构建已部署到TEST环境中之后,TEST中的Media文件夹可能会更新-只是为了排除可能建议将其放入Git并将其作为构建工件的答案.

In my Azure DevOps Pipeline I would like to copy a folder e.g. Media from 1 environment/app service, say TEST to another environment/app service say Live. The Media folder in TEST may get updated AFTER the Ci/cd build has been deployed to the TEST environment - just to exclude answers that might suggest putting it in Git and including it as a Build artifact.

编辑-说明使用接受的答案.

EDIT - Clarification on using the accepted answer.

我的仓库在接受的答案中包含给定的powershell脚本:

My repo contains the given powershell script in the accepted answer as :

azure/Copy-Media-Test-To-Live.ps1

然后我将azure文件夹作为工件添加到构建管道中,即

I then add the azure folder as an artifact in the build pipeline i.e.

编辑azure-pipelines.yml并添加:

Edit azure-pipelines.yml and Add:

- task: PublishPipelineArtifact@1 inputs: path: $(System.DefaultWorkingDirectory)/azure/ artifact: azure

- task: PublishPipelineArtifact@1 inputs: path: $(System.DefaultWorkingDirectory)/azure/ artifact: azure

在发布管道中-引用脚本以执行复制:

In the release pipeline - reference the script to perform the copy:

steps:- task: AzurePowerShell@4 displayName: 'Azure PowerShell script: FilePath' inputs: azureSubscription: 'Your subscription ' ScriptPath: '$(System.DefaultWorkingDirectory)/_your-artifact-path/azure/Copy-Media-Test-To-Live.ps1' azurePowerShellVersion: LatestVersion

steps:- task: AzurePowerShell@4 displayName: 'Azure PowerShell script: FilePath' inputs: azureSubscription: 'Your subscription ' ScriptPath: '$(System.DefaultWorkingDirectory)/_your-artifact-path/azure/Copy-Media-Test-To-Live.ps1' azurePowerShellVersion: LatestVersion

推荐答案

可以通过Kudu管理在App Service环境中运行的任何应用程序. Kudu有一个API,可以下载当前已部署到应用程序的任何文件夹的ZIP压缩存档.可以通过GET请求访问它:

Any application running in an App Service Environment can be managed via Kudu. Kudu has an API for downloading a ZIP compressed archive of any folder currently deployed to an application. It can be accessed with a GET request to:

https://{{YOUR-APP-NAME}} .scm.azurewebsites.net/api/zip/site/{{FOLDER}}

https://{{YOUR-APP-NAME}}.scm.azurewebsites.net/api/zip/site/{{FOLDER}}

您可以在PowerShell中使用Invoke-WebRequest cmdlet将这些内容提取到本地存储中.

You can use the Invoke-WebRequest cmdlet in PowerShell to pull this content to local storage.

您确实需要进行身份验证才能使用Kudu API,这在浏览器中很容易,但是在进行自动化时会涉及更多一点.请参阅下面的文章,其中详细介绍了如何检索和提供基本授权标头,以及如何使用命令API通过Invoke-RestMethod cmdlet提取ZIP文件.您的服务主体至少需要对应用程序有贡献者的访问权限,以获取可在API调用中使用的部署凭据.

You do need to authenticate to use the Kudu API, which is easy in a browser, but when automating is a little more involved. Please see the following article which details how to retrieve and present a Basic Authorization header, as well as demonstrating how to use the command API to extract a ZIP file using the Invoke-RestMethod cmdlet. Your service principal will need at least contributor access to your applications to get deployment credentials to use in the API calls.

https://blogs.msdn.microsoft.com/waws/2018/06/26/powershell-script-to-execute-commands-in-scm-website-on-all-instances/

编辑(包括有效的示例脚本):

EDIT (Include worked example script):

如果您有多个订阅,并且在部署运行时环境中未正确设置上下文,则可能需要使用Set-AzContext -Subscription "<SubsciptionName>"设置用于获取WebApp的上下文

If you have multiple subscriptions and the context has not been set properly in the deployment runtime environment, you may need to use Set-AzContext -Subscription "<SubsciptionName>" to set the context for getting the WebApp

$srcResGroupName = "Test"
$srcWebAppName = "tstest12"
$srcDirectory = "/site/wwwroot/myFolder/"

$dstResGroupName = "Test"
$dstWebAppName = "tstest123"
$dstDirectory = "/site/wwwroot/myFolder/"

# Get publishing profile for SOURCE application

$srcWebApp = Get-AzWebApp -Name $srcWebAppName -ResourceGroupName $srcResGroupName
[xml]$publishingProfile = Get-AzWebAppPublishingProfile -WebApp $srcWebApp

# Create Base64 authorization header

$username = $publishingProfile.publishData.publishProfile[0].userName
$password = $publishingProfile.publishData.publishProfile[0].userPWD
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))

$apiBaseUrl = "https://$($srcWebApp.Name).scm.azurewebsites.net/api"

# Download the ZIP file to ./tmp.zip

Invoke-RestMethod -Uri "$apiBaseUrl/zip$($srcDirectory)" `
                    -Headers @{UserAgent="powershell/1.0"; `
                     Authorization=("Basic {0}" -f $base64AuthInfo)} `
                    -Method GET `
                    -OutFile ./tmp.zip

# Get publishing profile for DESTINATION application

$dstWebApp = Get-AzWebApp -Name $dstWebAppName -ResourceGroupName $dstResGroupName
[xml]$publishingProfile = Get-AzWebAppPublishingProfile -WebApp $dstWebApp

# Create Base64 authorization header

$username = $publishingProfile.publishData.publishProfile[0].userName
$password = $publishingProfile.publishData.publishProfile[0].userPWD
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))

$apiBaseUrl = "https://$($dstWebApp.Name).scm.azurewebsites.net/api"

# Upload and extract the ZIP file

Invoke-RestMethod -Uri "$apiBaseUrl/zip$($dstDirectory)" `
                    -Headers @{UserAgent="powershell/1.0"; `
                     Authorization=("Basic {0}" -f $base64AuthInfo)} `
                    -Method PUT `
                    -InFile ./tmp.zip `
                    -ContentType "multipart/form-data"

这篇关于如何将文件夹从TEST应用服务复制到Azure上的LIVE应用服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 22:37