未加星标

Continuous Deployment with git, travis CI, heroku, node.js

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二03 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

A friend recently asked me about details for my Continuous Deployment setup for small projects. I promised to write him some notes about it - here they are.

Define the goal

I have tried to set up a process which allows me to prototype quickly, bring up a new project online with small effort, but still have a sound framework of integration tests, separation of development from staging (or production).

This is meant for small projects which are typically iterated fast by a small team (or just one developer), but coming from a big team development I would not feel well without some suporting structure like source control.

The minimum I want to do is:

develop locally, run and test my software locally on my laptop have a source control system which allows to track changes and branch out for trial versions. have some testing framework to avoid completely broken versions to go live have a deploy to a public url for staging and/or production, with a single line command to deploy Example: Limitless Garden

My example project is Limitless garden - here you can see most of this in action.

Start with github

The starting point is creating a github repository and clone it to your working directory.

$ git clone [email protected]:<your-github-name>/<your-repo-name>.git

In my case the platform is node.js with Express, so I initialize the project with

$ cd <my-repo-name> $ express -e . $ npm install

The -e switch in the express is for the ejs template engine, my personal favourite.

Now you can develop locally and run your local instance with

$ npm start

This will expose your website on http://localhost:3000

Any changes of course are commited as usual with

git add . git commit git push

or (if you use Visual Studio Code like me) with the corresponding UI functions for git interaction.

Deploy to heroku

to deploy this from git to heroku you can just set up a webhook from your github repo to your heroku instance. To do this you should have a heroku account and then open your heroku dashboard and create a new app. It will ask you to choose a name or let heroku generate one for you.


Continuous Deployment with git, travis CI, heroku, node.js

Heroku has quite sophisticated setup for deployment pipelines, and there is a free version, but it is limited to run a few hours a day. Good enough for staging, but if you want something always on they will ask you to pay. Lets go for free first.

After you selected your instance name, heroku will ask you to specify some options. To deploy from your github repo you check teh options below:


Continuous Deployment with git, travis CI, heroku, node.js

Now you should have heroku site which is always updated from your github repo.

You make a change in your code, commit and push the change to the github server, and a few moments later you see this new application in your heroku dashboard and can check the details:


Continuous Deployment with git, travis CI, heroku, node.js

You can click on the “open app” link and see your application running.

Testing and Continous Integration

You can add tests to your solution ( a bit out of scope here, as there are so many frameworks) and run tests manually on your local system - necessary, but a bit tedious, just type

$ npm test

before you decide to comit any changes.

Automation is possible in two ways:

Option 1: git hook

put your test script in a git commit hook, for our node.js application this can be done with the node package “pre-commit”

$ npm install pre-commit --save-dev

and then defining the “test” script as a pre-commit hook in your package.json:

{ "name": "limitless-garden", "version": "0.0.0", "private": true, "scripts": { "start": "node ./bin/www", "jshint": "jshint **.js", "test": "./node_modules/.bin/mocha -u tdd -R spec", }, "dependencies": { [...] }, "pre-commit": [ "jshint", "test" ], "devDependencies": { "expect.js": "^0.3.1", "jshint": "^2.8.0", "mocha": "^2.3.2", "pre-commit": "^1.1.2", [...] } } Option 2: travis.CI

The guys at travis-ci.org provide a fabulous Continous Integration service, and it is free if you can live with their limits (only public repositories, only one thread per test run,..) - for me this works fine.

You should sign in with your github account and Travis will utomatically pull the list of your public repositories and there are just two steps:

enable the repo with the button (image below)
Continuous Deployment with git, travis CI, heroku, node.js
add a “.travis.yml” file to your repo to tell Travis what to do. Mine looks like this : sudo: required dist: precise language: node_js node_js: - "4" services: - mongodb env: - CXX=g++-4.8 addons: apt: sources: - ubuntu-toolchain-r-test packages: - g++-4.8

Now that we have a CI process we can tell heroku to only deploy on successful CI test:


Continuous Deployment with git, travis CI, heroku, node.js
Conclusion

Voilá, that’s all. Almost. You will probably want to add some services like databases to this setup, and have different database instances for development, test, production?

Wait for the next post on this which should be coming soon.

Meanwhile, you could let me know what you think in the comments below, or enjoy some of my videos

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

分页:12
转载请注明
本文标题:Continuous Deployment with git, travis CI, heroku, node.js
本站链接:http://www.codesec.net/view/482740.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(36)