Sunday, August 15, 2021

How to clone a private git repository from Azure App Service Kudu console

In azure app service Kudu console,  it is easy to clone a public git repository using "git clone" command,  when it comes to private repository,   you may find the same "git clone" command is stuck at "Cloning" step and isn't working any more.  This is simply because the repository is a private one and need to be authenticated,  in the meanwhile, we need an non-interactive way since Kudu console doesn't support popped-out windows.

gitclone1.png

 

There are in total 3 approaches to clone the private git repository:

 

Approach 1 (Username & Password):  

The most obvious way to fix this is to put your username and password in the git URL:

 

 

git clone https://$username:$password@github.com/$username/$repo -v

 

 

Example:

gitclone2.png

The side effect is that the user credentials would be saved in the .git/config file and everybody who can access Kudu console will be able to view the password.

 

Approach 2 (SSH key):

Step 1:   Send a GET request to the Kudu site to get the SSH key,   the URL looks like: https://xxxx.scm.azurewebsites.net/api/sshkey?ensurePublicKey=1

gitclone4.png

Step 2:   Remove the double quote and add the SSH key in the Github SSH key

gitclone5.png

 

Step 3:   Go to Kudu console and use the SSH way to clone the repository:

 

 

git clone git@github.com:$username/$repo.git

 

 

For example:

gitclone6.png

 

Approach 3 (Personal Access Token):

This approach is to use "personal access token" created in the github developer settings page:  https://github.com/settings/tokens

 

 

git clone https://$token@github.com/$username/$repo.git

 

 

Example:

gitclone3.png

Posted at https://sl.advdat.com/3AHuqNe