某些上下文:我在一个git企业环境中,有一个git repo,我将整个代码拉出来,gulp构建它,然后将发行版复制到另一个repo,我把它推送到远程。
我已经在bash脚本中实现了所有这些功能,每当我执行sh gittogulp.sh
时,它都会完美无缺。但是当我使用cronjob来做到这一点时,它无法从git中提取新内容,并从已经存在的内容中进行gulp构建。
我想知道,如何在不手动触发脚本的情况下从git repo中提取。 Webhooks对我来说不是一个选择。 :(
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
答案 0 :(得分:1)
也许您不能将.sh
文件用作更改目录的终端,因此我向您提供使用以下方法(即代替.sh
文件中的第1,2行): / p>
git --git-dir=/home/<User>/<your_path>/.git checkout dev
...
并在此之后获得原始答案:git pull while not in a git directory
编辑:
另一种更好的方法是使用fabfile(用于python):
git commands using fabfile
例如(简单):
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:
from fabric.api import *
@task
def push():
local("git add -p && git commit")
local("git push")
最后:
$ fab pull
$ fab push