在Ubuntu中通过Cron拉动Git Pull然后通过Push [英] Git Pull and then Push via Cron in Ubuntu

查看:159
本文介绍了在Ubuntu中通过Cron拉动Git Pull然后通过Push的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些情况:我在git企业环境中,并且有一个git repo,我在其中拉出整个代码,进行gulp编译,然后将发行版复制到另一个仓库中,然后将其推送到远程.

Some context: I am in a git enterprise environment and there is a git repo where I pull the whole code, gulp build it and then copy the distribution to another repo, where I push it to remote.

我已经将bash脚本中的所有操作自动化了,每当我执行sh gittogulp.sh时,它就可以做到完美无缺.但是,当我使用cronjob进行此操作时,它无法从git中提取新内容,并无法从已经存在的内容中生成gulp.

I have automated all that in a bash script and whenever I execute sh gittogulp.sh it does that flawlessly. But when I use a cronjob to do that, it fails to pull new content from git and makes gulp builds from content that is already there.

我想知道,如何在不手动触发脚本的情况下从git repo中提取信息. Webhooks对我来说不是一个选择. :(

I want to know, how can I pull from a git repo without manually triggering the script. Webhooks is not an option for me. :(

gittogulp.sh是:

#!/bin/bash
cd ~/mainrepo
git checkout dev
git fetch
echo "Pulling latest code from git repo"
git pull

for commitid in $(git log --format="%h" -n 10)
do
    git checkout dev
    echo "git reset hard"
    git fetch --all
    git reset --hard origin/dev
    echo "Checkout commit id: ""$commitid"
    echo git checkout "$commitid"
    git checkout "$commitid"
    echo "Installing dependencies"
    npm install
    echo "Gulpifying the code"
    gulp dev
    basefolder="/root/mainrepo/manifests"
    basedestfolder="/root/outputrepo/dev"
    if [ -d "$basefolder" ]; then

        versionrepo=`cat "$basefolder"/version.txt`
        echo "Version in repo: ""$versionrepo"
        destfolder="$basedestfolder""/""$commitid"

        if [ -d "$destfolder" ]; then
            echo "Latest outputs are already in place"
            break
        else
            echo "copying latest output to ""$destfolder"
            mkdir "$destfolder"
            cp -r "$basefolder" "$destfolder"
            zipname="$versionrepo""_""$commitid"".zip"
            zipdestpath="$basedestfolder""/""$zipname"
            echo "Making new zip from latest commit"
            echo zip -r "$zipdestpath" "$destfolder"
            zip -r "$zipdestpath" "$destfolder"
        fi

    else
        echo "$basefolder"" : folder doesn't exist, exiting"
    fi
done

cd ~/outputrepo
git pull
git add .
git commit -m "adding new zip"
git push

推荐答案

也许您不能将.sh文件用作更改目录的终端,因此,我向您提供以下方法(即第1行, .sh文件中的2):

Maybe you can not using .sh file as a terminal for change directory, for this reason I offer to you that using following method (i.e. instead line 1,2 in your .sh file):

git --git-dir=/home/<User>/<your_path>/.git checkout dev
...

,然后按照以下操作获得原始答案:不在git目录中时进行git pull

and following this for original answer: git pull while not in a git directory

另一种更好的方法是使用fabfile(用于python):
使用fabfile的git命令

Another and better way to do this, is using fabfile (for python):
git commands using fabfile

例如(很简单):
fabfile.py:

For example (as simple):
fabfile.py:

from fabric.api import *

env.user = 'your_user'
env.hosts = ['127.0.0.1']
env.password = 'you_pswd'

@task
def pull():
    code_dir = '/srv/django/myproject'
    with cd(code_dir):
        run("git pull")

另一个示例(本地):

.
|-- __init__.py
|-- app.wsgi
|-- fabfile.py <-- our fabfile!
|-- manage.py
`-- my_app
    |-- __init__.py
    |-- models.py
    |-- templates
    |   `-- index.html
    |-- tests.py
    |-- urls.py
    `-- views.py

fabfile.py:

fabfile.py:

from fabric.api import *

@task
def push():
    local("git add -p && git commit")
    local("git push")

最后:
$ fab pull
$ fab push

Finally:
$ fab pull
$ fab push

这篇关于在Ubuntu中通过Cron拉动Git Pull然后通过Push的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