【问题标题】:Git auto-pull using cronjobGit 使用 cronjob 自动拉取
【发布时间】:2011-05-23 18:48:42
【问题描述】:

我试图创建一个 cronjob,其任务是每分钟执行一次git pull,以使我的生产站点与我的主分支保持同步。

由于权限问题,git pull需要系统用户nobody来完成。但是,似乎不允许 nobody 帐户运行命令。所以我必须以root 用户的身份创建任务。

我尝试的 crontab 条目:

*/1 * * * * su -s /bin/sh nobody -c 'cd ~heilee/www && git pull -q origin master' >> ~/git.log

它不起作用,我不知道如何调试它。

有人可以帮忙吗?

UPDATE1:git pull 命令本身是正确的。我可以毫无错误地运行它。

【问题讨论】:

  • 在 shell 中运行命令本身会发生什么?
  • 您有名为git.log的用户吗?
  • @Tom 如果我自己运行命令,它会运行。
  • 您需要更新输出以写入日志的绝对路径。波浪号 (~) 是您的主目录的相对路径。我认为这不会解决您的问题,但您应该以 ... >> /var/log/git.log 结尾
  • 因为git pull 会自动运行git merge,这可能会因冲突而失败,并且会使事情处于对于自动化脚本修复而言非常重要的状态,我强烈不建议这样做任何存储库,它甚至有可能发生除一项工作之外的任何事情,从而导致可能被证明不兼容的更新。请改用git fetch,并定期进行手动合并。

标签: git cron


【解决方案1】:

从 Git 版本 1.8.5 (Q4 2013) 开始,您可以使用 -C 参数轻松完成此操作。然后,您无需 cd 进入 Git 项目目录,而是可以将其指定为 Git 命令的一部分,如下所示:

*/15 * * * * git -C /home/me/gitprojectdir pull

【讨论】:

    【解决方案2】:

    在撰写此答案时,所有建议的解决方案都以 cd'ing 开始进入工作目录。我个人更喜欢这样使用git-dir 参数。

    git --git-dir=/path/to/project/.git pull
    

    出于我自己的目的,我使用这个 cron 来让我的本地开发盒与其他开发者保持同步。因此我使用fetch 而不是pull

    */15 * * * * git --git-dir=/home/andey/path/.git fetch -a
    

    为了简化当前接受的解决方案,使用git-dir 参数

    */1 * * * * su -s /bin/sh nobody -c '/usr/local/bin/git --git-dir=~dstrt/www pull origin master'
    

    【讨论】:

    • git-dir 参数似乎将更改拉入当前目录。我想知道为什么没有提到pushd/popd
    【解决方案3】:

    解决方案:

    */1 * * * * su -s /bin/sh nobody -c 'cd ~dstrt/www && /usr/local/bin/git pull -q origin master' 
    

    【讨论】:

    • 在 ubuntu 10.04 服务器上对我不起作用。 3 个不同的错误(~、/local/ 和 -q 都会导致问题)
    • */1 * * * * su -s /bin/sh nobody -c 'cd /home/dstrt/src/project && /usr/bin/git pull origin master' 对我来说效果更好:~ 扩展更可靠,我的发行版的 git 路径不同,git 的 -q 选项应该在 pull 之后或根本不,否则 git barfs。
    • */1 * * * * /bin/sh -c 'cd /path/to/repo && /usr/bin/git pull -q origin master' 为我工作。
    • */1 * * * * /bin/sh -c 'cd /home/userName/repo && /usr/bin/git pull -q origin master' 对我来说没问题。我的操作系统是 Ubuntu 14.04.02。
    • @kayue - 脚本一步一步做了什么?
    【解决方案4】:

    我创建了一个小脚本来处理它。然后可以使用命令crontab

    crontab -e
    0 2 * * * cd /root && ./gitpull.sh > /root/log/cron.log  2>&1 &
    

    这里是gitpull.sh

    #!/bin/bash
    
    source /etc/profile
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    export TERM=${TERM:-dumb}
    
    #----------------------------------------
    # Please set the following variable section
    # Please set up working directories, use','split
    # eg:path="/root/test/path1,/root/test/path2"
    path=""
    #----------------------------------------
    
    # Do not edit the following section
    
    # Check if user is root
    [ $(id -u) != "0" ] && { echo "${CFAILURE}Error: You must run this script as root.${CEND}"; exit 1; } 2>&1
    
    # Check if directory path exists
    if [[ "${path}" = "" ]]; then 
        echo "${CFAILURE}Error: You must set the correct directory path.Exit.${CEND}" 2>&1
        exit 1
    fi
    
    # Check if command git exists
    if ! [ -x "$(command -v git)" ]; then
        echo "${CFAILURE}Error: You may not install the git.Exit.${CEND}" 2>&1
        exit 1
    fi
    
    # Check where is command git
    git_path=`which git`
    
    # Start to deal the set dir
    OLD_IFS="$IFS" 
    IFS="," 
    dir=($path) 
    IFS="$OLD_IFS" 
    
    echo "Start to execute this script." 2>&1
    
    for every_dir in ${dir[@]} 
    do 
        cd ${every_dir}
        work_dir=`pwd`
        echo "---------------------------------" 2>&1
        echo "Start to deal" ${work_dir} 2>&1
        ${git_path} pull
        echo "---------------------------------" 2>&1
    done
    
    echo "All done,thanks for your use." 2>&1
    

    我们要设置工作目录

    【讨论】:

    • 您必须通过运行 chmod +x gitpull.sh 来使 gitpull.sh 脚本可执行
    【解决方案5】:
    #!/bin/bash
    cd /home/your_folder/your_folder && /usr/bin/git pull git@bitbucket.org:your_user/your_file.git
    

    这是我一直在使用和工作的

    【讨论】:

    【解决方案6】:
    */1 * * * * su -s /bin/sh nobody -c 'cd /home/heilee/src/project && /usr/bin/git pull origin master'
    

    这更正了几个错误,这些错误阻止了接受的答案在我的系统(Ubuntu >10.04 服务器)上工作。关键变化似乎是-qpull 之后而不是之前。在您跟踪 /var/log/syslog 文件或尝试运行未更新的生产代码之前,您不会注意到您的 pull 不起作用。

    【讨论】:

      【解决方案7】:

      虽然您首先需要弄清楚如何让更新正常工作,但最好使用上游的钩子来完成它。您可以简单地使用来自 post-commit 钩子的 curl 来完成此操作,或者如果您使用的是 github,则只需在他们身边使用 post-receive 钩子。

      【讨论】:

      • 钩子比使用 cron 干净得多。钩子只是脚本,所以你可以有类似cd /path/to/production && git pull的东西。
      • 我可以以“无人”的身份运行提交后挂钩吗?
      • cron 是独立的,不涉及通过 Web 服务打开潜在的安全漏洞。为什么你认为 webhook 更好?
      • 一个仅在更改时触发的 webhook 将 a) 更快地部署更改并且 b) 当出现问题时(即使没有任何变化)不会让您有数千个进程在您的机器上消耗大量资源)。
      • 在网络服务器自动旋转的环境中,您甚至不知道 IP 地址将是什么 - 即使它会更干净,创建“挂钩”也会有复杂的任务,但我投票支持 cron git pull 的简单性。
      猜你喜欢
      • 2014-07-30
      • 1970-01-01
      • 1970-01-01
      • 2018-04-30
      • 2021-10-12
      • 2019-07-31
      • 1970-01-01
      • 2020-10-08
      • 2017-11-27
      相关资源
      最近更新 更多