+++ author = "kandelvijaya" date = "2017-03-11T17:16:52+01:00" description = "GTX March 2017." tags = ["gtx", "git", "terminal", "XCode"] title = "GTX March: Git Auto Deployment.."
Last few months, I spent my off work time migrating one of the old app to self hosted Parse Server. Although, writing about Parse Server on dedicated Linux machine is out of scope for this post, I got few useful points regarding the use of terminal and git.
Lets say you have a website. You want to use
git. When you
git push, it should
deploy your site automatically. Plus, your teammate should also be able to push and pull.
Lets do this.
1. Creating a bare repo
Bare repo is used for sharing code. Typically shared for collaboration. Bare repos are good candidates for remote, where all teammates can push the code to.
Bare repos have
.git(for most case) extension on their path. For more detail
read this blog post. Difference between bare and normal git repo.
To make a bare repo for a existing repository (which is usually the case).
$ git clone --bare ~/my-website ~/my-website.git
my-website should be a normal git repository. Creating a bare repo is easy too. We wont be using this one for the example case.
$ git clone --bare init
Read Terminal Section for; how to push the bare repo to server.
2. Git hooks
Hooks are like callbacks. Maybe that's a programming view. Anyways, hooks let you run script after some event occurs on git. The event could be push, commit, merge, pull or etc. Here, we will focus on
Now say you moved the bare repo to the server machine, and want to configure git to deploy the site when someone pushes to this repo.
$ cd <yourpath>/<yourbarerepo.git>/hooks/ $ touch post-receive // (1) $ nano post-receive
(1):: Hooks are just shell script with special filename. Now we will clone the repo to a public website serving directory.
#!/bin/bash GIT_REPO=$HOME/<yourbarerepo.git> WORKING_DIRECTORY=$HOME/WWW/<yourwebsitePublicDir>/ set -e rm -rf $WORKING_DIRECTORY # remove old files git clone $GIT_REPO $WORKING_DIRECTORY #clone new content trap - EXIT
NOTE If you have critical project, I strongly suggest using
rsync and creating a backup dir during the cloning process.
If something goes wrong, the script can back it up easily.
Finally, make the hook script executable.
$ chmod +x post-receive
Thats the basics of automatic deployment. For more info please refer to: This fantastic in depth deployment guide from DigitalOcean
1. Background job, yes job.
Did you ever want to run a script repeatedly 1 am in the morning OR every time there is a new content for the app to consume OR database backup, periodic emailing, OR what have you. Enter cron job. Don't get afraid. There's just 1 line. Thats it.
$ crontab -e # available by default on Mac and Linux
Then edit the file. Lines starting with
#, as you guessed, are comments.
At the end of the file, I could do:
1 1 * * * ~/jobs/backupMongoDb.sh
Save and exit. Every 1(first) minute of every 1(hour) of every * (day) , of every * (week) of every year
run the script
~/jobs/backupMongoDb.sh. Simple and neat right.
So for the script part.
#!/bin/sh BACKINGDIR=$HOME/backup/ DESTNAME=<yourappname>+`date +"%m-%d-%y"` cd $BACKINGDIR mongodump --db <yourdbname> -o $DESTNAME
The last command
mongodump is the one that will dump the database. Finally, make this
$ chmod +x ~/jobs/backupMongoDb.sh
You could extend this script to remove the older directories in order to save space, zip the dumped database. This is left for your research.
2. Zipping and Scp(ping)
Lets get back to where we created bare cloned repo. Now we need to transfer that to server. We want to do it via the terminal.
$ scp -r <yourBareRepo.git> <yourname>@<ServerIP-or-domain>:~/website/<yourBareRepo.git>
SCP, stands for secure copy. For this step, you need to have local machine's SSH Keys added to the server beforehand. Transferring a file opens a network connection. Transferring 1000 files might need tons of connection to be opened and closed. This adds latency wherby 20 MB directory that contains 10000 files could take 10 minutes. Lets speed the transfer by transferring 1 file. 1 zipped file.
$ tar -czf bareRepo.zip <yourBareRepo.git> $ scp bareRepo.zip <yourname>@<ServerIP-or-domain>:~/website/bareRepo.zip
I have left for you to extract the zipped folder in the server. HINT:
$ man tar
Now on the local machine set the remote to be this bare repo on the server.
$ git remote add origin <username>@<SERVER_IP_ADDRESS>:website/bareRepo.git
1. Regexp find and replace
Last week, while at work in Zalando SE, I was refactoring Flexible Layout Kit. Every Element is represented by a ElementController which has
var model: <Some>Element? by conforming to a protocol.
We have 25 element controller. During the refactor, lets suppose, I want to make all,
var model: <Some>Element? to
var model: <Some>Element.
There are many manual ways. Xcode isn't powerful like AndroidStudio when it comes to refactoring. The way to do it quickly is to regex find replace.
(.*) is a capture group. It can be used later by referring it via $1 which is what we did in the replace section.
2. LLDB 'po' broken fix
After Swift came out, LLDB gets broken for various reason. It complains a lot. I cant debug effectively without
po. This is how I have been fixing this AST issue with LLDB. This is not the silver bullet.
- First Clean the project. Restart Xcode. Try lldb.
- (IF above doesn't work) Go to the derived data. Remove everything. Maybe
$rm -rfat that folder. Restart Xcode. Try lldb.
- (If above doesn't work) Change the derived data directory.
Suggesting / Commenting or editing this post can be done via github.
Thanks for reading until this point. Comments, suggestions and edits are appreciated. Cheers!