Pulling a Private GitHub Repository from Azure Cloud Shell

Azure Cloud Shell is a great environment for deploying Azure resources. If you use Infrastructure-as-Code (IaC), you can use git to clone your public repositories directly into your Cloud Shell using the basic git clone command.

But what if your repo is private? In that case, you’ll need to have a way to authenticate to your git server. Git disabled password authentication for that purpose about two years ago. The remaining option to use is to use SSH keys.

In this article, I will show you how to generate an SSH key pair, add it to your GitHub account, and use it to clone and pull contents from a private GitHub repo.

Generate a new SSH key pair

  1. Open your Azure Cloud Shell.
  2. Type the command ssh-keygen -t RSA to generate a new RSA key pair.
  3. Accept the defaults for all the prompts if this is your first key pair in Cloud Shell.
  4. You will be asked to create a password to protect the private key. While you can leave the password blank, I recommend setting a password.
    You will need to use this password each time the key is accessed.

At the end of the process, you will have a new folder, .ssh, in your Cloud Shell home directory. There will be two files: id_rsa and id_rsa.pub. The .pub file contains the public key of the pair.

We need the contents of the id_rsa.pub file to upload to GitHub. There are a few ways to obtain it, but because the file contents are rather short, you can simply execute the command cat ~/.ssh/id_rsa.pub and copy the output to your clipboard.

Create a new SSH key for authentication to GitHub

Using your web browser, go to your GitHub settings. Then:

  1. Go to the SSH and GPG keys pane.
    Direct link: https://github.com/settings/keys
  2. Click the New SSH key button.
  3. Give the new key a title, such as Azure CloudShell.
    If you have multiple Azure Cloud Shells in multiple tenants, you should probably make it more descriptive.
  4. Confirm the Key type is set to Authentication Key.
  5. Paste the contents of the id_rsa.pub file in the Key text box.
  6. Click the Add SSH key button.

You can now use this SSH key to authenticate to GitHub.

Clone the private repo to Cloud Shell

Go back to your Azure Cloud Shell. Before cloning, consider creating a repos folder to keep all your cloned repositories together in your Cloud Shell storage. Then, use the following clone command:

git clone git@github.com:GitHubUsername/RepositoryName.git

Replace GitHubUsername with your username (or the name of the organization if the repo is hosted in an organizational account) and RepositoryName with the name of the repo to be cloned. You can also find the entire string in GitHub, if you click the green Code button on the repo’s home page and then click the SSH tab.

Screenshot of the GitHub Code button used to show the SSH clone path.

A note about cloning a private repo from an organization: you’ll need to authorize your key to be used for SSO for that organization. This is an additional step you’ll take on the GitHub SSH and GPG keys pane.

Let me know what you think, or ask a question...

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.