Configuring a git repository on your own server

Published in code on February 05th, 2023.

Time to say goodbye to SFTP. This is the first step towards seamlessly pushing update to an app or website on your own server.

'been going back and forth with this one for quite some time.

The thing is, it's quite handy to push code to servers via an SFTP client when it comes to certain projects that might not require a DevOps overhead or a complex architecture... and it's a pitfall one can easily fall into, by choice, truth to be told, when considering the shortest path, the timeline and business goals. But it's also a practice that is frowned upon by many.

Because I haven't found a deployment approach in between the SFTP option and a full CI/CD pipeline documented online that I'm really happy with, I decided to write my own. I might get back from time to time to this article in case I'll forget any of these steps.


🪧 

This article uses Github as a reference and is based on the assumption that you have a repository created for your project, as well as access to your own hosting (shared or private).


The first time when you'll going to do this, it'll take you anytime between 30 - 60 minutes. The second time? probably between 15 - 30 minutes. The third one? You get the idea.

So let's get going...

Access your server and create an SSH key

SSH into your server and run the following command.

ssh-keygen -t <algorithm> -C "<youralias>"

The terminal will prompt you to set a path for the key and that's where you can define a custom key name. I prefer to go with the pattern id_<algorithm>_<repository_host>, as explicit key names are more useful when you're working with multiple repositories.

Feel free to bypass the passphrase step that the CLI will ask you about. The variables used in this command refer to the following:

  • <algorithm> is one of the ssh algorithms you may choose to use (ed25519, rsa, etc.)

  • <youralias> can be any identified of your choice attached to your ssh key; usually this is your e-mail address

Add your SSH key into config

Create or edit your server's SSH config.

vi ~/.ssh/config

Configure a new host that uses your private key as an authentication to Github's remote server. This will then be used when cloning the repo for the first time.

Host <repository_host>
     HostName github.com 
     AddKeysToAgent yes 
     PreferredAuthentications publickey 
     IdentityFile ~/.ssh/id_<algorithm>_<repository_host>

Here we've introduced a new variable:

  • <repository_host> helps you identify the correct host into your SSH config; I usually prefer to go with a name similar to the name of my git repository, as this will be used later during the active SSH connection

In case you've created your SSH config from scratch, you also need to update the permissions of the file. 600 represents the default permissions used for the private keys.

chmod 600 ~/.ssh/config 


Are you using the same server for 2 apps? 

Then you'd want to configure a second host as well, based on the example above. Github will not allow you to use the same deploy key for 2 repositories, and if you use your server as a shared host you'd need to generate & configure a second SSH key.


Configure the SSH key as a Github deploy key

Head over to your repository on Github and look for "Settings" -> "Deploy keys" -> "Add deploy key".

You'd want to give this a proper name, so that weeks or months later, if you'll ever have to work with multiple deploy keys, you'll know which is which.

Sticking to a proper naming convention or even casing for yourself is hard, but will make things ten fold easier into the future.

Now, copy the public key from your server and paste it in Github. Save your action and you're done with this step. The following command will quickly show you the key via cli.

cat ~/.ssh/id_<algorithm>_<repository_host>.pub

Of course, always replace the terms in the commands with your actual choices defined earlier. Then, time to...

Clone your repository

Move into the path where you'd like to clone your repository and run the following command

git clone git@<repository_host>:<github_username>/<repository_name>.git

The newest & final variables referenced are:

  • <github_username> is your Github username

  • <repository_name> represents the name of your git repository

... and you're done.

By now you should have your code cloned onto your server. Time to get to work and build something cool.

I'll also get back myself and continue this article with a second one, dedicated to setting up a deployment procedure for your app by keeping your repo in sync on the server.