在 Mac 上通过 cron git push 到 github

Ste*_*ner 8 bash git cron github macos

我正在尝试git push在 crontab 运行的 bash 脚本中使用以将提交推送到 github。在脚本的最后,我这样做:

# script processes some files, then:
git add -A
git commit -a -m "Updated $(date)"
git push origin master
Run Code Online (Sandbox Code Playgroud)

添加和提交工作正常(当从 CL 运行脚本时推送工作正常,而不是使用 cron),但我在使用 cron 推送到 github 时出错:

git: 'credential-osxkeychain' is not a git command. See 'git --help'.
fatal: could not read Username for 'https://github.com': Device not configured
Run Code Online (Sandbox Code Playgroud)

我已经搜索了其他线程(如thisthis),但它似乎不是 SSH 的问题(至少我尝试在 crontab 行中传递 SSH_AUTH_SOCK 环境变量,但没有奏效)。运行 OSX 10.8。

有任何想法吗?

Ste*_*ner 7

TL;DR:通过使用 SSH 而不是 HTTPS,通过 cron 解决了 git push 的问题。

感谢 GitHub 的支持团队,我能够解决这个问题。

第一个问题是,git-credential-osxkeychain当 cron 作业运行时,它不在我的路径中。对我来说,git并且git-credential-osxkeychain都用完了 Xcode 命令行工具目录:

$ which git
/Applications/Xcode.app/Contents/Developer/usr/bin/git
$ which git-credential-osxkeychain
/Applications/Xcode.app/Contents/Developer/usr/bin/git-credential-osxkeychain
Run Code Online (Sandbox Code Playgroud)

which git从 cron运行时,结果 cron 是git/usr/bin.

PATH在 cron 作业的顶部添加了适当的目录:

export PATH=$PATH:/Applications/Xcode.app/Contents/Developer/usr/bin
Run Code Online (Sandbox Code Playgroud)

这解决了git: 'credential-osxkeychain' is not a git command错误,但我又遇到了另一个错误:

fatal: could not read Username for 'https://github.com': Device not configured
Run Code Online (Sandbox Code Playgroud)

在 GitHub 支持团队的建议下,我git-credential-osxkeychain通过 SSH 而不是 https 进行身份验证解决了这个问题(根本不需要):

git remote set-url origin git@github.com:username/repo.git
Run Code Online (Sandbox Code Playgroud)