使用Codeship和ParallelCI加快部署工作流程

As developers, every second we save in our development process, adds up to make us that much more efficient. The faster we test our code and deploy, the faster our end users get a better product.

作为开发人员,我们在开发过程中节省的每一分每秒都使我们的工作效率大大提高。 我们测试代码和部署的速度越快,最终用户获得更好的产品的速度就越快。

The concept of continuous delivery helps us keep an optimized and quick build process. Here's a top down view of what continuous delivery consists of:

持续交付的概念有助于我们保持优化和快速的构建过程。 这是连续交付包括的自上而下的视图:

codeship-overview
  1. Push code to your Git repo

    将代码推送到您的Git存储库
  2. Run automated tests

    运行自动化测试
  3. Code is deployed to a hosting environment

    代码已部署到托管环境

All of the above happens automatically after you push to your repository. Having a continuous delivery system makes all the major tasks that you have to do simple and easy. A fully automated system can help save an incredible amount of time. This is where Codeship comes in.

推送到存储库后,上述所有操作都会自动发生。 拥有连续交付系统使您要做的所有主要任务变得简单而轻松。 全自动系统可以帮助节省大量时间。 这就是Codeship的来历

什么是Codeship? (What is Codeship?)

Codeship will do all of the above for you and provides a powerful service with an extremely easy to use dashboard. Their free plan provides:

Codeship将为您完成上述所有工作,并通过极其易于使用的仪表板提供强大的服务。 他们的免费计划提供:

  • 1 concurrent build

    1个并发构建
  • 1 testing pipeline

    1条测试管道
  • 100 private builds/month

    每月100个私人构建
  • 5 private projects

    5个私人项目
  • Unlimited team members

    无限的团队成员
  • Unlimited for open source

    无限开源

To setup a project on Codeship that will automatically run tests and deploy to your chosen host, we will have three steps to complete:

要在Codeship上设置一个项目,该项目将自动运行测试并部署到您选择的主机 ,我们将需要完成三个步骤:

  1. Setup Git repository

    设置Git存储库
  2. Create tests

    创建测试
  3. Create deployment

    创建部署

Let's look at how we can deploy an actual project in practice using GitHub, Codeship, and Heroku.

让我们看看如何使用GitHubCodeshipHeroku在实践中部署实际项目。

测试和部署节点应用程序 (Testing and Deploying a Node Appplication)

For this example, we will be using the meanjs.org MEAN stack starter kit. This project gives us a great foundation for a simple application that we can test and deploy.

对于此示例,我们将使用meanjs.org MEAN堆栈入门工具包。 该项目为我们可以测试和部署的简单应用程序奠定了良好的基础。

The starter kit provides a Node application that comes with tests that are run using the task runner Grunt to run Mocha tests for the Node back-end code and Karma tests for the Angular front-end code.

该入门工具包提供了一个Node应用程序,该应用程序附带使用任务运行程序Grunt运行的测试,以针对节点后端代码运行Mocha测试,为Angular前端代码运行Karma测试。

meanjs

We'll be deploying the MEAN stack application to Heroku and implementing continuous delivery through Codeship.

我们将把MEAN堆栈应用程序部署到Heroku并通过Codeship实现持续交付。

If you'd like to follow along, install Node, create a free Codeship account and a Heroku account, and get ready to get your deployment on!

如果您想继续,请安装Node ,创建一个免费的Codeship帐户和一个Heroku帐户,并准备好进行部署!

Related Reading: For information on deploying Node applications to Heroku (without the automated and continous delivery), read: How to Deploy a Node.js App to Heroku.

相关阅读 :有关将Node应用程序部署到Heroku(不进行自动连续交付)的信息,请阅读: 如何将Node.js应用程序部署到Heroku

设置项目 (Setting Up the Project)

Go ahead and fork the meanjs.org GitHub repo so that we can use our newly created Git repo as the basis for our continuous delivery experiment.

继续并分发meanjs.org GitHub存储库,以便我们可以使用新创建的Git存储库作为我们连续交付实验的基础。

meanjs-fork

Once we have that repository forked over to our GitHub account, go ahead and clone your project locally if you'd like to test. In my case, I use the following git command:

将存储库转移到我们的GitHub帐户后,如果要测试,请继续在本地克隆您的项目。 就我而言,我使用以下git命令:

克隆新的分叉仓库 (Clone the Newly Forked Repo)
$ git clone git@github.com:sevilayha/mean
启动MongoDB (Start MongoDB)

Make sure you have a local instance of MongoDB installed and running using:

确保使用以下命令安装并运行了MongoDB的本地实例:

$ mongod
安装npm依赖项 (Install npm Dependencies)

Install the dependencies for the project using the npm command:

使用npm命令安装项目的依赖项:

$ npm install
本地运行 (Run Locally)

