Skip to content

Instantly share code, notes, and snippets.

@NicmeisteR
Last active May 15, 2020 18:37
Show Gist options
  • Save NicmeisteR/92b52c70fff3dbc3d70e6d550619cc84 to your computer and use it in GitHub Desktop.
Save NicmeisteR/92b52c70fff3dbc3d70e6d550619cc84 to your computer and use it in GitHub Desktop.
Git Hooks Setup

Digital Ocean deployment with Git Hooks

Here are the simple steps needed to create a deployment from your local GIT repository to a server based on this in-depth tutorial.

1. Create a folder to deploy to

Connect to Digitl Ocean Droplet using SSH or VS Codem once in create the deploy folder and ensure you have the below structure:

/var/www/placeholder.com/repo
/var/www/placeholder.com/temp
/var/www/placeholder.com/html

2. Add a bare repository in the repo folder

Now we create a "bare" repository – one that does not contain the working copy files. It basicaly is the content of the .git repository folder in a normal working copy:

$ cd repo && git init --bare .

3. Add the post-receive hook script

This scrtipt is executed when the push from the local machine has been completed and moves the files into place. It recides in repo/hooks/ and is named 'post-receive'. You can use vim to edit and create it. The script does check if the correct branch is pushed (not deploying a develop branch for example). You can download a sample post-receive script below. Also, don't forget to add execute permissions to said script (SUPER IMPORTANT)

cd hooks
chmod +x post-receive

4. Add remote-repository localy (Where you're working on the source)

Now we add the this bare repository to your local system as a remote. Where "live" is the name you want to give the remote. This can also be called "staging" or "production" or "test" etc if you want to deploy to a different system or multiple systems.

$ git remote add live admin@yourserver.com:/var/www/placeholder.com/repo

5. Push to the production server

Now you can push the branch to the live server:

$ git push live {branch_name}

6. Additional Sources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment