如何将文件夹从 TEST 应用服务复制到 Azure 上的实时应用服务
How to Copy a Folder from a TEST App Service to a LIVE App Service on Azure
在我的 Azure DevOps 管道中,我想复制一个文件夹,例如来自 1 environment/app 服务的媒体,说 TEST 到另一个 environment/app 服务说 Live。测试中的媒体文件夹可能会在 Ci/cd 构建部署到测试环境后得到更新 - 只是为了排除可能建议将其放入 Git 并将其作为构建工件包含在内的答案。
编辑 - 关于使用已接受答案的说明。
我的仓库在接受的答案中包含给定的 powershell 脚本:
azure/Copy-Media-Test-To-Live.ps1
然后我将 azure 文件夹添加为构建管道中的工件,即
编辑 azure-pipelines.yml 并添加:
- task: PublishPipelineArtifact@1
inputs:
path: $(System.DefaultWorkingDirectory)/azure/
artifact: azure
在发布管道中-引用脚本执行复制:
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 进行管理。 Kudu 有一个 API 用于下载当前部署到应用程序的任何文件夹的 ZIP 压缩存档。可以通过 GET 请求访问它:
https://{{YOUR-APP-NAME}}.scm.azurewebsites.net/api/zip/site/{{FOLDER}}
您可以在 PowerShell 中使用 Invoke-WebRequest
cmdlet 将此内容提取到本地存储。
您需要进行身份验证才能使用 Kudu API,这在浏览器中很容易,但在自动化时会涉及更多。请参阅以下文章,其中详细介绍了如何检索和提供基本授权 header,以及演示如何使用命令 API 通过 Invoke-RestMethod
cmdlet 提取 ZIP 文件。您的服务主体将至少需要对您的应用程序的贡献者访问权限才能获取部署凭据以在 API 调用中使用。
编辑(包括有效的示例脚本):
如果您有多个订阅,并且在部署运行时环境中没有正确设置上下文,您可能需要使用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 DevOps 管道中,我想复制一个文件夹,例如来自 1 environment/app 服务的媒体,说 TEST 到另一个 environment/app 服务说 Live。测试中的媒体文件夹可能会在 Ci/cd 构建部署到测试环境后得到更新 - 只是为了排除可能建议将其放入 Git 并将其作为构建工件包含在内的答案。
编辑 - 关于使用已接受答案的说明。
我的仓库在接受的答案中包含给定的 powershell 脚本:
azure/Copy-Media-Test-To-Live.ps1
然后我将 azure 文件夹添加为构建管道中的工件,即
编辑 azure-pipelines.yml 并添加:
- task: PublishPipelineArtifact@1
inputs:
path: $(System.DefaultWorkingDirectory)/azure/
artifact: azure
在发布管道中-引用脚本执行复制:
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 进行管理。 Kudu 有一个 API 用于下载当前部署到应用程序的任何文件夹的 ZIP 压缩存档。可以通过 GET 请求访问它:
https://{{YOUR-APP-NAME}}.scm.azurewebsites.net/api/zip/site/{{FOLDER}}
您可以在 PowerShell 中使用 Invoke-WebRequest
cmdlet 将此内容提取到本地存储。
您需要进行身份验证才能使用 Kudu API,这在浏览器中很容易,但在自动化时会涉及更多。请参阅以下文章,其中详细介绍了如何检索和提供基本授权 header,以及演示如何使用命令 API 通过 Invoke-RestMethod
cmdlet 提取 ZIP 文件。您的服务主体将至少需要对您的应用程序的贡献者访问权限才能获取部署凭据以在 API 调用中使用。
编辑(包括有效的示例脚本):
如果您有多个订阅,并且在部署运行时环境中没有正确设置上下文,您可能需要使用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"