Pushing to BitBucket with an SSH key using gitpython

84 Views Asked by At

I'm trying to push some files to BitBucket using gitpython and an SSH key. It works fine when I do it through Git Bash, but when I try it using gitpython I keep getting this error: Cmd('git') failed due to: exit code(128) cmdline: git push --porcelain -- origin stderr: 'fatal: Could not read from remote repository.' Any help appreciated, thanks! Code below:

import git
    def add_and_commit(repo_dir, commit_message):
        try:
            print('Beginning BitBucket Push...') 
            git_ssh_identity_file = r"C:\Users\josh\.ssh\id_ed25519"
            git_ssh_cmd = 'ssh -i %s' % git_ssh_identity_file
            with git.Git().custom_environment(GIT_SSH_COMMAND=git_ssh_cmd):
                repo = git.Repo(repo_dir) 
                # Get a list of all mxliff files in the directory
                all_files = []
                for root, dirs, files in os.walk(repo_dir):
                    for file in files:
                        file_path = os.path.join(root, file)
                        if file.endswith(".mxliff"):
                            all_files.append(file_path)
    
                repo.index.add(all_files)         
    
                origin = repo.remote(name='origin') 
                
    
                existing_branch = repo.heads['main']
                existing_branch.checkout() 
    
                repo.index.commit(commit_message) 
                print('Commited successfully') 
                origin.push() 
                print('Pushed changes to origin')
        except Exception as e:
            print(f'Error when pushing to BitBucket: {e}')
            errors.append(f'Error when pushing to BitBucket: {e}')
0

There are 0 best solutions below