How to streamline git commits to Strato.de’s hosting server?

Whenever I update my web site I need do some manually work. Actually I use the FTP-connection with FileZilla. I connect to my web hosting area at Strato.de, drag ‘n drop all changed files from my local hard drive into server side, wait until all files are transferred and reload my page at browser. It consumes one or two minutes for each distribution as a release of code changes. I’m not happy. Why don’t I use a Git hook to synchronize my web site too while pushing changes to remote git repos?

Preconditions

My code lives inside private git repository at BitBucket.org. I implement all features (or even bigger tasks) via feature branch, test the changes locally and merge the branch back into MASTER branch (my holy grail). I synchronize my local and remote git branches via git push command.

I structure my web site project into multiple folders. The main entry point is the ./web/ folder. Inside this folder there are all modules and an index.html. At Strato’s admin panel I configed an internal redirect to folder /ronny-springer.de/web/.

I’m using a FTP connection to copy files from my PC to web site hoster. There is nice shell extension called GIT FTP which provides all the necessary ability to streamline code changes via mentioned file transfer protocol.

TL;DR
I work with msysgit at Windows 10 (git scm for Win). I need to update the curl.exe file inside ./bin folder at git installation to be able to use SFTP. You download curl 7.50.3 for Windows from internet and follow the instruction to add SFTP-capabilities to cURL by Carl Helmertz.

Sync and Go

I’m opening my GIT bash and navigate to my Project’s local checkout location at hard drive. I change the directory towards Git’s ./hooks folder and create a new pre-push shell file.

$ cd ./.git/hooks
$ touch pre-push
$ vi pre-push

Inside the shell file I past the git ftp command line, save and quit the file.

#!/bin/sh
#
code idea by Paul Zumbrun (pauljz)
while read oldrev newrev refname
do
 branch=$(git rev-parse — symbolic — abbrev-ref $refname)
 if [ “master” == “$branch” ]; then
 exec git ftp push -u [change into your ftp user name] -p [your ftp pw] ftp.strato.de
fi
done

I set file permission and navigate back to project root.

$ chmod +x pre-push
$ cd ../..

Now I do a local change (e.g. set background-color of HTML body to yellow). I commit und push the change.

$ git commit -am “apply background color”
$ git push origin master

// execution of pre-push hook
1 file to sync:
[1 of 1] Buffered for upload ‘web/module/theme-calm/theme.css’.
Uploading …
Last deployment changed to ff29f88c7b93e21dc07ae9ada93462d2d1fca022.

// execution of git push command
Counting objects: 6, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (6/6), 476 bytes | 0 bytes/s, done.
Total 6 (delta 3), reused 0 (delta 0)
To https://ronny-springer@bitbucket.org/ronny-springer/ronny-springer.de.git
67ecd97..ff29f88 master -> master

Now, switch to browser. I reload the page (+ clear cache). Et voilá! All changes are online within seconds.

www.ronny-springer.de

Imperfect things

I don’t like to save my ftp credentials as plain text strings inside hook file. It is relatively insecure. At the moment I did not try the way of SSH. Strato offers a secure channel communication and GIT FTP documents the options for (public) key.

TL;DR
Open GIT bash as Admin and test the SSH login to Strato. Try following command and enter your ftp password.
$ ssh [fpt user name]@ftp.strato.de

First of all I would like to use a post-received hook. I assume that this hook is triggered as last action after the git push (compared to docu). But the hook was never executed. Pragmatically, I turned over to pre-push.

Summary

I’m really impressed how fast I can deploy new features to my web site. I get rid of all manually work with an additionally tool (FileZilla). Now I push changes to remote master branch and my website is up to date immediately.

If you suggest an improvement (e.g. the imperfect things), please feel free to write a comment.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.