git 推送时自动从 GitHub 部署到服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7303518/
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-10 11:46:36  来源:igfitidea点击:

Automatically Deploy From GitHub To Server On Push

gitdeploymentgithub

提问by Justin

We have a VPS on linode, and code hosted on gitHub. How do we setup so when we push to gitHub, it also pushes automatically to our linode server. We are using PHP on the linode server.

我们在 linode 上有一个 VPS,在 gitHub 上托管了代码。我们如何设置当我们推送到 gitHub 时,它也会自动推送到我们的 linode 服务器。我们在 linode 服务器上使用 PHP。

Thanks.

谢谢。

采纳答案by Kristian Glass

You probably want to use GitHub's post-receive hooks.

您可能想使用GitHub 的 post-receive hooks

In summary, GitHub will POST to a supplied URL when someone pushes to the repo. Just write a short PHP script to run on your linode VPS and pull from GitHub when it receives said POST.

总之,当有人推送到 repo 时,GitHub 将 POST 到提供的 URL。只需编写一个简短的 PHP 脚本即可在您的 linode VPS 上运行,并在收到所述 POST 时从 GitHub 中提取。

回答by Karl

I wrote a small Github-Auto-Deployserver in python, that does exactly what you want.

我用 python编写了一个小型Github-Auto-Deploy服务器,它完全符合您的要求。

  • Enter your domain to a new post-receive service hookon Github
  • Match local repository paths with repository urls in the config file
  • The server will receive requests from github and run git pull in local repository path
  • It also runs a shell script for deployment afterwards if you provide one
  • 将您的域输入到Github 上的新接收后服务挂钩
  • 将本地存储库路径与配置文件中的存储库 url 匹配
  • 服务器将接收来自 github 的请求并在本地存储库路径中运行 git pull
  • 如果您提供一个 shell 脚本,它还会在之后运行一个 shell 脚本以进行部署

回答by jesal

I ended up creating my own rudimentary deployment tool (much like Karl but in PHP) which would automatically pull down new updates from the repo - https://github.com/jesalg/SlimJim- Basically it listens to the github post-receive-hook and uses a proxy to trigger an update script.

我最终创建了自己的基本部署工具(很像 Karl,但在 PHP 中)它会自动从 repo 中提取新的更新 - https://github.com/jesalg/SlimJim- 基本上它会监听 github post-receive- hook 并使用代理来触发更新脚本。

回答by user2131283

Maybe i'm out of context but i prefer to manually choose where to push from my command line eg: git push linode

也许我脱离了上下文,但我更喜欢从命令行手动选择推送的位置,例如: git push linode

To do this i create a repository container in my linode server and created a post-receive hook that checkouts my folder to the last pushed commit

为此,我在我的 linode 服务器中创建了一个存储库容器,并创建了一个 post-receive 钩子,将我的文件夹签出到最后一次推送提交

Create a git repo container mkdir /var/repo && cd /var/repo git --bare init

创建一个 git repo 容器 mkdir /var/repo && cd /var/repo git --bare init

Create the post-receive hook in /var/repo/hooks/touch post-receive nano post-receive chmod +x post-receive

在中创建 post-receive 钩子 /var/repo/hooks/touch post-receive nano post-receive chmod +x post-receive

post-receive content #!/bin/sh git --work-tree=/var/www/ --git-dir=/var/repo checkout -f

接收后内容 #!/bin/sh git --work-tree=/var/www/ --git-dir=/var/repo checkout -f

On your local repositorygit remote add linode root@<linode ip|domain>:/var/repo git push linode

在您的本地存储库上git remote add linode root@<linode ip|domain>:/var/repo git push linode

your code is now deployed

您的代码现已部署

回答by Chetabahana

You may refer to this tutorial:
Automatically Updating Your Website Using GitHub's Service Hooks:

你可以参考这个教程:
使用 GitHub 的服务钩子自动更新你的网站

In short it explains the following steps:

简而言之,它解释了以下步骤:

Create a php filein .gitfolder on your server with the following contents.

创建一个PHP文件.git包含以下内容您的服务器上的文件夹。

<?php `git pull`;?>

Setup your server for the SSH keysto exist. Something like:

为 S SH 密钥设置您的服务器。就像是:

key. cat ~/.ssh/id_rsa.pub

Setup the service hook on GitHub . Enter WebHook URL:

在 GitHub 上设置服务挂钩。输入WebHook 网址

http://your.domain.com/path/to/yourfile.php

When all is set. The file is going deploy all the work on your server each time you push to GitHub.

当一切都设置好了。每次推送到 GitHub 时,该文件都会在您的服务器上部署所有工作。

回答by VonC

Using any kind of webhook involves deploying a listener for that hook, and then triggering, from your listener server host, the action.

使用任何类型的 webhook 都涉及为该钩子部署侦听器,然后从侦听器服务器主机触发该操作。

You can take a shortcut now (oct. 2018) with GitHub Actions(Oct. 2018).

您现在可以使用GitHub Actions(2018 年 10 月)走捷径(2018 年10 月)。

GitHub Actions allows you to connect and share containers to run your software development workflow. Easily build, package, release, update, and deploy your project in any language—on GitHub or any external system—without having to run code yourself.

GitHub Actions 允许您连接和共享容器以运行您的软件开发工作流程。使用任何语言(在 GitHub 或任何外部系统上)轻松构建、打包、发布、更新和部署您的项目,而无需自己运行代码。

See Actions: pushing is only one of the possibilities behind Actions!

参见Actions:推送只是 Actions 背后的可能性之一!

Workflows can be triggered by GitHub platform events (i.e. push, issue, release) and can run a sequence of serial or parallel actions in response. Combine and configure actions for the services you know and love built and maintained by the community.

工作流可以由 GitHub 平台事件(即推送、发布、发布)触发,并且可以运行一系列串行或并行操作作为响应。为您熟悉和喜爱的由社区构建和维护的服务组合和配置操作。