virtualmin / virtualmin-gpl

Virtualmin web hosting control panel for Webmin
https://www.virtualmin.com
GNU General Public License v3.0
331 stars 102 forks source link

How to make git work with a virtual server files? #233

Open alanmels opened 3 years ago

alanmels commented 3 years ago

I've configured git per steps on https://www.virtualmin.com/documentation/web/git and believed I'd be able to work with the website's files under which git repository was created.

However, browsing git repository shows empty, switching to Repository Commands is showing commands for new and empty repository. Trying to add the /home/username/public_html to repository with git add command naturally gives the fatal: '/home/username/public_html' is outside repository error, because the document root for the virtual server is indeed outside of git repository directory.

Could you please explain how we could make Virtualmin's Git Repository plugin work with the document root of virtual server under which it was created?

jcameron commented 3 years ago

This isn't supported right now, sorry. You'd need to write a separate script to copy files from under git to the public_html dir when you're ready to publish the site.

alanmels commented 3 years ago

Wait, but what's the use of behind-the-scenes repository if developers can't see the real websites (which means virtual servers should work and be available in browser)? How they are supposed to work with their websites? Isn't Virtualmin for hosting websites that are available for browsing? If so then git repositories should be able to work with those websites, aren't they?

alanmels commented 3 years ago

For all readers of this thread in the future, please read this article to get things sorted out: http://sysadmin.circularvale.com/server-config/auto-deploy-your-virtualmin-website-with-git:

Virtualmin’s non-standard implementation of git can be a little awkward to work with, primarily because git itself assumes too many things about the environment by default. However, there is a very easy way to auto-deploy your website from a virtualmin git repo to your virtualmin website root (public_html) using git hooks.

This assumes you have installed git onto your virtualmin server, and will be accessing the remote (virtualmin) repository over ssh (you can also use any protocol that works with git hooks; http doesn’t!)

  1. Create a new virtual server in virtualmin, and set up a repository for it
  2. On the virtualmin server (ssh into it, or whatever method you prefer), navigate to the git directory (/home/sitename/public_html/git/git.git/), and execute the following commands:
  3. git config core.bare false
  4. git config receive.denycurrentbranch ignore
  5. Then go to add the hook; go into the hooks directory, create a file called post-receive, and add the following:
  6. !/bin/sh

  7. cd /home/sitename/public_html/
  8. git –git-dir=/home/sitename/public_html/git/reponame.git –work-tree=/home/sitename/public_html/ checkout -f
  9. Add execute permissions on the hook, and ensure the ownership is correct (www-data:sitename)
  10. And that’s it…get the url for your repo (something like sitename@siteurl:public_html/git/git.git), and set it up on your local copy of git, then create some files and push from local to remote and it should automatically deploy – by the way, you will need to use “git push origin master” the first time you push to the empty repo

I think this will just deploy the master branch, and I don’t think it will delete files that are deleted locally (not sure exactly how the checkout command works).

I’ll be adding another tutorial that incorporates branches and proper sync between the two.

alanmels commented 3 years ago

Hi Jamie,

I've got it working by implementing post-receive script just the way described on https://gist.github.com/noelboss/3fe13927025b89757f8fb12e9066f2fa.

Do you thinks it's possible to incorporate the suggested post-receive script into the ../git/repository.git/hook` directory when a repository created?

#!/bin/bash
TARGET="/home/webuser/deploy-folder"
GIT_DIR="/home/webuser/www.git"
BRANCH="master"

while read oldrev newrev ref
do
    # only checking out the master (or whatever branch you would like to deploy)
    if [ "$ref" = "refs/heads/$BRANCH" ];
    then
        echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
        git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f $BRANCH
    else
        echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
    fi
done