using git to deploy my node.js app to my production server

前端 未结 2 2012
自闭症患者
自闭症患者 2020-12-06 03:45

I have a local git repository on my development machine.

I want to use git to push the code up to my production server. and trigger a restart of the web server once

相关标签:
2条回答
  • 2020-12-06 04:23

    I would recommend a post-receive hook on a bare repo on your server, with:

    GIT_DIR=/path/to/bare/repo/.git 
    GIT_WORK_TREE=/var/www/www.example.org git checkout -f
    

    By pushing to a bare repo, you won't have any issue with a working tree out of sync with what you are pushing (since a bare repo has no working tree).
    Then, you would checkout your bare repo directly on the live site directory structure (through the post-receive hook).

    This is similar to "git GIT_WORK_TREE post-receive hook deployment remote".
    Setting the GIT_DIR avoids the issue mentioned in "Git checkout in post-receive hook: “Not a git repository '.'”".

    0 讨论(0)
  • 2020-12-06 04:26

    It is usually not a good idea to push code directly to a live website. For this you can create a bare repository on your server, outside the public directory. This repository will push changes to your Live website repository and will be like the gate keeper for your Website. To set things up, create a repository around your live website directory:

    $ cd ~/www
    $ git init
    $ git add .
    $ git commit -m"initial import of pre-existing web files"
    

    With this created, move to a directory not accessible via HTTP. To keep things simple,lets call this bare directory HUB and the website directory LIVE. Initialize a bare repository here:

    $ cd; mkdir site_hub.git; cd site_hub.git
    $ git --bare init
    

    Then, from inside your live website working directory, add this bare repository as a remote and push live website’s master branch.

    $ cd ~/www
    $ git remote add hub ~/site_hub.git
    $ git remote show hub
    * remote hub
      URL: /home/rizwan/site_hub.git
    $ git push hub master
    

    You need hooks to commit changes to the live repository. Create a post-update hook inside the HUB repository :

    #!/bin/sh
    
    echo
    echo "**** Pulling changes into Live [Hub's post-update hook]"
    echo
    
    cd $HOME/www || exit
    unset GIT_DIR
    git pull hub master
    
    exec git-update-server-info
    

    Inside this hook, you can have the code to restart the server after the pull is completed.

    Also, create a post-commit hook on the LIVE repo to send changes done to the live website back to HUB.

    #!/bin/sh
    
    echo
    echo "**** pushing changes to Hub [Live's post-commit hook]"
    echo
    
    git push hub
    

    On your local machine, add the HUB repository as a remote and push changes to it:

    git remote add hub <hub-repository-url>
    

    How this works is, you write some code and push it to the bare repository, which using its post-update hook to push changes to the live respository and restart the server.

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