如何在 git 推送(GitHub 和 node.js)后自动部署我的应用程序?

发布于 2025-01-02 14:39:48 字数 83 浏览 2 评论 0原文

我的应用程序 (node.js) 部署在 VPS (linux) 上。我使用 git hub 作为存储库。如何在 git push 上自动部署应用程序?

I have my application (node.js) deployed on a VPS (linux). I'm using git hub as a repository. How can I deploy the application automatically, on git push ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

哆啦不做梦 2025-01-09 14:39:48

PHP 中的示例:

导航到 github,进入您的 github 存储库,添加单击“管理”,然后

单击选项卡“服务挂钩”=> 'WebHook URLs'

并添加

http://your-domain-name/git_test.php

然后创建 git_test.php

<?php 
try
{
  $payload = json_decode($_REQUEST['payload']);
}
catch(Exception $e)
{
  exit(0);
}

//log the request
file_put_contents('logs/github.txt', print_r($payload, TRUE), FILE_APPEND);


if ($payload->ref === 'refs/heads/master')
{
  // path to your site deployment script
  exec('./build.sh');
}

在 build.sh 中,您需要输入常用命令从 github 检索您的网站

Example in PHP:

Navigate to github into your github repository add click "Admin"

click tab 'Service Hooks' => 'WebHook URLs'

and add

http://your-domain-name/git_test.php

then create git_test.php

<?php 
try
{
  $payload = json_decode($_REQUEST['payload']);
}
catch(Exception $e)
{
  exit(0);
}

//log the request
file_put_contents('logs/github.txt', print_r($payload, TRUE), FILE_APPEND);


if ($payload->ref === 'refs/heads/master')
{
  // path to your site deployment script
  exec('./build.sh');
}

In the build.sh you will need to put usual commands to retrieve your site from github

稚然 2025-01-09 14:39:48

有几次提到 Git 挂钩作为答案/评论,这在过去对我有用。所以如果其他人需要更多细节,这里是我的食谱。

我使用 git post-receive hooknode-supervisor 来完成简单的自动部署(假设您在那台机器上使用 git 远程存储库)。


设置您的 Post-Receive Hook

在您的存储库中:sudo vi hooks/post-receive

它应该类似于:

#!/bin/sh
GIT_WORK_TREE=/home/path/to/your/www
export GIT_WORK_TREE
git checkout -f

设置文件权限:chmod +x hooks/ post-receive

Git 将在推送到存储库后刷新应用程序目录中的文件。


使用 Node-Supervisor 运行 Node

您需要在计算机上安装 Node-Supervisor 作为全局节点模块: sudo npm installsupervisor -g

现在只需运行您的节点应用程序使用node-supervisor,它将监视工作目录中文件的更改:

supervisor /home/path/to/your/www/server.js(注意supervisor而不是节点)。

There were a few mentions of Git hooks as answers/comments, which has worked for me in the past.. so here's my recipe should someone else require more specifics.

