You can easily automate your code deployment using crontab and git.
Lets assume you push your code to your remote PROD branch and a cronjob on prod keeps running or runs at a certain time checks if your branch is behind the remote or not and if it is, then it pulls code and restarts your application.
To start off make sure you have both git and a git repository. If you do then you can skip to Automation, if not then start by installing git and initializing a git repository on your local. For Ubuntu, here is how,
sudo apt-get update sudo apt-get install git cd /your-path-to-git sudo mkdir project cd project git init git remote add origin https://github.com/user/repo.git git pull origin git checkout -b branch-name git add * git commit -a -m "initial commit" git push origin branch-name
Note: replace ‘https://github.com/user/repo.git’ with your HTTPS git repo URL.
If you see an error that remote already exists then you can use the following to remove it and then add your new repository under the same name or you can add a new name
git remote rm origin git remote add origin https://github.com/user/repo.git
or
git remote add newremotename https://github.com/user/repo.git git remote add origin https://github.com/user/repo.git
Automation
Replace ‘/your-path-to-git/project’ with the path to your git project.
replace ‘branch-name’ with the branch you want to automate.
Save this in a file called automate.sh.
cd /your-path-to-git/project git checkout -b branch-name git remote update UPSTREAM=${1:-'@{u}'} LOCAL=$(git rev-parse @) REMOTE=$(git rev-parse "$UPSTREAM") BASE=$(git merge-base @ "$UPSTREAM") if [ $LOCAL = $REMOTE ]; then echo "Up-to-date" elif [ $LOCAL = $BASE ]; then echo "Need to pull" sh pull_code.sh fi
Replace both the occurences of ‘branch-name’ with your required branch name and then save the code in a file named ‘pull_code.sh’ in the same directory as the ‘automate.sh’.
git reset --hard origin/branch-name git pull branch-name
If you run the file using the following command then it should check for changes in the remote branch and pull them. Make sure to change ‘branch-name’ to whatever branch name you are using.
sudo sh automate.sh "remote/origin/branch-name"
Make the two files executable by the user
sudo chmod u+x *.sh
Cronjob
The next step would be to create a cronjob for this using crontab.
Use the following command for that,
crontab -e
Then hit ‘i’ start editing using vi (Visual Editor – The default editor that comes with the UNIX operating system).
# MAILTO="your-email@example.com" # This is a comment * * * * * /yourpath/automate.sh > /dev/null 2>&1
You can add your email in the MAILTO parameter. But this works only if you don’t have logging set, here we have set all the output and standard error output to null, so it won’t work. For the email functionality to work, you need to have the mail server setup for this.
To log all the output to a file, replace the job with the following.
* * * * * /yourpath/automate.sh >> /var/log/cron.log 2>&1
After adding the above code to your crontab, use the following to write changes and quit the vi.
‘ESC’ + ‘wq!’
This job is set to run every minute. It should have already output some data into your logs. Just to be sure we can restart the cron service once after the changes are saved.
sudo service crond restart