Using Github to Automate Code Deployment (Raspberry Pi?)

by scanos in Circuits > Software

179 Views, 4 Favorites, 0 Comments

Using Github to Automate Code Deployment (Raspberry Pi?)

github1.png
github4.png

Do you like your code scrambled or boiled?, if neither then read on thus avoiding the rookie mistake many programmers make of.....not version controlling or documenting code. The basic premise is instead of jumping onto your application server / Raspberry Pi and start coding, you craft the code in a proper document storage area ( e.g. Github.com) and then push it onto the application server.

Why do it? better quality code, recover from disaster and effective team working etc.

This instructable shows you how to deploy software code held in Github.com to a remote deployment environment (appliaction server) using only two commands . To do this, we use special scripts known as githooks. I assume that you have some familiarity with Github but as a reminder, Github.com is a web repository where you can store software code.

Github.com offers collaboration, communication and work flow to ensure integrity of code. It is used a lot where say a customer commissions a developer to write some code for them. Github.com allows the customer to introduce a process whereby the developer uses Github.com to write and store code from which the customer can securely deploy this code to his environment. It offers segregation of duties which means that the developer never sees the customer's technical environment (GDPR!) and that the customer always has access to the code that he or she has paid for.

Some other things to note are that with a free Github.com account your git repositories are public and that to secure them by making them private, you need to pay a subscription of around $7 per month. Furthermore, one should not confuse Github.com as a web service with Git as a technology. You will see in this instructable that we can install git repositories on our own environment, and interface them with repositories which we have set up on Github.com.

What you will need for this instructable

  • A github.com account.
  • A Linux environment (application server) with a web server (could be a VPS or even a Raspberry Pi).
  • Putty or a similar terminal emulator.
  • Ability to deploy git repositories on your Linux environment.

Adding and Changing Files on Github.com

github2.png
github3.png

After you have signed up for an account on Github.com, you can start creating repositories. I have shown the workflow in one of the images. WIthin the code window, you can start uploading and creating files, mainly your code files, e.g. php, js files etc.

When you want to make a change to your files, you first create a branch. To do this, click on the button that says branch master. You will be presented with a dialog box (find or create branch) into which you can type the name of your branch, say newfeature1. Press return and you will see that the button now displays branch newfeature1. In addition, all of the files from master have been transferred to the branch's code window.

Now, let's make a simple change to the README.md file to show how Github manages changes. Firstly, click on the README.md file to open it and then click on the pencil icon to edit it. Change some text in te document after which go to the bottom of the document and put some comments in the COMMIT CHANGES dialog box , then press the COMMIT CHANGES button. This will then show you the modified file.

Next, click on the code tab and you will see a button with green text COMPARE AND PULL REQUEST on the same line as your branch name, i.e. newfeature1. Click on the compare and pull request button and it will take you to a dialog box showing the text you entered earlier. Click on the CREATE PULL REQUEST button and it will take you to a screen with a notice that should state -This branch has no conflicts with the base branch - Click on the MERGE PULL REQUEST BUTTON then CONFIRM MERGE button

You should now see the following text displayed Pull request successfully merged and closed . At this this stage, you can press the DELETE BRANCH button.

In summary, you changed a file and then committed it to your local branch and after which you merged it back to the master branch, overwriting the previous version of that file.

Github retains the previous versions of the master file to permit you to roll back if necessary and also logs all activities.

In the next step, I will show you how to deploy the changed files to a remote application server. Firstly, you will need to get a https link to this Github.com repository which you can access by clicking on the code tab then pressing the clone or download button which will display Clone with HTTPS . Next, click on the copy icon.

Setting Up Your Remote Applications Server to Receive the Github Code

I assume that you know how to us Putty.

Firstly, log into your server using Putty and install git

To do this, type sudo apt-get install git

Now go to your home directory by typing cd ~

Next type git clone <github repo> which is the https url alluded to at the end of the previous step. The command is something like git clone https://github.com/scanos/project_test.git

When you execute this command, it will create a project_test folder into which all of the files from the github.com repository will be transferred.

Next type git init

We want to use this git repository to synchronise with the main github.com respository (which will be maintained by the software developer) and use it as a conduit to push changes to our application deployment folder on this server.

We need to ensure that the user running the scripts we are about to set up has access to the deployment folder. For example, if the deployment folder was /var/www and the user was test then we would have to add user test to the group which owns the deployment folder; for Apache that's usually www-data. Type whoami to confirm your log in account.

The next stage is to set up a script or githook which will copy the application files to the deployment directory whenever we update / synchronise (merge) with the Github.com Master repository.

To do this, we need to create a script in the hidden hooks folder which resides in the local git we just created.

To do this, go to the git directory, e.g. cd ~/project_test

Next, create a script in the hooks directory - type nano .git/hooks/post-merge

Here is an example of what you should type

#!/bin/bash
unset GIT_INDEX_FILE

git --work-tree=/var/www/pdo --git-dir=/root/project_test/.git checkout -f

To explain this, the work-tree is the deployment directory whilst the git-dir directory is self explanatory. You will have to replace root with the whoami user name and project_test with the name of your own git on this server.

You will have to make this script executable chmod +x .git/hooks/post-merge

Next, we'll show you how to use this script.

How to Use the Script

github4.png

The usage is very simple.

1. After changes have been made to the Github.com repository, you will want to merge them to your application server.

2) Go to the git folder on your application server , type cd ~/project_test where project_test is the name of the git repository.

type git reset --hard

then type git pull

You should see text something like

root@vps213595:

~/project_test# git reset --hard
HEAD is now at bd7aac7 Update README.md

~/project_test# git pull

remote: Counting objects: 3, done. remote: Compressing objects: 100% (3/3), done. remote: Total 3 (delta 2), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. From https://github.com/scanos/project_test bd7aac7..7142f8a master -> origin/master Updating bd7aac7..7142f8a Fast-forward

README.md |

2 +- 1 file changed, 1 insertion(+), 1 deletion(-)

3) It might seem a bit daunting at first but it ensures that changes to your deployment directories are controlled, auditable and structured. You may be asking why not just transfer files from Github.com to the application server deployment folders. The reason is that this would not offer segregation of duties and therefore could compromise security.