Azure DevOps中Docker@2任务构建失败却仍显示成功的问题咨询
Azure DevOps Docker@2任务构建失败却显示成功的解决办法
问题场景
在Azure DevOps构建流水线中使用Docker@2任务构建镜像时,因Dockerfile路径拼写错误导致文件找不到,构建逻辑明明失败了,但任务却仍然被标记为成功。日志中显示未处理的Promise拒绝警告,但进程没有返回错误退出码。
你的流水线片段:
stages: - stage: build displayName: Build jobs: - job: build_service displayName: Build Service dependsOn: [] pool: vmImage: 'ubuntu-22.04' steps: - task: Docker@2 displayName: Docker Build inputs: command: build addBaseImageData: false addPipelineData: false buildContext: . Dockerfile: $(Build.SourcesDirectory)/servic/Dockerfile
对应的错误日志:
(node:2524) UnhandledPromiseRejectionWarning: Error: No Dockerfile matching /home/vsts/work/1/s/servic/Dockerfile was found. at Object.run (/home/vsts/work/_tasks/Docker_e28912f1-0114-4464-802a-a3a35437fd16/2.212.1/dockerbuild.js:15:15) at getToken.then (/home/vsts/work/_tasks/Docker_e28912f1-0114-4464-802a-a3a35437fd16/2.212.1/docker.js:72:27) at process._tickCallback (internal/process/next_tick.js:68:7) at Function.Module.runMain (internal/modules/cjs/loader.js:834:11) at startup (internal/bootstrap/node.js:283:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3) (node:2524) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:2524) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. Finishing: Docker Build
原因分析
这是Docker@2任务旧版本(比如你使用的2.212.1)的一个已知问题——任务内部的Promise拒绝未被正确捕获,导致进程没有返回非零退出码,Azure DevOps因此判定任务执行成功。
解决方案
1. 升级Docker@2任务到最新版本
微软后续已经修复了这个问题,将Docker@2任务升级到最新稳定版后,未处理的Promise拒绝会被正确捕获,任务会在构建失败时自动标记为失败。
- 在流水线编辑器中选择Docker@2任务,点击版本下拉框选择最新版即可;
- 也可以在YAML中显式指定最新版本号(具体版本以Azure DevOps市场显示的为准),示例:
- task: Docker@2@2.220.0 displayName: Docker Build inputs: # 你的输入参数保持不变
2. 临时 workaround(无法升级任务时)
如果暂时无法升级任务,可以在Docker构建步骤前添加一个文件校验步骤,提前检查Dockerfile是否存在,避免进入构建逻辑后出现未捕获错误:
- bash: | DOCKERFILE_PATH="$(Build.SourcesDirectory)/servic/Dockerfile" if [ ! -f "$DOCKERFILE_PATH" ]; then echo "错误:Dockerfile路径不存在 - $DOCKERFILE_PATH" exit 1 fi displayName: 校验Dockerfile路径 condition: succeeded()
把这个步骤放在Docker@2任务之前,一旦检测到文件不存在,会直接退出并标记任务失败。
内容的提问来源于stack exchange,提问作者Daniel Lerps
相关产品推荐
相关产品推荐

