You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure DevOps发布后,如何通过脚本在Kudu站点替换文件夹文件?

问题描述

背景信息

  • 通过Azure DevOps CI/CD管道部署.NET Core Web应用
  • 发布管道完成后,需要在Azure Web App Kudu站点中将一个文件夹的文件替换到另一个文件夹
    • 源路径:C:\home\License\*.*
    • 目标路径:C:\home\site\wwwroot\wwwroot\License\

错误信息

执行az webapp copy命令时触发报错:

ERROR: 'copy' is misspelled or not recognized by the system.
##[error]Cmd.exe exited with code '2'.
解决方案

Azure CLI并无az webapp copy内置命令,因此出现识别错误。可通过以下几种方式实现Kudu站点内的文件复制替换操作:

方法1:用Azure CLI调用Kudu执行自定义脚本

  1. 在代码仓库中创建copy-license-files.cmd脚本(适配Windows环境),内容如下:
@echo off
echo 开始复制许可证文件...
xcopy "C:\home\License\*.*" "C:\home\site\wwwroot\wwwroot\License\" /Y /E /I
echo 复制完成。
  • /Y:覆盖文件时不弹出确认提示
  • /E:复制所有子目录(包括空目录)
  • /I:若目标路径不存在且复制多文件,自动创建目标目录
  1. 在Azure DevOps发布管道中添加Azure CLI任务,执行命令:
az webapp deploy --resource-group Practice-RG --name basicdotnetwebapp --src-path copy-license-files.cmd --type script --target-path copy-license-files.cmd

该命令会将脚本上传至Web App并触发执行,完成文件复制。

方法2:用Azure PowerShell任务调用Kudu REST API

在Azure DevOps中添加Azure PowerShell任务,运行以下脚本:

$resourceGroupName = "Practice-RG"
$webAppName = "basicdotnetwebapp"
$kuduApiUrl = "https://$webAppName.scm.azurewebsites.net/api/command"

# 获取Web App发布凭据
$publishingCredentials = Get-AzWebAppPublishingProfile -ResourceGroupName $resourceGroupName -Name $webAppName -OutputFile null
$username = $publishingCredentials.Properties.PublishingUserName
$password = $publishingCredentials.Properties.PublishingPassword
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username, $password)))

# 构造复制命令
$command = "xcopy C:\home\License\*.* C:\home\site\wwwroot\wwwroot\License\ /Y /E /I"
$body = @{ command = $command } | ConvertTo-Json

# 调用Kudu API执行命令
Invoke-RestMethod -Uri $kuduApiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method Post -ContentType "application/json" -Body $body

方法3:通过Azure CLI的SSH功能执行命令

若Web App已启用SSH,可直接连接Kudu环境执行复制命令:

az webapp ssh --resource-group Practice-RG --name basicdotnetwebapp --command "xcopy C:\home\License\*.* C:\home\site\wwwroot\wwwroot\License\ /Y /E /I"

注意:如果是Linux环境的Web App,需将xcopy替换为cp -r,路径改为Linux风格(如/home/License/*)。

内容的提问来源于stack exchange,提问作者Vaishnavi Vaishu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 21:18:09