TeamCity Docker构建报错:Git提交时作者身份未知
问题详情
将TeamCity构建迁移至Docker容器后,Git提交阶段失败,报错「作者身份未知」:
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"to set your account's default identity.
Omit --global to set the identity only in this repository.fatal: unable to auto-detect email address (got 'root@some_id.(none)')
尝试在Dockerfile中添加git config --global配置时,被Xray策略阻止;去掉--global执行则提示「fatal: not in a git directory」。当前使用TeamCity的Docker wrapper运行构建,Dockerfile内容如下:
FROM python:3.7 USER root RUN apt-get update RUN apt-get install -y git RUN apt-get -y install curl RUN apt-get install openssh-client RUN mkdir -p -m 0700 ~/.ssh && ssh-keyscan bitbucket.org >> ~/.ssh/known_hosts RUN mkdir script WORKDIR /script COPY . /script RUN pip install -r requirements.txt RUN curl -fsSL https://deb.nodesource.com/setup_12.x | bash - RUN apt-get install -y nodejs RUN mkdir /usr/bin/nodejs RUN ln -s /usr/bin/nodejs /usr/local/bin/node
可行解决方案
方案1:在TeamCity构建步骤中动态配置Git身份
既然无法在Docker镜像构建阶段配置,直接在TeamCity的构建步骤中,进入Git仓库目录后执行局部配置:
# 切换到代码目录(对应Dockerfile中的WORKDIR) cd /script # 设置当前仓库的Git身份 git config user.name "你的TeamCity用户名" git config user.email "你的TeamCity绑定邮箱" # 执行后续的Git提交命令 git commit -m "提交信息"
关键点:必须在Git仓库目录下执行git config(不带--global),确保当前目录包含.git文件夹(即COPY到容器的代码是完整的Git仓库)。
方案2:复用TeamCity内置环境变量自动配置
TeamCity提供内置环境变量BUILD_USER_NAME和BUILD_USER_EMAIL,可以直接引用实现和旧版一致的自动配置:
cd /script git config user.name "%BUILD_USER_NAME%" git config user.email "%BUILD_USER_EMAIL%"
这种方式无需硬编码账号信息,完全复用TeamCity的用户身份配置,和旧构建行为对齐。
方案3:通过容器启动命令注入全局配置(绕过Dockerfile限制)
如果需要全局配置,可在TeamCity的Docker wrapper启动参数中添加临时命令,避免修改Dockerfile:
在TeamCity的「构建步骤」→「命令行」中,先执行全局配置再执行构建逻辑:
git config --global user.name "你的用户名" && git config --global user.email "你的邮箱" && 你的构建脚本命令
注意:此方式的全局配置仅在当前容器生命周期内有效,不会写入镜像,因此不会触发Xray的拦截策略。
内容的提问来源于stack exchange,提问作者ad100

