【问题标题】:fatal: could not read Username When pushing to git using a cron job致命:使用 cron 作业推送到 git 时无法读取用户名
【发布时间】:2019-08-24 20:42:31
【问题描述】:

嘿,我正在尝试使用 Mac 上的 cron 作业将代码推送到 git。我使用crontab -e 编辑了我的 crontab,并在里面有以下内容:

* 12 * * 1 ~/Dropbox/MD/sync.sh
* 12 * * 5 ~/Dropbox/MD/sync.sh

脚本如下:

#!/bin/bash

cd ~/Dropbox/MD
/usr/bin/git add .
/usr/bin/git commit -m "Docs auto update"
/usr/bin/git push origin master

但是命令失败,当我运行mail 时,我可以检查错误消息:fatal: could not read Username for 'https://github.com': Device not configured。我该如何解决这个问题?如果我从终端手动运行命令,我没有问题,也不会提示我输入用户凭据。

对此的任何指示将不胜感激。谢谢!

【问题讨论】:

标签: bash git macos cron


【解决方案1】:

建议使用 SSH 密钥自动执行 git 任务。您可以按照以下步骤设置具有推送访问权限的 SSH 密钥:

  1. 获取用户的SSH密钥(或生成一个:ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  2. 复制公钥的内容 (cat ~/.ssh/id_rsa.pub)
  3. 转到 github.com 上的项目
  4. 进入项目设置
  5. 选择Deploy Keys (https://github.com/user/project/settings/keys)
  6. 选择添加部署密钥
  7. 添加您的公钥内容
  8. 检查Allow write access
  9. 选择Add Key

请注意,这仅适用于使用以下遥控器克隆的存储库:git@github.com:user/project.git

遥控器可以通过以下步骤更新:

  1. 转到服务器上的存储库文件夹
  2. 运行git remote remove origin
  3. 运行git remote add origin git@github.user/project.git

【讨论】:

  • 非常感谢!对于生成用户的 SSH 密钥的第一步,电子邮件是 GitHub 帐户电子邮件吗?
  • 这不是必需的,电子邮件并不真正相关。我可以是任何电子邮件,它可以帮助跟踪添加到 github 时公钥的来源。 @Kex
【解决方案2】:

如果我从终端手动运行命令,我没有问题,也不会提示我输入用户凭据。

如果您的 cron 作业没有像您一样运行,就会出现这种情况。

Git 会检查 git config credential.helper 的值以查看 github.com 用户名/密码是否被缓存(通常是 OSX Keychain for Mac)。
例如,如果 cron 作业以 root 身份运行,则不会配置该设置。

注意:检查是否为plist in $HOME/Library/LaunchAgents would work better,因为launchd might replace cron

【讨论】:

    猜你喜欢
    • 2023-04-08
    • 2016-10-13
    • 1970-01-01
    • 2019-06-15
    • 1970-01-01
    • 1970-01-01
    • 2012-11-14
    • 2019-11-11
    相关资源
    最近更新 更多