How to set up a Git hook so that after pushing to ssh://peter@foo.com/~/bar.com.git, it will go to ~/bar.com and do a git pull?

后端 未结 2 853
-上瘾入骨i
-上瘾入骨i 2020-12-08 03:01

I was advised to set up on a remote server

foo.com/~/bar.com       # live webpage content
foo.com/~/bar.com.git   # a bare repo

so, from my

相关标签:
2条回答
  • 2020-12-08 03:21

    I use the following post-update script (make sure you set executable bit on it):

    #!/bin/sh
    rm -rf ~/public_html/xxx
    git-archive --format=tar --prefix=xxx master | tar x -C ~/public_html
    

    It does the thing, but there's a short window when the site is not available. Doing git pull directly in yours ~/bar.com will expose git's .git directory, so make sure you either block it in your http server, or keep .git somewhere higher in directory hierarchy, ie. something like:

    ~
     \
      - repo
        \
         - .git
         - bar.com
           \
            - your content
    
    0 讨论(0)
  • 2020-12-08 03:25

    You can add a post-receive hook to the ~/bar.com.git repo for this. To do this add to the ~/bar.com.git/hooks/ directory an executable file post-receive with the content:

    #!/bin/sh
    
    unset $(git rev-parse --local-env-vars)
    cd ~/bar.com
    git pull
    

    Make sure the post-receive file has the executable bits(e.g. 755).

    Now whenever something is pushed to the ~/bar.com.git repo, the ~/bar.com repo is updated automatically.

    See also

    • getting "fatal: not a git repository: '.'" when using post-update hook to execute 'git pull' on another repo
    • Git - post-receive hook with git pull “Failed to find a valid git directory”

    to understand why unsetting some environment variables is necessary.

    0 讨论(0)
提交回复
热议问题