Shammer's Philosophy

My private adversaria

git pull automation on High Sierra

This is required 2 files at least, one is the script to run git pull, and the other is the plist file to invoke the script. Procedure is below.

  1. Generate the script
  2. Generate the plist file in $HOME/Library/LaunchAgent

script sample

#!/bin/bash
while true;
do
    GW_CHECK=`netstat -rn -f inet | grep default | wc -l`;
    if [ 1 -eq $GW_CHECK ];then
	break;
    else
	sleep 1;
    fi
done

ping -c 1 bitbucket.org > /dev/null
if [ $? -eq 0 ];then
    ST="$HOME/Sites";
    WK="$HOME/Workspace";
    array=("$ST/repo1" "$ST/repo2" "$ST/repo3" "$WK/repo1" "$WK/repo2" "$WK/repo3" "$WK/repo4" "$WK/repo5" "$WK/repo6");
    for repository in ${array[@]};
    do
	cd $repository;
	echo "git pull in $repository...";
	git pull;
    done
    exit 0;
else
    echo "ping to bitbucket.org failed."
    exit 1;
fi

There is a possibility that Wifi setup wouldn't be completed at the time above script invoked from Launch Agent, so I add an operation to check network reachability.

plist sample

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTDPLIST1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
  <dict> 
    <key>Label</key> 
    <string>pull.from.bitbucket</string> 
    <key>UserName</key> 
    <string>aaa</string> 
    <key>GroupName</key> 
    <string>staff</string> 
    <key>ProgramArguments</key> 
    <array> 
      <string>/Users/aaa/bin/pull-from-bitbucket.sh</string> 
    </array> 
    <key>StandardOutPath</key> 
    <string>/Users/aaa/pull-from-bitbucket.stdout</string> 
    <key>StandardErrorPath</key> 
    <string>/Users/aaa/pull-from-bitbucket.stderr</string> 
    <key>RunAtLoad</key> 
    <true/> 
  </dict> 
</plist>