I use a combination of the git post-receive hook and node-supervisor to accomplish simple auto deployment (assuming you're using a git remote repository on that machine).


Setup Your Post-Receive Hook

In your repository: sudo vi hooks/post-receive

And it should look something like:

#!/bin/sh
GIT_WORK_TREE=/home/path/to/your/www
export GIT_WORK_TREE
git checkout -f

Set file permissions: chmod +x hooks/post-receive

Git will refresh the files in your app directory following a push to the repo.


Run Node with Node-Supervisor

You'll need to install Node-Supervisor on your machine as a global node module: sudo npm install supervisor -g

Now simply run your node app with node-supervisor and it'll watch for changes to files in your working directory:

supervisor /home/path/to/your/www/server.js (note supervisor instead of node).

浅浅淡淡 2025-01-09 14:39:48

在这里回复可能已经很晚了。但我在 github 上找到了这个项目,似乎可以做你想做的事情,但是以一种更干净的方式。

https://github.com/logsol/Github-Auto-Deploy

查看一下。我也有兴趣了解其他人在评论和点赞方面对此的看法。

干杯,
S

Probably very late to repond here. But I found this project on github and seems to do what you want to do but in a much cleaner way.

https://github.com/logsol/Github-Auto-Deploy

Check it out. Would be also interested to know what others think of this in terms of comments and upvotes.

Cheers,
S

°如果伤别离去 2025-01-09 14:39:48

在我目前正在开发的一个项目中,我遵循 Jez Humble 的精彩著作“持续交付”(非常值得一读)中涵盖的指南。

这意味着使用某种形式的持续集成服务器创建部署管道(我使用 Thoughtworks 免费社区版 Go),负责首先检查代码的质量、复杂性并运行单元测试。然后它可以遵循部署管道,从而推送到您的生产服务器。

这听起来非常复杂,但并非必须如此,并且确实使编写代码的整个过程及其进入生产的方式变得安全且无忧(没有可怕的发布日!)。

我对实时系统使用完整的部署管道,对我编写的 npm 模块使用精简版本,并且两者共享相同的一键部署技术。

In a project I am currently developing I follow the guidelines covered in Jez Humble's brilliant book "Continuous Delivery" (well worth a read).

This means creating a deployment pipeline using some form of continuous integration server (I use Thoughtworks free community edition of Go), that is responsible for first checking your code for quality, complexity and running unit tests. It can then follow a deployment pipeline resulting in a push to your production servers.

This sounds very complicated, but doesn't have to be, and does make the whole process of writing code and it making it's way into production safe and worry free (no scary release days!).

I use a full deployment pipeline for live systems, and a cut down version for npm modules that I write, and both share the same 1-click deployment technique.

香草可樂 2025-01-09 14:39:48

我刚刚针对您的问题发布了一个基于节点的解决方案: node-cd

它包含在您的 VPS 上运行的简单节点应用程序将接收 Github post-receive Hooks 并执行您喜欢的脚本(例如 shell 脚本)这将杀死你的应用程序,git pull,然后重新启动 它)。

I just published a node-based solution to your problem: node-cd

It consists in a simple node app running on your VPS that will receive Github post-receive Hooks and execute a the script you like (for example a shell script that will kill your app, git pull, and restart it).

乖不如嘢 2025-01-09 14:39:48

这是另一个简单的 NodeJS 实现。

它是一个非常简单的节点服务器,在您配置的主机名和端口上运行,并且可以设置为处理 GitHub 发布接收 Web 挂钩。并且可以自定义实际的拉/测试/部署操作来执行您想要的任何操作。在当前的实现中,它是在 NodeJS 服务器脚本中内联指定的 shell 命令。并且还有一个非常简单的基于 Secret_key 的安全方案。

https://github.com/shyam-habarakada/rscds

我的临时服务器已经安装了节点并且正在运行,所以写这篇文章又快又容易。

Here's another simple nodeJS implementation.

It's a very simple node server that runs on a hostname and port you configure and can be setup to handle GitHub post receive web hooks. And the actual pul/test/deploy actions can be customised to do anything you want. In the current implementation, it is a shell command that is specified inline in the nodeJS server script. And there is a very simple secret_key based security scheme in place as well.

https://github.com/shyam-habarakada/rscds

My staging server already had node installed and running, so writing this up was quick and easy.

想挽留 2025-01-09 14:39:48

我发现该项目使用git方便部署。

git-play

我认为这对你来说是正确的方法。

一探究竟。

I found the project for easy deployment uses git.

git-play

I think it's proper way for you.

Check it out.

一萌ing 2025-01-09 14:39:48

如果您想要基于 python/tornado 的解决方案,我编写了一个脚本来处理来自 Github 的 Webhook 服务。您可以在 https://github.com/Akobi/ops/tree/master/ 找到它autodeploy