Now that we have setup our project, let's run it by starting up the project with Node:

现在我们已经设置了项目,让我们通过使用Node启动项目来运行它:

$ node server.js

We'll be able to see our project in browser at: http://localhost:3000.

我们将能够在浏览器中查看我们的项目: http:// localhost:3000

meanjs-start

Great! We got a MEAN stack app up pretty quickly. Now let's see how tests are implemented because we'll want Codeship to run our tests every time we push to our repository.

大! 我们很快就启动了MEAN堆栈应用程序。 现在让我们看看如何实现测试,因为我们希望Codeship每次推送到存储库时都运行测试。

本地测试 (Test Locally)

If we look into the Gruntfile.js, we can see that there are three tasks built for testing on lines 181-183:

如果查看Gruntfile.js ,我们可以看到在第Gruntfile.js行中构建了三个测试任务:

grunt.registerTask('test', ['test:server', 'test:client']);
grunt.registerTask('test:server', ['env:test', 'mochaTest']);
grunt.registerTask('test:client', ['env:test', 'karma:unit']);

The grunt test task will run both the server testing task and the client testing task. Just to be sure that everything works perfectly, let's test out the well... tests. Go ahead and type:

grunt test任务将同时运行服务器测试任务和客户端测试任务。 为了确保一切正常,让我们测试一下...测试。 继续输入:

$ grunt test

Important Note: We can run the back-end and front-end tests with separate commands using grunt test:server and grunt test:client.

重要说明 :我们可以使用grunt test:servergrunt test:client分别使用命令运行后端和前端测试。

We'll be able to see the tests run and hopefully we'll see everything passing! Now this would take up a lot of our time if we wanted to keep running tests on our code manually. What happens when we have multiple people on a team all pushing code to the same repository? We wouldn't want to have someone test all the new incoming code. That would not be the most fun job in that development team.

我们将能够看到测试正在运行,并希望我们能看到一切通过! 现在,如果我们想继续在代码上手动运行测试,这将占用我们很多时间。 当我们的团队中有多个人都将代码推送到同一个存储库中时,会发生什么情况? 我们不希望有人测试所有新的传入代码。 那将不是该开发团队中最有趣的工作。

We now have a Node and Angular stack application with working tests. Let's get this integrated into Codeship so that our tests are run automatically on every push. After we've got that configured, we'll set up deployment to Heroku automatically!

现在,我们有了带有工作测试的Node and Angular堆栈应用程序。 让我们将其集成到Codeship中,以便我们的测试在每次推送时自动运行。 配置完成后,我们将自动将部署设置为Heroku!

将代码库连接到我们的GitHub存储库 (Hooking Up Codeship to Our GitHub Repo)

This is probably going to be the simplest of all the steps. Just login to Codeship, link your GitHub account, and select your repo. We are going to start our first project.

这可能是所有步骤中最简单的。 只需登录Codeship,链接您的GitHub帐户,然后选择您的存储库即可。 我们将开始我们的第一个项目。

codeship-first

And link our GitHub repository.

并链接我们的GitHub存储库。

codeship-connect-github

We'll be given a list of our repositories that we can choose from:

我们将为您提供可供选择的存储库列表:

codeship-select-repo

Once your repository is selected, the next step is to setup your tests.

选择存储库后,下一步就是设置测试。

设置测试 (Setup Tests)

Test Driven Development and Behavior Driven Development have become very popular recently and rightly so. Implementing testing into our projects helps keep quality high and our development process efficient.

测试驱动开发和行为驱动开发最近并且很受欢迎。 在我们的项目中实施测试有助于保持高质量和我们的开发流程高效。

Codeship provides a simple interface (two textareas) to input two things:

Codeship提供了一个简单的界面(两个文本区域)来输入两件事:

  1. Setup commands

    设置命令
  2. Test commands

    测试命令
codeship-setup-tests

Just like we did locally, our setup commands will be:

就像我们在本地所做的一样,我们的设置命令将是:

npm install

And our test commands will be:

我们的测试命令将是:

grunt test

It's that simple to setup Codeship with our tests! Now whenever we push to our repository, Codeship will grab the new code, create a new virtual machine, install Node dependencies, and run our tests. We can also setup notifications for build errors or successes.

使用我们的测试来设置Codeship就这么简单! 现在,每当我们推送到存储库时,Codeship就会获取新代码,创建新的虚拟机,安装Node依赖项并运行测试。 我们还可以设置有关构建错误或成功的通知。

If we change something in our repo, git commit and push, we can see that Codeship will recognize the change and trigger a build.

如果我们在仓库,git commit和push中更改了某些内容,则可以看到Codeship将识别出更改并触发构建。

codeship-look

If we click into that build, we can see exactly what commands are being run and how long they take. This build ran successfully and took 1 min 30 sec.

