-->

Pushing to Hosted project above webroot

2019-04-12 21:41发布

问题:

I have a CakePHP app hosted on DreamHost - and a clone of it locally on my MacBook.

I'm trying to set up my environment so that I can develop on the MacBook and push to the hosted site as necessary - but can't figure out how to set up gitto push from local to remote when the remote files are above the webroot.

How do I set up my local git to push to files above webroot on the hosted app?

Thanks!

回答1:

but can't figure out how to set up git to push from local to remote when the remote files are above the webroot.

To setup the repos, you will need an intermediate repo to serve as the reference point. Now this repo could be on a hosting site like github or bitbucket, or it could be a bare repo on your server itself. (Note - bitbucket has free private repos for small number of users)

The advantage of a bare repo is that you can circumvent any issues related to setting up ssh mechanism between the server, the online service and your dev machine. The cons would be losing all the code on the server in case your server goes down, though you will still have a copy of it on your dev machine, so not all will be lost.

Step 1

So let us assume you move ahead with a bare repo. Ignore this step if you will be using bitbucket/github.

For this, we need to create a bare repo as follow on your server:

ssh user@myserver
cd /some/isolated/location
git init --bare intermediate_repo.git

This creates an intermediate repo with path /some/isolated/location/intermediate_repo.git.

Next, cd inside this bare repo, and run the following commands, so that you can access the repo over ssh as a git server

cd intermediate_repo.git
mv hooks/post-update.sample hooks/post-update
chmod a+x hooks/post-update
git update-server-info

So now your bare repo is good to go as the intermediate repo, and we can add it as remote in other repositories.

Step 2

Now in your dev machine, add a remote (Step2)

git remote add intermediate ssh://user@myserver:/some/isolated/location/intermediate_repo.git

Step 3

And on your servers repo, add a remote

ssh user@myserver
cd /repo/location
git remote add intermediate /some/isolated/location/intermediate_repo.git

Step 4

So now you are good to go ().

You can push your code as following from your dev machine

git push intermediate branchname

and pull it into your configured code repo as

ssh -t user@myserver "cd /repo/location && git pull intermediate branchname"

PS

If you choose to go with a bitbucket installation, then skip step 1, and in steps 2 & 3 above, replace the url/path with the bitbucket ssh url. Check out this bitbucket link for setting up ssh access with bitbucket. Also, while generating ssh keys, generate them without passphrases, otherwise you might have to debug more stuff around ssh and passphrases.

Your Views folder should be available to you at location /repo/location/Views, which you can configure for webroot access.



回答2:

This is a great question, and the best I can offer you is a workaround, but it's what works for our team. You'll need to host your code with a third-party service like GitHub or BitBucket for this to work.

We write/test code locally, and push to GitHub. When we're ready to deploy, we run a script along these lines:

echo "Connecting to mysite.com";
# DocumentRoot is /var/www/site
ssh -t myUser@myIPAddress "cd /var/www; git pull;" 

This connects to the server via ssh, and executes two commands, one cding to the git directory, and the other one pulling the changes. Once git pull has finished, the connection exits. (The -t flag forces pseudo-tty allocation, allowing us to execute those two commands).

We've created a 'Machine' user at GitHub along the lines outlined here so we can use key-based auth and not worry about tossing passwords around.

Does this help? What more can I add/explain?



回答3:

If you have ssh access, you can push your repo to the server to a directory of your choice, and then either create a symlink from the web root folder to the views folder in that directory or write a script and rsync them. This script can be implemented in a post-hook on the remote git repo.



回答4:

Have you investigated using SSH for the git push? SSH would have access to everywhere on the host machine that your account would have access to. We use it all of the time for pushes outside of the webroot.