Checkout latest git commit automatically

Phanix
·
·
IPFS
·

If you want to automate the branch that gets the last commit (probably only on the staging/testing server), you can use git command plus shell (sed, grep) crontab to complete. Of course, if it is gitlab, gitlab-runner can help, but it seems to be more intuitive to use shell script (?)

 #!/bin/bash
#filename = /var/www/my.sh
#expect="remotes/origin"
#sub="git checkout "

branches=$(git branch -a --sort=-committerdate | grep 'remotes/origin' | grep -v 'HEAD' | grep -v 'master' | head -n 1 | sed -e 's/^ */ /' -e 's/ *$//' -e 's/remotes\/origin\//git\ checkout\ /')
#echo "$branches"
eval "cd /var/www/mywww; $branches"
#You can do other things you want to do next

Adding the --sort parameter to the git branch command can specify what future to sort according to, and committerdate is the date and time, and adding a minus sign (-) means descending.

Then create a passwordless ssh key and add it to the account that can pull repo, and then create a crontab and you are done.

 5-55/10 * * * * cd /var/www/mywww; ssh-agent bash -c 'ssh-add /home/user/.ssh/nopwd; git pull --all'; sh /var/www/ my.sh

Original link: Phanix's Blog

CC BY-NC-ND 2.0

Like my work? Don't forget to support and clap, let me know that you are with me on the road of creation. Keep this enthusiasm together!