azure - 如何将文件夹从测试应用服务复制到 Azure 上的实时应用服务

标签 azure powershell azure-devops copy azure-pipelines-release-pipeline

在我的 Azure DevOps Pipeline 中,我想复制一个文件夹,例如来自 1 个环境/应用服务的媒体(例如“测试”)到另一个环境/应用服务(例如“实时”)。 TEST 中的 Media 文件夹可能会在 Ci/cd 构建部署到 TEST 环境后进行更新 - 只是为了排除可能建议将其放入 Git 并将其作为构建工件包含在内的答案。

编辑 - 关于使用已接受答案的澄清。

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

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

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

编辑 azure-pipelines.yml 并添加:

-任务:PublishPipelineArtifact@1 输入: 路径:$(System.DefaultWorkingDirectory)/azure/ 神器: azure

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

步骤: - 任务:AzurePowerShell@4 显示名称:'Azure PowerShell 脚本:文件路径' 输入: azureSubscription: '您的订阅 ' ScriptPath: '$(System.DefaultWorkingDirectory)/_your-artifact-path/azure/Copy-Media-Test-To-Live.ps1' azurePowerShell版本:最新版本

最佳答案

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

https://{{您的应用程序名称}}.scm.azurewebsites.net/api/zip/site/{{FOLDER}}

您可以使用Invoke-WebRequest PowerShell 中的 cmdlet 将此内容提取到本地存储。

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

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

编辑(包括工作示例脚本):

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

关于azure - 如何将文件夹从测试应用服务复制到 Azure 上的实时应用服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58580956/

相关文章:

windows - Copy-Item 命令在 Powershell 命令行中有效,但在 bat 文件中无效

xcode - 使用 XCode 连接到 Mac 上托管在 VSTS 上的 Git

python - Azure Functions Python 错误 - 没有作业函数

powershell - 无法使用 PowerShell 在 Azure SQL 数据库上设置标签

Powershell 并排对象

html - -Uri -UseBasicParsing powershell

azure - 如何在另一个任务Azure Pipeline中重用一个任务中的DownloadFile

unit-testing - Azure DevOps 管道 : Tests that pass locally fail on the pipeline

azure - 共享日历未显示在 Office 365 的 azure get 日历连接器中

azure - 如何知道我的虚拟机为何重新启动