Here are the simple steps needed to create a deployment from your local GIT repository to a server based on this in-depth tutorial.
You are developing in a working-copy on your local machine, let's say on the master branch. Most of the time, people would push code to a remote server like github.com or gitlab.com and pull or export it to a production server. Or you use a service like deepl.io to act upon a Web-Hook that's triggered that service.
But here, we add a "bare" git repository that we create on the production server and publish our branch (f.e. master) directly to that server. This repository acts upon the push event using a 'git-hook' to move the files into a deployment directory on your server. No need for a middle man.
This creates a scenario where there is no middle man, high security with encrypted communication (using ssh keys, only authorized people get access to the server) and high flexibility due to the use of .sh scripts for the deployment.
- Know how to use GIT, Terminal etc.
- Have a local working-working copy ready
- Have SSH access to your server using private/public key
- Create a folder to deploy to on production server (i.e. your httpds folder)
- Add a bare repository on the productions server
- Add the post-receive hook script to the bare repository (and make it executable)
- Add the remote-repository resided on the production server to your local repository
- Push to the production server, relax.
Nuf said. I assume we are working on master – but you could work on any branch.
ssh into your production server:
$ ssh [email protected]
$ mkdir ~/deploy-folder
Now we create a "bare" repository – one that does not contain the working copy files. It basically is the content of the .git repository folder in a normal working copy. Name it whatever you like, you can also commit the .git part from project.git or leave it to create the repository in an existing empty folder:
$ git init --bare ~/project.git
This script is executed when the push from the local machine has been completed and moves the files into place. It resides in project.git/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;
chmod +x post-receive
Now we add the this bare repository to your local system as a remote. Where "production" is the name you want to give the remote. This also be called "staging" or "live" or "test" etc if you want to deploy to a different system or multiple systems.
$ cd ~/path/to/working-copy/
$ git remote add production [email protected]:project.git
Make sure "project.git" corresponds to the name you gave in step 3. If you are using Tower or a similar App, you will see the newly added remote in your sidebar under "Remotes" (make sure it's not collapsed).
Now you can push the master branch to the production server:
$ git push production master
If you are using tower, you can drag & drop the master branch onto the new production remote. That's it. Have questions, improvements?