FreeBSD GIT deploy server - Deploy application to server via GIT push
Deploy software from a local machine to a remote machine via GIT push.
Download and install GIT for the remote FreeBSD system with:
cd /usr/ports/devel/git && sudo make config-recursive install distclean
Two directories will be created on the server:
Make a GIT repositories directory within the user's directory whom will be making the commit via GIT (a seperate user and directory can be setup for this, but for simplicity's sake we'll use the current user account):
sudo mkdir /usr/home/<username>/git && cd /usr/home/<username>/git
Create a GIT repository folder inside the
/usr/home/<username>/git directory, move into this folder and initialise it as a GIT repository with:
sudo mkdir <repository_name>.git && cd <repository_name>.git sudo 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:
sudo ee hooks/post-receive
Put the follow code into the file:
#!/bin/sh git --work-tree=<path_to_required_project_files_destination> --git-dir=/usr/home/<username>/git/<repository_name>.git checkout -f
Save and exit. Now make the file executabled with:
sudo chmod +x hooks/post-receive
git directory is owned by the user whose directory it is (as the
sudo command used will likely have set all files in
git directory to
sudo chown -R <username>:<username> /usr/home/<username>/git
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:
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
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:
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>:/usr/home/<username>/git/<repository_name>.git
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.