使用 python2 将 git 命令传递给 OS 的问题
Issues using python2 to pass git commands to the OS
下面是我的 python 脚本,注意这是为了测试。
from types import StringType
from os.path import isabs
from os import system, popen, getenv
from glob import glob
cmd = "ssh-add -l"
returned_value = system(cmd) # returns the exit code in unix
print('returned value:', returned_value)
cmd = "git clone git@bitbucket.org:XXXXXXXXXX/python_git_test.git"
returned_value = system(cmd) # returns the exit code in unix
print('returned value:', returned_value)
当我从CMD运行这个
C:\Users\jenkins\Desktop>python test.py
3072 SHA256:dl91g2BDUmEVpRqqBjJ8oXeHsEKYueW+LpmwCcGkd7I jenkins@cibuild1 (RSA)
3072 SHA256:i5kOlssdnL1g4RzB65bOira+4Y7LcSngp4sLCPb8aXI jenkins@CIBUILD3 (RSA)
('returned value:', 0)
Cloning into 'python_git_test'...
git@bitbucket.org: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
('returned value:', 128)
当我 运行 这个来自 gitbash
$ python test.py
3072 SHA256:dl91g2BDUmEVpRqqBjJ8oXeHsEKYueW+LpmwCcGkd7I jenkins@cibuild1 (RSA)
Cloning into 'python_git_test'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
('returned value:', 0)
('returned value:', 0)
我有几个问题,为什么当 gitbash 和 cmd 都在 ssh-agent 中加载了正确的密钥时,我在 CMD 上遇到错误?
其次,这是一个较大的 Jenkins 构建问题的一小段。所以一般来说,jenkins 是调用 git 更新程序 python 文件的那个,我一直得到访问被拒绝的公钥。有谁知道詹金斯会尝试使用什么终端?我假设 CMD?
尝试先设置 set "GIT_SSH_COMMAND=ssh -Tv"
,然后在 CMD 中设置 python test.py
。
您将确切地看到 Git 正在使用什么作为 SSH 密钥,以了解它为什么不访问远程存储库。
您可以在 git bash
会话中执行相同操作(其中将继承在 CMD 中设置的环境变量)。
下面是我的 python 脚本,注意这是为了测试。
from types import StringType
from os.path import isabs
from os import system, popen, getenv
from glob import glob
cmd = "ssh-add -l"
returned_value = system(cmd) # returns the exit code in unix
print('returned value:', returned_value)
cmd = "git clone git@bitbucket.org:XXXXXXXXXX/python_git_test.git"
returned_value = system(cmd) # returns the exit code in unix
print('returned value:', returned_value)
当我从CMD运行这个
C:\Users\jenkins\Desktop>python test.py
3072 SHA256:dl91g2BDUmEVpRqqBjJ8oXeHsEKYueW+LpmwCcGkd7I jenkins@cibuild1 (RSA)
3072 SHA256:i5kOlssdnL1g4RzB65bOira+4Y7LcSngp4sLCPb8aXI jenkins@CIBUILD3 (RSA)
('returned value:', 0)
Cloning into 'python_git_test'...
git@bitbucket.org: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
('returned value:', 128)
当我 运行 这个来自 gitbash
$ python test.py
3072 SHA256:dl91g2BDUmEVpRqqBjJ8oXeHsEKYueW+LpmwCcGkd7I jenkins@cibuild1 (RSA)
Cloning into 'python_git_test'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
('returned value:', 0)
('returned value:', 0)
我有几个问题,为什么当 gitbash 和 cmd 都在 ssh-agent 中加载了正确的密钥时,我在 CMD 上遇到错误?
其次,这是一个较大的 Jenkins 构建问题的一小段。所以一般来说,jenkins 是调用 git 更新程序 python 文件的那个,我一直得到访问被拒绝的公钥。有谁知道詹金斯会尝试使用什么终端?我假设 CMD?
尝试先设置 set "GIT_SSH_COMMAND=ssh -Tv"
,然后在 CMD 中设置 python test.py
。
您将确切地看到 Git 正在使用什么作为 SSH 密钥,以了解它为什么不访问远程存储库。
您可以在 git bash
会话中执行相同操作(其中将继承在 CMD 中设置的环境变量)。