我可以在环境变量中保存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屋!