A while back I wrote about how to set up a WordPress deployment strategy using Git commits to update your code, now I’m going to leave a simple recipe here for updating more generic code so that you can push any project to your Digital Ocean droplet without too much fuss via Git commits and hooks.
Without further ado, below is the code you will need to get this up and running to deploy to your live server on an Ubuntu machine (I’m using the pre-installed Docker container that runs on Ubuntu):
// 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 "firstname.lastname@example.org" // Verify Git is up and running $ git config --list // Add a git user to the Linux distro $ su - $ useradd git $ passwd *password* // create your repo $ mkdir -p /home/git/mvpguy.git $ cd /home/git/mvpguy.git
Now you have Git installed and a a repo to work with! If you use a GUI or the terminal, your remote endpoint to access your repo will be something like this:
Updating your site via commits
OK, but that was just the Git repo, what we want is that the repo updates the files being served to end users and we achieve this via hooks. Here’s the code:
// go into your hooks folder and edit post-receive $ cd /home/git/mvpguy.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/mysite/public --git-dir=/home/git/mvpguy.git checkout -f // make it writeable $ chmod +x post-receive
Now whenever you push code to the repo, it will update the location you added into post-receive with the new files. This works much better than FTP or manually adding files via terminal. Some of the big advantages are a quicker deployment process and keeping your code versioned so you can roll back when you make mistakes.