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

如何在Azure Pipelines(Linux代理)中可靠运行后台Mock服务器?

问题:Azure Pipelines中无法可靠启动并保持Mock服务器运行

我没法在Azure Pipelines的单个步骤里可靠启动Mock服务器,还得让它在后续步骤里持续运行——要么服务器直接关闭,要么当前步骤永远卡着完不成。

背景

我在Azure Pipelines上跑Prism Mock Server(换成Mockoon也有同样问题)已经卡了好几天。我写了个简单的PowerShell脚本,读取包含Port和Path列的CSV文件,批量创建多个Prism Mock服务器供后续流程使用。脚本核心逻辑如下:

foreach($pathAndPortPair in $pathsPortsDictionary.GetEnumerator()) {

  $key = $pathAndPortPair.Name
  $value = $pathAndPortPair.Value
  if (-Not $value.Contains($rootFolder)) {
    $value = Join-Path $rootFolder $value
  }

  if($IsWindows) {
    start-process pwsh.exe -ArgumentList "-noexit -command prism mock -d -p $key $value"
  }
  
  if($IsLinux) {
    ...
  }
}

其中$key是端口号,$value是API规格文件路径。这个脚本在我本地Windows机器上运行正常,但放到Azure Pipelines里就各种出问题,要么服务器启动不了,要么启动后留不住。

我试过以下几种启动服务器的命令,都没解决问题:

  • Start-Process -FilePath "npx" -ArgumentList "prism mock -d -p $key $value":直接跑不起来,报npm相关错误
  • Invoke-Expression "/bin/bash prism mock -d -p $key $value":执行失败,bash报错:

    /usr/local/bin/npx: npx: line 3: syntax error near unexpected token (' /usr/local/bin/npx: line 3: const cli = require('../lib/cli.js')

  • Start-Job -ScriptBlock { prism mock -d -p $key $value }:作业启动后立刻就关闭了,用Get-Job查状态显示已完成,后续调用服务器完全没反应
  • Start-Process -FilePath "prism" -ArgumentList "mock -d -p $key $value":服务器确实启动了,但因为进程在前台运行,当前Pipeline步骤永远结束不了,还收到错误:

    "The STDIO streams did not close within 10 seconds of the exit event from process '/usr/bin/pwsh'. This may indicate a child process inherited the STDIO streams and has not yet exited."

  • start-process pwsh.exe -ArgumentList "-noexit -command prism mock -d -p $key $value":报错找不到文件:

    An error occurred trying to start process 'pwsh.exe' with working directory '/home/vsts/work/1/s'. No such file or directory

有没有其他解决思路?我现在完全没辙了——怎么让这些Mock服务器在后台运行,既能让当前流水线步骤正常结束、启动后续步骤,又不会把服务器终止?

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 21:51:17