Home > Enterprise >  Issues using python2 to pass git commands to the OS
Issues using python2 to pass git commands to the OS

Time:03-12

below is my python script, note this is for testing.

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 [email protected]:XXXXXXXXXX/python_git_test.git"
returned_value = system(cmd)  # returns the exit code in unix
print('returned value:', returned_value)

When I run this from 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'...
[email protected]: 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)

When I run this from 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)

I have a couple of questions, why when both gitbash and cmd have the correct key loaded in ssh-agent am I getting an error on CMD?

Second, This is a small snippet from a larger Jenkins build issue. So generally speaking jenkins is the one that calls the git updater python file, I consistently get the access denied publickey. Does anyone know what terminal jenkins would be trying to use? I'm assuming CMD?

CodePudding user response:

Try and set set "GIT_SSH_COMMAND=ssh -Tv" first, before python test.py in CMD.

You will see exactly what Git is using as SSH key, to understand why it does not access the remote repository.

You can do the same in a git bash session (where the environment variable set in the CMD will be inherited).

  • Related