Azure DevOps Server:初始化作业时如何防止 'Access to path is denied'?

Azure DevOps Server: How to prevent 'Access to path is denied' when initalizing a job?

我正在使用 Azure DevOps Server 2019(简称 ADOS,安装在本地)运行 基于 C# 中的 Selenium 的端到端测试。

时不时地,如果没有正确停止我的 Selenium 测试使用的 chromedriver.exe 进程,构建就会严重结束。因此,后续构建在 Initialize Job 步骤失败,错误如下所示:

One or more errors occurred. (Access to path 'C:...\chromedriver.exe' is denied.)

据我了解,因为 chromedriver.exe 仍然是 运行ning ADOS 在构建开始时尝试清理工作文件夹时无法删除其 .exe 文件运行.

我试过禁用 Get sources 步骤定义的 Clean 功能,但这只是将错误向前移动了一点结帐 步骤。

为了解决这个问题,我不得不手动将 RDP 连接到我的代理机器中,taskkill /IM chromedriver.exe /F 并重新运行 失败的构建。

我的问题是 - 如何防止访问被拒绝错误?我已经想到了这些方法,但我不确定哪个是实用的:

  1. Get sources 步骤之前自动将 ADOS 获取到 运行 taskkill。怎么样?

  2. 使代理具有更高的权限,以便它可以删除 chromedriver.exe,即使它的进程仍在 运行ning 中。再一次,如何?

如果你的假设是正确的:

What I understand from this is that because chromedriver.exe is still running ADOS cannot delete its .exe file when it tries to clean the working folder at the start of the build run.

确保在每次测试结束时使用 driver.close()driver.quit() 方法。

使用拆解来确保它总是会发生。

编辑:

正如 OP 评论的那样:

when I cancel the ADOS build, the test step is terminated abruptly and the teardown step is not executed.

我推荐 atexit 方法。

对于 C# 使用 ProcessExit Event (credit to @Fredrik Mörk's answer)

对于 Python 使用 atexit

我想出了一个基于 ADOS Pipelines 功能的解决方案。在我的 Visual Studio 测试任务之后,我添加了一个 Command Line task 并像这样配置它:

  1. 脚本是'taskkill/IM chromedriver.exe /F'。
  2. 控制选项下我设置了:
    • 运行 这个任务'Custom conditions'
    • 自定义条件到'canceled()'
    • 出现错误继续 已检查。

这样,每当我的构建被手动取消时,chromedriver.exe 进程的所有实例都会终止,为后续构建扫清道路 运行。