Deploy a project using Git push

后端 未结 19 894
囚心锁ツ
囚心锁ツ 2020-11-22 07:06

Is it possible to deploy a website using git push? I have a hunch it has something to do with using git hooks to perform a git reset --hard on the

相关标签:
19条回答
  • 2020-11-22 07:54

    We use capistrano for managing deploy. We build capistrano to deploy on a staging server, and then running a rsync with all of ours server.

    cap deploy
    cap deploy:start_rsync (when the staging is ok)
    

    With capistrano, we can made easy rollback in case of bug

    cap deploy:rollback
    cap deploy:start_rsync
    
    0 讨论(0)
  • 2020-11-22 07:58

    I am using the following solution by toroid.org, which has a simpler hook script.

    on the server:

    $ mkdir website.git && cd website.git
    $ git init --bare
    Initialized empty Git repository in /home/ams/website.git/
    

    and install the hook on the server:

    $ mkdir /var/www/www.example.org
    $ cat > hooks/post-receive
    #!/bin/sh
    GIT_WORK_TREE=/var/www/www.example.org git checkout -f
    GIT_WORK_TREE=/var/www/www git clean -f -d # clean directory from removed files
    
    $ chmod +x hooks/post-receive
    

    on your client:

    $ mkdir website && cd website
    $ git init
    Initialized empty Git repository in /home/ams/website/.git/
    $ echo 'Hello, world!' > index.html
    $ git add index.html
    $ git commit -q -m "The humble beginnings of my web site."
    
    $ git remote add web ssh://server.example.org/home/ams/website.git
    $ git push web +master:refs/heads/master
    

    then to publish, just type

    $ git push web
    

    There is a full description on the website: http://toroid.org/ams/git-website-howto

    0 讨论(0)
  • 2020-11-22 08:02

    After many false starts and dead ends, I'm finally able to deploy website code with just "git push remote" thanks to this article.

    The author's post-update script is only one line long and his solution doesn't require .htaccess configuration to hide the Git repo as some others do.

    A couple of stumbling blocks if you're deploying this on an Amazon EC2 instance;

    1) If you use sudo to create the bare destination repository, you have to change the owner of the repo to ec2-user or the push will fail. (Try "chown ec2-user:ec2-user repo.")

    2) The push will fail if you don't pre-configure the location of your amazon-private-key.pem, either in /etc/ssh/ssh_config as an IdentityFile parameter or in ~/.ssh/config using the "[Host] - HostName - IdentityFile - User" layout described here...

    ...HOWEVER if Host is configured in ~/.ssh/config and different than HostName the Git push will fail. (That's probably a Git bug)

    0 讨论(0)
  • 2020-11-22 08:03

    dont install git on a server or copy the .git folder there. to update a server from a git clone you can use following command:

    git ls-files -z | rsync --files-from - --copy-links -av0 . user@server.com:/var/www/project
    

    you might have to delete files which got removed from the project.

    this copies all the checked in files. rsync uses ssh which is installed on a server anyways.

    the less software you have installed on a server the more secure he is and the easier it is to manage it's configuration and document it. there is also no need to keep a complete git clone on the server. it only makes it more complex to secure everything properly.

    0 讨论(0)
  • 2020-11-22 08:03

    For Deployment Scenario

    In our scenario we're storing the code on github/bitbucket and want to deploy to live servers. In this case the following combination works for us (that is a remix of the highly upvoted answers here):

    1. Copy over your .git directory to your web server
    2. On your local copy git remote add live ssh://user@host:port/folder
    3. On remote: git config receive.denyCurrentBranch ignore
    4. On remote: nano .git/hooks/post-receive and add this content:

      #!/bin/sh GIT_WORK_TREE=/var/www/vhosts/example.org git checkout -f

    5. On remote: chmod +x .git/hooks/post-receive

    6. Now you can push there with git push live

    Notes

    • This solution works with older git versions (tested with 1.7 and 1.9)
    • You need to make sure pushing to github/bitbucket first, so you'll have a consistent repo on live
    • If your .git folder is within document root make sure you hide it from the outside by adding to .htaccess (source):

      RedirectMatch 404 /\..*$

    0 讨论(0)
  • 2020-11-22 08:03

    Sounds like you should have two copies on your server. A bare copy, that you can push/pull from, which your would push your changes when you're done, and then you would clone this into you web directory and set up a cronjob to update git pull from your web directory every day or so.

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