首页 > 解决方案 > GIT:仅适用于 http/https 克隆存储库:git push 失败以明文形式显示密码

问题描述

我们正在使用 https 通过 Jenkins 构建克隆一个 repo,如下所示:

git clone https://${repo_username}:${repo_password}@internalgit.com/scm/project/repo.git -b ${branch_name} $tmp

在上面的命令中: ${repo_username} 和 ${repo_password} 是作为秘密传递的 Jenkins 变量,因此它们不会被记录为明文。

但是,这会将用户凭据添加到 git 远程 URL,并且在任何推送失败的情况下,它会在以下错误中以明文形式显示凭据:

[ERROR] To https://user:password@internalgit.com/scm/project/repo.git
[ERROR]  ! [remote rejected] master -> master (pre-receive hook declined)
[ERROR] error: failed to push some refs to 'https://user:password@internalgit.com/scm/project/repo.git'

推送失败可能有多种正当原因,但是,在屏幕上打印凭据是不可接受的。

有没有办法:

以下变通方法有效,但在我们的用例中不可接受:

标签: git

解决方案


假设输出可见但输入不可见: git clone https://${repo_username}:${repo_password}@internalgit.com/scm/project/repo.git -b ${branch_name} $tmp | sed "s/${repo_password}/<redacted>/g" 应该做你想做的。

我误读了这个问题;要使这个答案起作用,您必须在每次推送时运行它(即git push 2>&1 | sed "s/${repo_password}/<redacted>/g"。我也错过了 git 将其打印到stderr,因此除非您想使用process substition,否则很难重定向输出。

如果密码包含任何特殊的正则表达式字符,您应该转义密码(否则 sed 可能会比您的意思更多或更少匹配)。这个答案有一些现成的解决方案,用于转义字符串以与 sed 一起使用。


推荐阅读