我可以在环境变量中保存 git 凭据吗? [英] Can I hold git credentials in environment variables?
问题描述
我想创建一个非常简单的 shell 脚本,最终将由另一个应用程序调用,用于更新本地 git 存储库:
I'd like to create a very simple shell script, which will ultimately be called by another application, that updates a local git repository:
#!/bin/bash
cd $1
sudo git pull
执行此操作时,系统会要求我提供凭据(我从私有 BitBucket 存储库中提取).
When executing this I'm asked for credentials (I'm pulling from a private BitBucket repository).
我可以(简单地)将凭据存储在环境变量中吗?
Can I ( briefly) store credentials in environment variables?
#!/bin/bash
export GIT_USERNAME=<user>
export GIT_PASSWORD=<pass>
cd $1
sudo git pull
以上行不通.会什么?我可以以编程方式修改原始网址,但这似乎有点过分.
The above doesn't work. Would anything? I could programmatically modify the origin url but that seems a bit execessive.
推荐答案
我知道这是一个很老的问题,但是如果你真的需要为 HTTP 基本身份验证传递用户名和密码,你可以像这样设置帮助器:
I know that it's very old question but if you really need to pass username and password for HTTP basic authentication you can just set helper like this:
git config credential.helper '!f() { sleep 1; echo "username=${GIT_USER}"; echo "password=${GIT_PASSWORD}"; }; f'
更新:我已将 sleep 1
添加到函数中.在某些环境中,由于竞争条件可能需要它.我有 2 台运行 Debian Jessie 的虚拟机.它们具有相同的架构,但 CPU 和内核数量不同.在其中一台机器上,助手在没有 sleep
的情况下工作正常.另一方面,它不是.经过几个小时的调试,我运行 strace
来查看发生了什么.它神奇地开始起作用.strace
只是让 git 慢了一点.
UPDATE: I've added sleep 1
to the function. In some environments it may be probably needed due to race condition. I've got 2 virtual machines running Debian Jessie. They had the same architecture but different CPU and different number of cores. On one of these machines the helper was working fine without sleep
. On the other one it wasn't. After few hours of debugging I run strace
to see what's happening. And it magically started to work. strace
just made git a little bit slower.
这篇关于我可以在环境变量中保存 git 凭据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!