它基本上使用 JSON 配置文件来列出您期望从哪些存储库推送、您想要在部署时运行哪些命令以及命令必须在哪个目录中运行。您所要做的就是修改配置文件根据你的喜好并运行脚本!

此外,我使用 Nginx 作为反向代理将这些 POST 转发到我的脚本。您可以在同一 Github 存储库的“nginx”文件夹下找到 Nginx 配置。

快乐推!

If you want a python/tornado-based solution, I wrote a script to handle POST requests from Github's Webhook Services. You can find it at https://github.com/Akobi/ops/tree/master/autodeploy

It basically uses a JSON config file to list which repos you expect pushes from, which commands you want to run on deploy, and what directory the commands must run in. All you would have to do is modify the config file to your liking and run the script!

In addition, I use Nginx as a reverse proxy to forward these POSTs to my script. You can find the Nginx config in the same Github repo under the 'nginx' folder.

Happy pushing!

天生の放荡 2025-01-09 14:39:48

在我看来,PHP 的答案是完全合法的,但如果您更喜欢 Ruby,我在博客中发布了一个解决方案。它与 PHP 答案是一样的,只是语言不同。您使用 Web 挂钩,并且有一个简单的脚本来监听相关的 HTTP 请求。

http://gilesbowkett.blogspot.com/2012/06 /heroku-style-deployment-on-ec2.html

the PHP answer is totally legit in my opinion, but if you prefer Ruby, I blogged a solution. it's the same thing as the PHP answer, just in a different language. you use a web hook and you have a simple script listen for the relevant HTTP requests.

http://gilesbowkett.blogspot.com/2012/06/heroku-style-deployment-on-ec2.html

自此以后,行同陌路 2025-01-09 14:39:48

我创建了自己的基本部署工具,它会自动从存储库中下载新更新 - https://github.com /jesalg/SlimJim - 基本上它监听 g​​ithub post-receive-hook 并使用代理来触发更新脚本。

I've created my own rudimentary deployment tool 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.

送舟行 2025-01-09 14:39:48

我是 https://commando.io 的创始人,最近我们宣布通过一项服务与 GitHub 集成。该集成允许您在推送到 GitHub 存储库时在服务器上运行执行。这是在推送代码时自动运行部署脚本的绝佳机会。

执行是您在 Commando.io 内部编写的脚本,可以使用 bash、perl、python、ruby、go 或 node.js 编写。要了解更多信息并查看运行 git pull 的示例执行脚本,请参阅我们的博客文章公告:http://blog.commando.io/run-executions-via-github-push/

I'm the founder of https://commando.io and recently we announced an integration with GitHub via a service. The integration allows you to run executions on servers when you push to a GitHub repo. This is a perfect opportunity to automatically run deployment scripts when you push code.

An execution is a script you write inside of Commando.io that can be written in bash, perl, python, ruby, go, or node.js. To read more, and see an example execution script of running git pull, see our blog post announcement: http://blog.commando.io/run-executions-via-github-push/

不必在意 2025-01-09 14:39:48

Deepl.io 似乎是这个领域新的、有前途的竞争者。

功能(取自其网站):

  • 从 GitLab 捕获 webhooks GitHub
  • 配置多个存储库
  • 每个存储库配置多个分支
  • 使用您自己的部署脚本,PHP、shell 或两者兼而有之
  • 发送确认电子邮件

Deepl.io seems to be new and promising contender in this space.

Features (taken from its website):

  • Catch webhooks from GitLab & GitHub
  • Configure multiple repositories
  • Configure multiple branches per repository
  • Use your own deploy scripts, either PHP, shell or both
  • Sends confirmation emails
谁与争疯 2025-01-09 14:39:48

另请注意,有一些免费/廉价的服务,例如 REPOMAN.IO ,可以为您自动执行几乎所有这些操作。

Also note there are free/inexpensive services out there like REPOMAN.IO that automate almost all of this for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文