Deploy to server using GIT
How to deploy software to a server using GIT (such as a website).
Two directories need to be created on the server:
Create the first for the GIT repository and go into it with:
mkdir repo && cd repo
mkdir <software_name>.git && cd <software_name>.git
git init --bare
GIT has been initialised in the directory and a new folder called hooks should exist where a file called post-receive will need to be created within it. This will be a GIT hook that invokes some code within it when data is pushed to this server later.
Create and edit the file with:
nano hooks/post-receive
Put the follow code into the file:
#!/bin/sh
git --work-tree=<path_to_software_dest> --git-dir=<path_to_repo_.git_dir> checkout -f
Save and exit. Now make the file executabled with:
chmod +x post-receive
From the client (development system) it will be a good idea to create an alias for the server connection via SSH (if not done already) before going further (and is essential if you want to use a non-default RSA key file to connect the repo). If this has already been done, please skip these next parts to adding the remote repository.
Open up the SSH config for the current user profile with:
~/.ssh/config
OR
If SSH config does not exist, create the directory and file with:
sudo nano ~/.ssh/config
Next enter in the server connection information in the following format:
Host <host_config_name>
HostName <host_address> #ip or url
User <ssh_username>
Port <number>
PreferredAuthentications publickey #Lets it know we want to use RSA authetication
# Custom RSA file path only
IdentityFile ~/.ssh/<rsa_key_to_use> #If not default RSA file
IdentitiesOnly yes #Stops SSH from trying default RSA if custom is set
The Host name is what will be written to used these SSH setting when calling the SSH command.
To connect to the server via SSH the <host_config_name> can now be used by shorthand. Connection can be established like so:
ssh <host_config_name>
Now the GIT repository can be added with the new shorthand syntax. Go to the GIT repo that needs to push committed content to the server and enter the following (giving the remote repo an approprite name, like 'live' or 'testing'):
git remote add <remote_repo_name> <host_config_name>:<path_to_repo_.git_dir>
Content can now be pushed to the server with:
git push <remote_repo_name> master
Ensure files have correct permissions on server as this can not translate when pushing with GIT. This is how to change the ownership of the files on the new system:
chgrp -R <user_group> <path_to_repo_.git_dir>
Updating the files thereafter should not change this.