如果单击该内部版本,则可以准确看到正在运行的命令以及它们花费的时间。 该构建成功运行,耗时1分30秒

codeship-success

This entire system is now automated and our tests will run when our code is pushed to the repository. Much simpler than running tests and checking the repo manually.

现在,整个系统是自动化的,并且当我们将代码推送到存储库时,我们的测试将运行。 比运行测试和手动检查存储库要简单得多。

Codeship will also conveniently provides a multitude of notification options for build errors or if you want to be notified of certain events.

Codeship还可以方便地为构建错误或如果您希望收到某些事件通知提供多种通知选项。

通知事项 (Notifications)

Codeship comes with a lot of integrations for build notifications. You can integrate with email, GitHub, Hipchat, Campfire, Grove, Flowdock, and Slack. There is also a free Chrome extension for Codeship build notifications called Shipscope.

Codeship附带了许多用于生成通知的集成。 您可以与电子邮件,GitHub,Hipchat,Campfire,Grove,Flowdock和Slack集成。 还有一个免费的Chrome扩展程序,用于Codeship构建通知,称为Shipscope

By default, you will receive email notifications if a build has failed:

默认情况下,如果构建失败,您将收到电子邮件通知:

codeship-build-failed

建立徽章 (Build Badges)

There are also badges that can be placed on your site or GitHub repo so that users can see that all builds are green!

还可以将徽章放置在您的网站或GitHub存储库上,以便用户可以看到所有内部版本都是绿色的!

codeship-badge

Now that we've gotten our setup and first round of tests ready, let's look at a neat new feature that will speed up our workflow even more. This new feature that Codeship recently released makes builds even faster than they already are.

现在我们已经准备好设置并进行了第一轮测试,让我们看一下一个新功能,它可以进一步加快工作流程。 Codeship最近发布的这项新功能使构建速度比现有速度更快

ParalellCI (ParalellCI)

While this build only took 1 min 30 seconds, when this application gets larger and has more tests, this time could go up much further. When you have multiple developers pushing and deploying their code, the more automated and faster this process is, the better. Etsy is a prime example of a company that deploys a whopping 30+ times per day.

虽然此构建仅用了1分钟30秒 ,但当此应用程序变大并进行更多测试时,这次可能会增加很多。 当有多个开发人员推送和部署他们的代码时,此过程越自动化,越快,效果越好。 Etsy是一家公司每天部署30次以上的典型例子。

Codeship's new feature, ParallelCI, allows you to create multiple testing pipelines to greatly increase the speed of your builds. Earlier, we noted that the MEAN stack starter kit had two sets of tests (back-end and front-end).

Codeship的新功能ParallelCI允许您创建多个测试管道,以大大提高构建速度。 之前,我们注意到MEAN堆栈入门工具包进行了两组测试(后端和前端)。

Let's split those two test tasks into parallel testing pipelines and see how that improves our build times. When we create testing pipelines, they will run parallel to each other, which means that one doesn't have to finish before the other starts; they both run at the same time and don't interfere with each other.

让我们将这两个测试任务分成并行的测试管道,看看如何缩短构建时间。 当我们创建测试管道时,它们将彼此并行运行,这意味着一个管道不必在另一个管道开始之前完成; 它们都在同一时间运行,并且不会互相干扰。

How is this done? Codeship will create separate VMs for each testing pipeline. Each VM will run your setup commands and then do its set of testing.

怎么做? Codeship将为每个测试管道创建单独的VM 。 每个VM都会运行您的设置命令,然后进行一组测试。

codeship-parallel-ci

Let's setup another testing pipeline and name this Test Frontend.

让我们建立另一个测试管道,并将其命名为Test Frontend

codeship-create-pipeline

Now we'll change the testing commands for each pipeline to:

现在,我们将每个管道的测试命令更改为:

# first testing pipeline
grunt test:server

# second testing pipeline
grunt test:client

ParallelCI构建速度提高 (ParallelCI Build Speed Increase)

Now when we push to our repository and trigger a build, we can see the next build happening with ParallelCI.

现在,当我们推送到存储库并触发构建时,我们可以看到ParallelCI正在发生下一个构建。

codeship-next-build

We can see both testing pipelines are running:

我们可以看到两个测试管道都在运行:

codeship-parallel-builds

You'll notice your tests run quicker than the original build. This is because of the ParallelCI testing pipelines. We can see the comparison of the two builds.

您会注意到您的测试比原始版本运行得更快。 这是因为有ParallelCI测试管道。 我们可以看到两个版本的比较。

codeship-build-comparison

Original Build: 1 min 30 sec ParallelCI Build: 27 sec

原始版本 :1分30秒ParallelCI版本 :27秒

1/3rd of what it was. 1/3

Even for a simple application like this with some quick tests, the speed improvements are apparent.

