简体   繁体   English

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

[英]How can I automatically deploy my app after a git push ( GitHub and node.js)?

I have my application (node.js) deployed on a VPS (linux). 我将我的应用程序(node.js)部署在VPS(linux)上。 I'm using git hub as a repository. 我正在使用git hub作为存储库。 How can I deploy the application automatically, on git push ? 如何在git push上自动部署应用程序?

Example in PHP: PHP中的示例:

Navigate to github into your github repository add click "Admin" 导航到github进入你的github存储库添加点击“管理员”

click tab 'Service Hooks' => 'WebHook URLs' 点击标签'Service Hooks'=>'WebHook网址'

and add 并添加

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

then create 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');
}

In the build.sh you will need to put usual commands to retrieve your site from github 在build.sh中,您需要放置常用命令以从github检索您的站点

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. 有几个提到Git钩子作为答案/评论,过去对我有用..所以这是我的配方,如果其他人需要更多的细节。

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). 我使用git post-receive hooknode-supervisor的组合来完成简单的自动部署(假设你在该机器上使用git远程存储库)。


Setup Your Post-Receive Hook 设置您的接收后挂钩

In your repository: sudo vi hooks/post-receive 在您的存储库中: 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 设置文件权限: chmod +x hooks/post-receive

Git will refresh the files in your app directory following a push to the repo. 在推送到repo后,Git将刷新app目录中的文件。


Run Node with Node-Supervisor 使用Node-Supervisor运行节点

You'll need to install Node-Supervisor on your machine as a global node module: sudo npm install supervisor -g 您需要在您的计算机上安装Node-Supervisor作为全局节点模块: 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: 现在只需使用node-supervisor运行您的节点应用程序,它将监视工作目录中文件的更改:

supervisor /home/path/to/your/www/server.js (note supervisor instead of node ). supervisor /home/path/to/your/www/server.js (注意supervisor而不是node )。

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. 但是我在github上找到了这个项目,似乎做了你想做的事,但是以更清洁的方式。

https://github.com/logsol/Github-Auto-Deploy 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 小号

I just published a node-based solution to your problem: node-cd 我刚刚发布了一个基于节点的解决方案来解决你的问题: 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). 它包含一个在你的VPS上运行的简单节点应用程序,它将接收Github后接收挂钩并执行你喜欢的脚本(例如一个将杀死你的app,git pull并重新启动它的shell脚本)。

In a project I am currently developing I follow the guidelines covered in Jez Humble's brilliant book "Continuous Delivery" (well worth a read). 在我正在开发的项目中,我遵循Jez Humble的精彩书籍“持续交付”中所涵盖的指导原则(非常值得一读)。

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. 这意味着使用某种形式的持续集成服务器(我使用Go的 Thoughtworks免费社区版)创建部署管道,负责首先检查代码的质量,复杂性和运行单元测试。 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. 我使用实时系统的完整部署管道,以及我编写的npm模块的缩减版本,并且两者共享相同的一键式部署技术。

Here's another simple nodeJS implementation. 这是另一个简单的nodeJS实现。

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. 它是一个非常简单的节点服务器,可以在您配置的主机名和端口上运行,并且可以设置为处理GitHub后接收Web挂钩。 And the actual pul/test/deploy actions can be customised to do anything you want. 并且可以定制实际的pul / test / deploy操作以执行您想要的任何操作。 In the current implementation, it is a shell command that is specified inline in the nodeJS server script. 在当前实现中,它是在nodeJS服务器脚本中内联指定的shell命令。 And there is a very simple secret_key based security scheme in place as well. 并且还有一个非常简单的基于secret_key的安全方案。

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

My staging server already had node installed and running, so writing this up was quick and easy. 我的登台服务器已经安装并运行了节点,因此快速轻松地编写它。

I found the project for easy deployment uses git. 我发现易于部署的项目使用git。

git-play 混帐播放

I think it's proper way for you. 我认为这是适合你的方式。

Check it out. 看看这个。

