Pushing from GitHub to a Web Server

2019-03-09 19:51发布

问题:

I am wanting to move all of my sites to GitHub for all of the obvious benefits. I am not a big fan of the command line, so I like GitHub because it allows me to avoid touching all of that. Setting up a repository on GitHub and then syncing it with my computer is easy enough. I need to be able to push from GitHub to the webserver automatically though so when I update something locally to the main branch, sync it with GitHub, it goes live on the site. From Googling the subject it seems like most of the techniques either require the command line or seem fairly complex. I want to do this with about 15-20 sites (many of which are hosted on different servers from different clients). I want to find an option that is within my skill set and doesn't take 2-3 hours per site. Anyone know of the best, easiest way to set this up?

回答1:

The part which is complex is the webhook on GitHub

Every GitHub repository has the option to communicate with a web server whenever the repository is pushed to.

That means your web site must have a process listening to those JSON messages sent by GitHub upon reception of a commit.

You can see multiple examples of those listeners, like this webhook-deployer, with an auto.php (for a php server):

<?php

 // Prevent accidental XSS
 header("Content-type: text/plain");

 // Run the script 
 if ( $_POST['payload'] ) {
  shell_exec("./pull.sh");
}

That GitHub project recommends an SSH key with no passphrase, which I agree at first (to test it out).
However, especially for private projects, it is best to run an ssh-agent and manage an ssh key passphrase protected.
As janos comments:

  • If the GitHub repository is public, then he doesn't even need one.
  • If the repo is private, then he needs it, but this should not be taken so lightly. If possible he should use a key agent.
    If that's too complicated then he could use a dedicated SSH key without passphrase just for this deployment, and that key should never leave the deployment PC.


回答2:

I know this ticket is old, but for those who find this somehow, checkout dploy.io. It's a hosted service made specifically for the purpose of deploying your repo from GitHub/Bitbucket to your server. It supports SFTP/FTP/S3/Heroku/SSH commands and more.

Disclaimer: I work on dploy.io



回答3:

You might want to take a look at this PHP script:
https://github.com/JohannesHoppe/easy-git-deploy
(It does a git clone, git pull, git push for you)

Since several years I manage all my wordpress installations with that script.

Hint: If you are using a shared hosting environment, than script limits might break the first execution. In that case, login via SSH and do the first clone manually:

git clone 'https://user:passwort@//github.com/user/repo.git'

Here you can also manually confirm the SSH key fingerprint.

Second hint: You should secure the directory with a .htaccess / .htpasswd file.



标签: git github ftp