sn n
sn n

Reputation: 399

Automate Git clone from Private repo using SSH

I have been writing bash script to clone private Github repo using SSH. Steps need to be followed -

  1. Generate SSH key using ssh-keygen -t rsa -b 4096 -C "your email".
  2. Copy the output of cat ~/.ssh/id_rsa.pub
  3. Store it within - https://github.com/settings/keys

How do I automate step 3 in bash script. Any help would be appreciated.

Upvotes: 1

Views: 2000

Answers (1)

VonC
VonC

Reputation: 1329092

You would need to use the GitHub API POST /user/keys to add a public SSH key to the authenticated user's GitHub account.

It requires that you are authenticated via Basic Auth, or OAuth with at least write:public_key scope.

So your script must take as parameter.

Result: example

# Add a SSH-Key (type "user:passwd ^D"), output: JSON object, or JSON error
curl -X POST -u <user[:passwd]> https://api.github.com/user/keys \
  --data "{\"title\": \"<title>\", \"key\": \"$(cat $HOME/.ssh/id_rsa.pub)\"}"

(other example here)

Upvotes: 1

Related Questions