If you want a python/tornado-based solution, I wrote a script to handle POST requests from Github's Webhook Services . 如果你想要一个基于python / tornado的解决方案,我写了一个脚本来处理来自Github的Webhook服务的 POST请求。 You can find it at https://github.com/Akobi/ops/tree/master/autodeploy 您可以在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! 它基本上使用JSON配置文件来列出您希望推送哪些存储库,要在部署时运行哪些命令,以及命令必须在哪个目录中运行。您所要做的就是根据自己的喜好修改配置文件并运行剧本!

In addition, I use Nginx as a reverse proxy to forward these POSTs to my script. 另外,我使用Nginx作为反向代理将这些POST转发到我的脚本。 You can find the Nginx config in the same Github repo under the 'nginx' folder. 您可以在'nginx'文件夹下的相同Github仓库中找到Nginx配置。

Happy pushing! 快乐推!

the PHP answer is totally legit in my opinion, but if you prefer Ruby, I blogged a solution. PHP的答案在我看来是完全合法的,但如果你更喜欢Ruby,我会在博客上写一个解决方案。 it's the same thing as the PHP answer, just in a different language. 这与PHP的答案是一样的,只是用不同的语言。 you use a web hook and you have a simple script listen for the relevant HTTP requests. 您使用Web挂钩,并且您有一个简单的脚本侦听相关的HTTP请求。

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

我已经创建了自己的基本部署工具,它会自动从repo中删除新的更新 - https://github.com/jesalg/SlimJim - 基本上它会监听github post-receive-hook并使用代理来触发更新脚本。

I'm the founder of https://commando.io and recently we announced an integration with GitHub via a service. 我是https://commando.io的创始人,最近我们宣布通过服务与GitHub集成。 The integration allows you to run executions on servers when you push to a GitHub repo. 集成允许您在推送到GitHub存储库时在服务器上运行执行。 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. 执行是您在Commando.io中编写的脚本,可以用bash,perl,python,ruby,go或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/ 要阅读更多内容,并查看运行git pull的示例执行脚本,请参阅我们的博客帖子公告: http//blog.commando.io/run-executions-via-github-push/

Deepl.io seems to be new and promising contender in this space. Deepl.io似乎是这个领域新的有前途的竞争者。

Features (taken from its website): 功能(摘自其网站):

  • Catch webhooks from GitLab & GitHub 从GitLab和GitHub抓住webhooks
  • Configure multiple repositories 配置多个存储库
  • Configure multiple branches per repository 每个存储库配置多个分支
  • Use your own deploy scripts, either PHP, shell or both 使用您自己的部署脚本,PHP,shell或两者
  • Sends confirmation emails 发送确认电子邮件

还要注意那里有免费/廉价的服务,比如REPOMAN.IO ,可以为你自动化几乎所有这些。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当应用程序在子文件夹中时,如何通过git将node.js应用程序部署到azure? - How can I deploy a node.js app to azure via git when the app is in a subfolder? 使用git将我的node.js应用程序部署到我的生产服务器 - using git to deploy my node.js app to my production server 如何将node.js Web应用程序上传到openshift? 它不是在git上,而是在我的计算机上? - How do I upload my node.js web app to openshift? It is NOT on git, but just on my computer? 如何更改我在github中进行1次推送的git消息? - How can I change the git message of my 1 push in github? Heroku Deploy - “推送拒绝,无法编译Node.js应用程序” - Heroku Deploy - “Push rejected, failed to compile Node.js app” 将我的node.js应用程序从Github部署到VPS - Deploying my node.js app from Github to a VPS 将所有文件推送到我的 github 后如何修复 git 提交的身份不明的用户 - How to fix git committed unidentified user after I push all my files to my github 如何使用git push推送所有github起源? - How can I push to all the github origins with a git push? 如何使用 Git/GitHub 将 discord.js bot 部署到远程服务器(我拥有并在我的网络上),而无需手动 FTP 到其中 - How can I use Git/GitHub to deploy a discord.js bot to a remote server (that I own and is on my network) without having to manually FTP into it 如何在推送时自动部署我的git repo的子模块? - How can I auto-deploy my git repo's submodules on push?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM