Setting up a server with Git hooks to deploy and Docker for your projects
4/25/2020
1. Create your server
I used Digital Ocean for my hosting and find it's great. This tutorial is for any hosting that has SSH and shell access. If you want to use my referral link, you get 100USD credit and I get a cut! If you don't want to use the referral, just click here.
I normally select a pre-built Docker droplet to make life super simple - 2-3min and you are ready to go with your new IP address and server:
The cheapest server should work (5USD/month):
2. SSH into your new Server - Set up a Git Repository with Hooks
// Note: your SSH keys should be already added to droplet on creation for this to work
// login into the droplet via SSH
$ ssh root@ipaddressofdroplet
// Install Git
$ sudo apt-get update && sudo apt-get install git
// Config Git
$ git config --global user.name "Your Name"
$ git config --global user.email "youremail@domain.com"
// Verify Git is up and running
$ git config --list
// create your git repo on the server
$ mkdir -p /home/git/myproject.git
$ cd /home/git/myproject.git
$ git init --bare
// go into your hooks folder and edit post-receive
$ cd /home/git/myproject.git/hooks
$ nano post-receive
// add something like the code below into post-receive
// - edit the paths to your liking
#!/bin/bash
git --work-tree=/srv/myproject/public --git-dir=/home/git/myproject.git checkout -f
// save before closing the nano editor!
// make post-receive executable
$ chmod +x post-receive
// make sure your work tree folder exists so that you can create files inside it!
$ mkdir /srv/myproject/public
3. Push the files to the Remote repository
Now you can connect your local files to the remote repo. Any files you push to the remote server will be also copied to the work-tree folder and become live.
// Note: for security you should probably change the root user!
// navigate to the folder you wish to copy the remote server to your local machine
$ cd /somefolder
$ git clone ssh://root@mydropletip/home/git/myproject.git
Make changes to the files you would like to be live on your server in this local folder on your machine, then push them to the live server:
$ git add .
$ git commit -m "Initial commit"
$ git push
Your files should no be on your live server. You should verify all is working by visiting your work-tree folder on the remote server and checking your commit files are present there. If not, go back and see where you did something wrong.
$ ssh root@yourserverip
$ cd /your/worktree/folder
// are my files there? Good. Let's continue.