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

Azure Release Pipeline未清理代理Package文件夹问题求助

清理Azure发布管道中IIS部署的临时文件夹

下面提供几种强制清理相关目录的方案,按需选择:

方案1:清理IIS站点的物理目录

在停止站点后、部署前,用PowerShell任务清空目标站点的物理文件夹:

  • 在StopIIS任务之后、DeployIIS任务之前添加以下PowerShell任务:
# 替换成你的IIS站点实际物理路径,比如C:\inetpub\wwwroot\MyAspNetApp
$targetPath = "C:\inetpub\wwwroot\${{ variables.IISWebsiteName }}"
if (Test-Path $targetPath) {
    # 递归删除目录下所有内容,-Force处理只读文件
    Remove-Item "$targetPath\*" -Recurse -Force
    Write-Host "已清空目标站点目录: $targetPath"
} else {
    Write-Host "目标目录不存在,跳过清理"
}

方案2:通过部署任务参数自动清理

IISWebAppDeploymentOnMachineGroup任务支持通过额外参数禁用默认的“保留服务器额外文件”规则,自动清理站点目录:
在DeployIIS任务的inputs里添加一行:

AdditionalArguments: '-disableRule:SkipExtraFilesOnServer'

默认情况下,这个任务会保留服务器上不属于部署包的文件,添加该参数后会删除这些额外文件,只保留部署包里的内容。

方案3:清理代理上的临时下载目录

如果要清理代理机器上的临时下载文件夹($(Pipeline.Workspace)下的目录),可以在下载构建产物之前或者部署完成后添加脚本任务:

- script: |
    if exist "$(Pipeline.Workspace)\buildPipeline\My Asp Net App" (
        rmdir /s /q "$(Pipeline.Workspace)\buildPipeline\My Asp Net App"
        echo 已清理代理临时下载目录
    ) else (
        echo 代理临时目录不存在,跳过清理
    )
  displayName: '清理代理旧临时目录'

修改后的完整管道配置

trigger:
- none

variables:
    IISWebsiteName: 'MyAspNetApp'

resources:
 pipelines:
     - pipeline: 'buildPipeline'
       project: 'My Asp Net App'
       source: 'Build pipeline'
       branch: 'main'
       

stages:
 - stage: 'DeployeWebSite'
   displayName: 'Deploy website'
   pool:
     vmImage: windows-latest
    
   jobs:
   - deployment: DeployWebsite
     displayName: 'Deploy website'
     environment: 'dev-web01.VMI835964'
     strategy:
      runOnce:
          deploy:
           steps:
               - checkout: none

               # 可选:清理代理上的旧临时目录
               - script: |
                   if exist "$(Pipeline.Workspace)\buildPipeline\My Asp Net App" (
                       rmdir /s /q "$(Pipeline.Workspace)\buildPipeline\My Asp Net App"
                       echo 已清理代理临时目录
                   ) else (
                       echo 代理临时目录不存在,跳过清理
                   )
                 displayName: '清理代理旧临时目录'

               - download: 'buildPipeline'
                 name: 'DownloadBuildArtifacts'           
                 displayName: 'Download build artifacts'
                 artifact: 'My Asp Net App'
              
               - task: IISWebAppManagementOnMachineGroup@0
                 name: 'StopIIS'
                 displayName: 'Stop IIS website - ${{ variables.IISWebsiteName }}'
                 inputs:
                    IISDeploymentType: 'IISWebsite'
                    ActionIISWebsite: 'StopWebsite'
                    StartStopWebsiteName: '${{ variables.IISWebsiteName }}'         

               # 清理IIS站点物理目录
               - task: PowerShell@2
                 displayName: '清理IIS站点物理目录'
                 inputs:
                    targetType: 'inline'
                    script: |
                        $targetPath = "C:\inetpub\wwwroot\${{ variables.IISWebsiteName }}"
                        if (Test-Path $targetPath) {
                            Remove-Item "$targetPath\*" -Recurse -Force
                            Write-Host "已清空目标站点目录: $targetPath"
                        } else {
                            Write-Host "目标目录不存在,跳过清理"
                        }
                  
               - script: echo '$(Pipeline.Workspace)\buildPipeline\My Asp Net App'
                            
               - task: IISWebAppDeploymentOnMachineGroup@0
                 name: 'DeployIIS'
                 displayName: 'Deploy IIS website - ${{ variables.IISWebsiteName }}'         
                 inputs:
                    WebSiteName: '${{ variables.IISWebsiteName }}'             
                    Package: '$(Pipeline.Workspace)\buildPipeline\My Asp Net App'
                    TakeAppOfflineFlag: true
                    # 启用自动清理服务器额外文件
                    AdditionalArguments: '-disableRule:SkipExtraFilesOnServer'
            
               - task: IISWebAppManagementOnMachineGroup@0
                 name: 'StartIIS'
                 displayName: 'Start IIS website - ${{ variables.IISWebsiteName }}'               
                 inputs:
                    IISDeploymentType: 'IISWebsite'
                    ActionIISWebsite: 'StartWebsite'
                    StartStopWebsiteName: '${{ variables.IISWebsiteName }}'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 08:12:27