我正在尝试使用以下示例代码向远程 Git 服务器提交/推送更改:
#!/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}
Run Code Online (Sandbox Code Playgroud)
如果我手动调用 shell 脚本(例如./commit.sh
),它会立即工作。相反,当通过 cron 作业触发时,一切正常,直到由于git push
某种奇怪的原因似乎永远不会被触发。
这是我的 crontab 行:
*/20 * * * * /home/username/Sites/git/repo/commit.sh
Run Code Online (Sandbox Code Playgroud)
和一些冗长的 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
Run Code Online (Sandbox Code Playgroud)
为什么git push
只有在手动调用脚本而不是通过 crontab 运行时才会触发?
您希望在哪个导演(IES|y)中执行此操作?从您的脚本中不清楚。尝试进入目录1,例如:
#!/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
Run Code Online (Sandbox Code Playgroud)
或者你可以做一个git add --all /path/to/git/repo/files
.
编辑:
从命令行运行脚本不一定与从 cron 运行相同;请参阅此问答:当 cron 执行作业时,“工作目录”是什么,尤其是Gilles 的回答。
1关于如何检查命令是否成功的一些想法