Category: Programming
Tags: #git #devops #deployment #command-line
Published: 2024-12-17

How to Deploy a Web Application Using Git

If you have a local development environment and a remote server, you can use Git to painlessly deploy your application.


On the Remote Server

Shell into the server and make sure you are in the user directory for your site.

Crate a Bare Git Repository

mkdir projectname.git
cd projectname.git
git —bare init

Post Hook Receive

Create a post-receive hook in the hooks directory of the bare repository.

This will be executed after any push to the repository.

vi hooks/post-receive

Yeah yeah, I use vim. Shut it.

Put the following in the post-receive file:

#!/bin/sh
GIT_WORK_TREE=/home/projectname git checkout master -f

# cd /home/projectname
# composer install
# migrations etc

I left the commented out lines in there to show you where you can add additional commands to run after the push.

To summarize, when a push is made to the repository, the master branch is checked out to the user directory.

Set Permissions

Make sure that the post-receive file is executable.

chmod +x hooks/post-receive

Add Your SSH Key

In order to not have to enter a password every time you push to the repository, you will need to add your public key to the server. You can skip this step if you are okay with entering your password every time you push to the repository.

Edit ~/.ssh/authorized_keys and append your public key to the bottom of the document.

There is a chance that the .ssh directory does not exist. In this case you will need to create the .ssh directory and subsequently the authorized_keys file then update accordingly.


In Your Local Environment

Add the Remote Repository

Add the new git repository as a remote to your local repository.

git remote add production ssh://account@domain.com/home/projectname/projectname.git

Replace account@domain.com with the user from your remote server and the name of the domain mapped to your account.

For example, if your server user account is called spanky and your domain is mycoolproject.com, you would replace it with spanky@mycoolproject.com (apologies to anyone who buys that domain).

In a typical cpanel scenario, the user account would be the same as the domain name minus the .com so you would use mycoolproject@mycoolproject.com

Deploy Your Application

Any time you want to deploy the latest version of your code to the server, you can run the following command:

git push production develop:master --force

When running this command, you are pushing to the production remote you just added. I'm using develop as an example but, whatever branch you are working on, you need to push to the master branch on the remote server, hence the develop:master.

--force is being used to overwrite the master branch on the remote server. We don't need to deal with any weird git conflicts as the only purpose of that repository is to deploy your current code.

Summary

I use this all the time for small projects. It's a great way to deploy without having to deal with the overhead of CI/CD or the pain of FTP.