Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to switch between SSH keys? #540

Open
@ChristofferJoergensen

Description

@ChristofferJoergensen

I have createda bounty on Stackoverflow for an answer to this question.

Is there any way to set different SSH keys toruby-git configuration on the fly, so I can work with different private repos?

What I've done is working well, but it works with one SSH key only.

I have created/ruby_git.sh in the root folder of my Rails app:

#!/bin/bashexec /usr/bin/ssh -o StrictHostKeyChecking=no -i ./certs/private_key "$@"

I have created/certs/private_key with my SSH key:

-----BEGIN OPENSSH PRIVATE KEY-----...-----END OPENSSH PRIVATE KEY-----

I have created/initializers/git_init.rb:

Git.configuredo |config|config.git_ssh=Rails.root.join("ruby_git.sh").to_send

I have also tried another approach, to create custom sh scripts and SSH private key files for each repo in runtime and delete them after use. But this seems to alterGit globally, so the next thread/session inherits the newGit config:

# @repo_id, @ssh_url and @private_key are instance variables set# based on the repo that we try to interact withcert_path=Rails.root.join("git_config","certs",@repo_id).to_sconfig_path=Rails.root.join("git_config","configs","#{@repo_id}.sh").to_sgit_config="#!\/bin\/bash\n\nexec\/usr\/bin\/ssh -o StrictHostKeyChecking=no -i#{cert_path}\"$@\""File.open(config_path,"w"){ |f|f.write(git_config)}File.open(cert_path,"w"){ |f|f.write(@private_key)}File.chmod(0755,config_path)File.chmod(0600,cert_path)Git.initGit.configure{ |config|config.git_ssh=config_path}Git.ls_remote(@ssh_url)FileUtils.remove_entry(cert_path)FileUtils.remove_entry(config_path)

Your environment

  • version of git and ruby-git: 1.8.1
  • version of ruby: ruby 3.0.0p0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp