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

如何将Git短提交哈希传入Docker Build用于Dotnet包版本控制?

解决Docker构建时传递Git短哈希的问题

错误原因

第二个错误unknown flag: --short是因为Shell未正确解析命令替换部分,误将git rev-parse的--short参数当成了docker build的命令行标志。第一个错误"docker build" requires exactly 1 argument也是类似的参数解析问题,Shell拆分了命令参数,导致Docker收到不符合要求的输入。

解决方法

给--build-arg的参数值添加双引号,确保整个Git命令的输出作为完整参数传递给Docker:

Unix系Shell(bash/zsh)

docker build --build-arg GIT_COMMIT="$(git rev-parse --short HEAD)" .

Windows PowerShell

docker build --build-arg GIT_COMMIT="$(git rev-parse --short HEAD)" .

额外优化(可选)

如果仅在构建阶段使用该参数(比如dotnet build),可以省略Dockerfile中的ENV步骤,直接使用ARG的值:

FROM ...

ARG GIT_COMMIT

...

RUN dotnet build -c Release --no-restore /p:SourceRevisionId=${GIT_COMMIT}

验证步骤

  1. 单独运行git rev-parse --short HEAD,确认能输出7位短哈希
  2. 执行修正后的Docker构建命令,检查参数是否正确传递到dotnet build

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 19:28:16