git 从 GitHub 推送到 Web 服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17367247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 08:42:22  来源:igfitidea点击:

Pushing from GitHub to a Web Server

gitgithubftp

提问by Andrew

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?

我想将我所有的网站移到 GitHub 以获得所有明显的好处。我不是命令行的忠实粉丝,所以我喜欢 GitHub,因为它可以让我避免接触所有这些。在 GitHub 上设置存储库,然后将其与我的计算机同步非常简单。我需要能够自动从 GitHub 推送到网络服务器,所以当我在本地将某些内容更新到主分支时,将它与 GitHub 同步,它会在网站上上线。从谷歌搜索这个主题来看,似乎大多数技术要么需要命令行,要么看起来相当复杂。我想对大约 15-20 个站点(其中许多托管在来自不同客户端的不同服务器上)执行此操作。我想找到一个在我的技能范围内并且每个站点不需要 2-3 小时的选项。任何人都知道设置它的最佳,最简单的方法?

回答by VonC

The part which is complex is the webhook on GitHub

复杂的部分是GitHub 上webhook

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

每当将存储库推送到时,每个 GitHub 存储库都可以选择与 Web 服务器进行通信。

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

这意味着您的网站必须有一个进程来监听 GitHub 在收到提交时发送的那些 JSON 消息。

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

您可以看到这些侦听器的多个示例,例如这个webhook-deployer,带有 auto.php(用于 php 服务器):

<?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:

这GitHub的项目建议,没有密码的SSH密钥,这我同意,在第一次(来测试它)。
但是,特别是对于私人项目,最好运行 ssh-agent 并管理受保护的 ssh 密钥密码。
正如 janos评论的那样

  • 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.
  • 如果 GitHub 存储库是公开的,那么他甚至不需要一个。
  • 如果 repo 是私有的,那么他需要它,但这不应该掉以轻心。如果可能,他应该使用关键代理。
    如果这太复杂了,那么他可以使用一个没有密码的专用 SSH 密钥来进行此部署,并且该密钥不应离开部署 PC。

回答by Dima Sabanin

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.

我知道这张票是旧的,但对于那些以某种方式找到它的人,请查看 dploy.io。这是一项托管服务,专门用于将您的存储库从 GitHub/Bitbucket 部署到您的服务器。它支持 SFTP/FTP/S3/Heroku/SSH 命令等。

Disclaimer: I work on dploy.io

免责声明:我在 dploy.io 上工作

回答by Johannes Hoppe

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)

你可能想看看这个 PHP 脚本:
https: //github.com/JohannesHoppe/easy-git-deploy
(它为你做了一个 git clone、git pull、git push)

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

几年来,我使用该脚本管理我所有的 wordpress 安装。

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:

提示:如果您使用的是共享主机环境,则脚本限制可能会中断第一次执行。在这种情况下,通过 SSH 登录并手动执行第一个克隆:

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

Here you can also manually confirm the SSH key fingerprint.

这里也可以手动确认SSH密钥指纹。

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

第二个提示:您应该使用 .htaccess / .htpasswd 文件保护目录。