Shell – Pushing to a Git repo from shell script

cronshell-script

I'm trying to commit/push changes to a remote Git server with the below sample code:

#!/bin/sh

USER='username'
REPO='/home/'${USER}'/Sites/git/bitbucket/kb'
COMMIT_TIMESTAMP=`date +'%Y-%m-%d %H:%M:%S %Z'`
DATELOG=`date +'%Y-%m-%d-%H-%M-%S'`
LOG="/tmp/${DATELOG}.txt"

MKDOCS=`which mkdocs`
GIT=`which git`
NOTIFY=`which notify-send`

# Only proceed if we have a valid repo.
if [ ! -d ${REPO}/.git ]; then
  echo "${REPO} is not a valid git repo! Aborting..." >> ${LOG}
  exit 0
else
  echo "${REPO} is a valid git repo! Proceeding..." >> ${LOG}
fi

cd ${REPO}
${MKDOCS} build --clean >> ${LOG}
${GIT} add --all . >> ${LOG}
${GIT} commit -m "Automated commit on ${COMMIT_TIMESTAMP}" >> ${LOG}
${GIT} push git@bitbucket.org:username/repo.git master >> ${LOG}

# Depends on libnotify
${NOTIFY} 'KB notification' 'Changes were pushed to Bitbucket.' --icon=dialog-information >> ${LOG}

If I invoke the shell script manually (e.g. ./commit.sh) it works immediately. On the contrary, when triggered via a cron job everything works just fine until the git push which then never seems to be triggered for some odd reason.

Here's my crontab line:

*/20 * * * * /home/username/Sites/git/repo/commit.sh

And some verbosity for git push

09:53:32.732216 git.c:349               trace: built-in: git 'push' 'git@bitbucket.org:username/repo.git' 'master'
09:53:32.732514 run-command.c:341       trace: run_command: 'ssh' 'git@bitbucket.org' 'git-receive-pack '\''username/repo.git'\'''
09:53:39.665197 run-command.c:341       trace: run_command: 'pack-objects' '--all-progress-implied' '--revs' '--stdout' '--thin' '--delta-base-offset' '--progress'
09:53:39.665526 exec_cmd.c:134          trace: exec: 'git' 'pack-objects' '--all-progress-implied' '--revs' '--stdout' '--thin' '--delta-base-offset' '--progress'
09:53:39.666778 git.c:349               trace: built-in: git 'pack-objects' '--all-progress-implied' '--revs' '--stdout' '--thin' '--delta-base-offset' '--progress'
Counting objects: 7, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (7/7), 4.23 KiB | 0 bytes/s, done.
Total 7 (delta 4), reused 0 (delta 0)
To git@bitbucket.org:username/repo.git
   0ef4905..91437d0  master -> master

How come git push would be triggered only when the script is invoked manually and not when run via the crontab?

Best Answer

What director(ies|y) are you looking to do this in? It is not clear from you script. Try going into the directory1, something like:

#!/bin/sh

GIT=`which git`
REPO_DIR=/home/username/Sites/git/repo/
cd ${REPO_DIR}
${GIT} add --all .
${GIT} commit -m "Test commit"
${GIT} push git@bitbucket.org:username/repo.git master

Or you can do a git add --all /path/to/git/repo/files.

Edit:

Running a script from the command line is not necessarily the same as running it from cron; see this Q&A: What is the 'working directory' when cron executes a job, especially Gilles' answer.

1Some ideas on how to check if a command succeeded

Related Question