1

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.

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.

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 is:

#!/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
naqushab
  • 754
  • 1
  • 8
  • 24

1 Answers1

1

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
...

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

EDIT:

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

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")

Another example (as locally):

.
|-- __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")

Finally:
$ fab pull
$ fab push

Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150