自托管Agent重启后,如何继续执行同一Pipeline的后续阶段任务?
问题描述
我的Pipeline包含Stage A和Stage B两个阶段,Stage A中的某任务需安装/卸载驱动并重启系统。自托管Agent重启后已就绪,但触发的构建在10-15分钟后出现报错:##[error]We stopped hearing from agent xxx...,导致跳过后续Stage B。以下是Stage A的YAML配置:
- task: CmdLine@2 displayName: install drivers inputs: verbose: false workingDirectory: $(Pipeline.Workspace)/$(Build.BuildId)/${{ parameters.targetfolder }}/A script: pnputil /add-driver xxx.inf /install failOnStdErr: false continueOnError: true - task: CmdLine@2 displayName: reboot condition: always() inputs: verbose: true script: | echo rebooting the device... shutdown /r /f /t 5
请问是否有办法让Agent重启后继续执行同一构建的后续阶段?
可行解决方案
方法1:使用Agent自带的重启恢复脚本
Azure DevOps自托管Agent提供了专门的重启脚本,能告知服务器Agent即将重启,重启后自动恢复当前作业。Windows环境下直接调用Agent安装目录下的RestartAgent.ps1即可:
- task: PowerShell@2 displayName: Reboot and resume agent condition: always() inputs: targetType: 'inline' script: | # 调用Agent自带的重启脚本,设置超时时间为20分钟(可按需调整) & "$env:AGENT_HOMEDIRECTORY\bin\RestartAgent.ps1" -RestartTimeout 1200
替换原有的shutdown任务后,Agent重启完成会自动衔接未完成的构建,继续执行Stage B。
方法2:延长作业超时时间
如果坚持使用shutdown命令,需要延长作业的超时阈值,避免服务器因长时间未收到心跳而终止作业:
- 在Stage或Job级别添加
timeoutInMinutes参数,设置足够覆盖重启和Agent恢复的时间(比如30分钟):
stages: - stage: StageA jobs: - job: JobA timeoutInMinutes: 30 # 延长作业超时时间 steps: # 驱动安装任务 - task: CmdLine@2 displayName: install drivers inputs: verbose: false workingDirectory: $(Pipeline.Workspace)/$(Build.BuildId)/${{ parameters.targetfolder }}/A script: pnputil /add-driver xxx.inf /install failOnStdErr: false continueOnError: true # 重启任务 - task: CmdLine@2 displayName: reboot condition: always() inputs: verbose: true script: | echo rebooting the device... shutdown /r /f /t 5
- 确认Agent配置中的状态检查间隔(默认1分钟)无需调整,若重启耗时极长,可适当调大
agentstatuscheckinterval参数。
方法3:拆分Pipeline为两个独立任务
如果前两种方法不适用,可将流程拆分为两个Pipeline:
- 第一个Pipeline仅执行驱动安装+重启操作。
- 重启完成后,通过任务触发第二个Pipeline执行Stage B的内容,触发前可添加检查Agent在线状态的步骤,确保Agent就绪后再启动后续流程。
内容的提问来源于stack exchange,提问作者shravya
相关产品推荐
相关产品推荐

