How i sync my code automatically to a remote server

I do not want to have my Docker setup on my Mac for several reasons. Docker on Mac sucks because of volumes. I could use Docker-Machine but i don’t like the idea of having a virtual machine running just for that.

That is why i have my docker setup running on a powerful server in the cloud.

As soon as the code changes it want all the changes to be automaticaly synced to my server.

But how the get code changes there ?

Because MacOs is awesome you can achieve this with included tools such as rsync and lauchd.

Here are my settings, please adapt it to your needs.

First of i have created a simple bash file which takes care of calling the actual rsync command. This fits my needs as a PHP/Symfony Developer but you can easily adapt it to your needs.

Please checkout my gist on github to get the code.

Put the file to ~/sync.sh

You can call it like below but first you have to replace some placeholder with your variables

– my_user=your username on the remote server
– my_remote_host=the hostname of your destination server
– my_source_folder=this is the local folder where your code resides
– my_remote_folder=this is he remote folder where the code gets synced to
– my_project_folder_to_monitor=this is the local folder where your code resides (should be the same as my_source_folder)

/bin/sh -c "~/sync.sh -h my_remote_host -u my_user -s my_source_folder -d my_remote_folder"

Okay, that is already pretty good. But i wanted to run this script as soon as code changes in my project. To do this i created a job for the laund on my mac.

Please checkout my gist on github to get the code and create a file called my_sync_job.plist in ~/Library/LaunchAgents Of course you will need to adapt the placeholder as described before.

Now we can Load the Job by doing this.

launchctl load '~/Library/LaunchAgents/my_sync_job.plist'

Now if you did all correct you can try to make a change in any file in your project folder and i will get synced to the remote server.

You can unload it by doing this

launchctl unload '~/Library/LaunchAgents/my_sync_job.plist'

You could to some more fancy stuff with these tools so be creative 😉