即使对于这样的简单应用程序并进行了一些快速测试,速度的提高也是显而易见的。

Now that we've got testing configured and optimized, let's look at deploying our application to Heroku.

现在我们已经配置和优化了测试,让我们看看将应用程序部署到Heroku。

部署管道 (Deployment Pipelines)

Codeship lets us configure deployment pipelines to have our applications pushed out to hosting environments as soon as the tests are run successfully.

使用Codeship,我们可以配置部署管道,以在测试成功运行后将我们的应用程序推送到托管环境。

codeship-deployment

We can create deployment pipelines as complex or as simple as we want. If you want to include a staging server and QA server in the mix, then by all means. For our purposes, we'll create a very simple deploy to Heroku pipeline.

我们可以根据需要创建复杂或简单的部署管道。 如果要在混合中包括登台服务器和QA服务器,则一定要使用。 为了我们的目的,我们将创建一个非常简单的部署到Heroku管道。

Codeship provides many options for supported hosts including:

Codeship为受支持的主机提供了许多选项,包括:

  • Google App Engine

    Google App引擎
  • Capistrano

    Capistrano
  • Cloud Foundry

    云铸造
  • dotCloud

    点云
  • EngineYard

    发动机场
  • Heroku

    Heroku
  • Nodejitsu

    结柔
codeship-deployments

If the above hosts aren't where you deploy to, then you are able to configure custom script deployments very easily. In our case, we will set the master branch to go to Heroku.

如果上述主机不在您要部署的位置,则可以非常轻松地配置自定义脚本部署。 在本例中,我们将分支设置为进入Heroku。

codeship-heroku-deploy

All we need are our Heroku app name and API key. You will need an already created Heroku application ready for this integration. Create one from the Heroku dashboard by clicking the plus sign.

我们需要的只是Heroku应用名称和API密钥。 您将需要一个已经创建的Heroku应用程序,可以进行此集成 。 通过单击加号从Heroku仪表板中创建一个。

Related Reading: Learn how to create an app on Heroku. Deploy a Node.js App to Heroku.

相关阅读 :了解如何在Heroku上创建应用。 将Node.js应用程序部署到Heroku

准备好Heroku (Getting Heroku Ready)

There are a few steps we'll need to get our app ready for Heroku. We have been using a local instance of MongoDB, but we don't have that luxury on Heroku. We will need to add the free MongoLab addon.

我们需要一些步骤来为Heroku准备好我们的应用程序。 我们一直在使用MongoDB的本地实例,但在Heroku上却没有这么奢侈。 我们将需要添加免费的MongoLab插件

From the command line, make sure you have the Heroku toolbelt, and type:

在命令行中,确保您具有Heroku工具带 ,然后键入:

$ heroku addons:add mongolab

Once that is installed, you'll be able to see the MongoLab addon in your Heroku dashboard.

安装完成后,您将能够在Heroku仪表板中看到MongoLab插件。

heroku-mongolab-addon

We will now need to grab the MongoDB URI so we can connect to our database. To find out our MongoDB URI, type the following into your command line:

现在,我们需要获取MongoDB URI,以便我们可以连接到数据库。 要查找我们的MongoDB URI,请在命令行中输入以下内容:

$ heroku config | grep MONGOLAB_URI

That will spit out your URI including username and password. We'll have to go into our MEAN app and change the database URI for production since that's the environment that will connect to this MongoLab instance.

这将吐出您的URI,包括用户名和密码。 我们必须进入MEAN应用并更改数据库URI以进行生产,因为这是连接到此MongoLab实例的环境。

In config/env/production.js, add this URI to the db.uri parameter.

config/env/production.js ,将此URI添加到db.uri参数。

Commit and push this new change and Codeship will handle the rest!

提交并推动这一新更改,Codeship将处理其余的一切!

We can see the build, the tests, and the deployment all happen.

我们可以看到构建,测试和部署都已完成。

codeship-heroku-deploy

Then we can see our application in browser!

然后我们可以在浏览器中看到我们的应用程序!

https://codeship-deploy.herokuapp.com

https://codeship-deploy.herokuapp.com

结论 (Conclusion)

Hopefully this gives you a good overview of how Codeship and continuous delivery can help increase the speed and efficiency of our code.

希望这可以使您很好地了解Codeship和持续交付如何帮助提高我们的代码的速度和效率。

Be sure to check out Codeship and the great features that they offer. Go forth and implement your own continuous deployment/integration/development system!

请务必查看Codeship及其提供的强大功能。 继续并实施您自己的连续部署/集成/开发系统!

DISCLAIMER: This post is sponsored by Codeship through Syndicate Ads.

免责声明:这篇文章是由Codeship通过Syndicate Ads赞助的。

翻译自: https://scotch.io/tutorials/speed-up-your-deployment-workflow-with-codeship-and-parallelci

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值