python circle_与Python和Circle CI的持续集成

python circle

In this tutorial, we'll review why Continuous Integration is indispensable to the development lifecycle of both solitary developers and teams and how we can immediately begin to reap its benefits.

在本教程中,我们将回顾为什么持续集成对于单独的开发人员和团队的开发生命周期必不可少,以及我们如何立即开始获得其好处。

Continuous Integration (CI) is a software development practice wherein developers regularly merge their code with a central repository after which automated builds and tests are run.

持续集成(CI)是一种软件开发实践,其中,开发人员定期将其代码与中央存储库合并,然后运行自动构建和测试。

We'll be using git for version control and Circle CI as our CI server. It'll provide us with a reliable configurable environment for us to run our automated builds and tests.

我们将使用git进行版本控制,并使用Circle CI作为CI服务器。 它将为我们提供一个可靠的可配置环境,使我们可以运行我们的自动化构建和测试。

持续整合原则 ( Principles of Continuous Integration )

Before we get our hands dirty with a demonstration, let's briefly discuss the principles of Continuous Integration.

在进行演示之前,让我们简要讨论持续集成的原理。

维护一个源代码存储库 (Maintain a single source code repository)

Continuous Integration (CI) advocates the use of a version control system to track changes in code.
Everything needed for a fully functional application should be pushed to a single repository.

持续集成(CI)提倡使用版本控制系统来跟踪代码中的更改。
功能齐全的应用程序所需的所有内容都应推送到单个存储库中。

构建自动化 (Build automation)

It should be trivial to trigger a complete rendering of the project with a single command. This should include tasks like the creation and migration of databases and configuring project environment variables.

用单个命令触发项目的完整渲染应该很简单。 这应包括创建和迁移数据库以及配置项目环境变量之类的任务。

进行构建自测 (Make the build self testing)

The CI tool used should trigger a run of the project tests after the build is complete. Of course, this necessitates that comprehensive tests be written for any work to be integrated.

构建完成后,使用的CI工具应触发运行项目测试。 当然,这需要为要集成的任何工作编写全面的测试。

Before the code that triggered the build is merged with the main branch, all tests must be seen to pass.

在触发构建的代码与main分支合并之前,必须确保所有测试均已通过。

频繁提交到主分支 (Make frequent commits to the main branch)

Making atomic and frequent commits encourages integration and makes conflicts between work from different developers easier to manage and resolve.

进行原子的和频繁的提交会鼓励集成,并使来自不同开发人员的工作之间的冲突更易于管理和解决。

After a section of work has been built and passes tests on the CI server, it should be merged to the main branch after review.

在构建了一部分工作并在CI服务器上通过测试之后,应在审查后将其合并到主分支。

每次提交都应触发CI服务器上的构建 (Every commit should trigger a build on the CI server)

This assures that each commit made has not broken the build. It also becomes trivial to pinpoint a commit that caused errors in the build.

这确保了每次提交都不会破坏构建。 查明导致构建错误的提交也很简单。

损坏的版本应立即修复 (Broken builds should be fixed immediately)

When it is discovered that a commit has caused a build on the CI server to fail, the commit should be analysed and the cause of the break resolved as soon as possible.

如果发现提交已导致CI服务器上的构建失败,则应分析该提交并尽快解决中断原因。

CI构建应在生产环境的副本中完成 (The CI build should be done in a replica of the production environment)

Running the build in an environment with little or no deviation from the production environment assures that running the same application in the actual production environment will not be fraught with unwelcome surprises.

在与生产环境几乎没有偏差的环境中运行构建可确保在实际生产环境中运行相同的应用程序不会出现意外的惊喜。

CI构建的输出应可见 (The output of CI builds should be visible)

Build status should be made available to all relevant stakeholders. Circle CI can be configured to send email and web notifications after a build has completed.

建造状态应提供给所有相关的利益相关者。 可以将Circle CI配置为在构建完成后发送电子邮件和Web通知。

持续整合的优势 ( Advantages of continuous integration )

The benefits of implementing Continuous Integration in your development cycle are several. Let's note down a few here.

在开发周期中实施持续集成的好处有很多。 让我们在这里记下一些。

  1. Since build output is extremely visible, when a build fails, we can find and resolve bugs quickly.

    由于构建输出非常明显,因此当构建失败时,我们可以快速找到并解决错误。
  2. Continuous Integration helps to enforce testing in our applications, since we rely on these tests to assess the success of our builds.

    持续集成有助于在我们的应用程序中执行测试,因为我们依靠这些测试来评估构建的成功。
  3. Because the build is run in a production like environment, CI reduces the time it takes to validate the quality of software to be deployed.

    由于构建在类似生产的环境中运行,因此CI减少了验证要部署的软件质量所需的时间。
  4. Since atomic commits are encouraged, CI allows us to reduce integration problems with existing code, enabling us to deliver better software faster.

    由于鼓励使用原子提交,因此CI使我们能够减少与现有代码的集成问题,从而使我们能够更快地交付更好的软件。

面向单独开发人员和团队的CI ( CI for solo developers and teams )

Now that I've bended your ear with praises for Continuous Integration, let's sober up a little.

现在,我赞扬您对持续集成的称赞,让我们冷静一下。

It's evident how the benefits we discussed above would be extremely valuable for teams. Now, let's focus on how CI can enrich development for solo developers and whether the benefits outweigh the costs.

显然,我们上面讨论的收益对于团队来说将是极为宝贵的。 现在,让我们集中讨论CI如何为单独的开发人员丰富开发,以及收益是否超过成本。

All the advantages we mentioned above still apply to solo developers but if you're solo, you should think about the following before implementing CI.

我们上面提到的所有优点仍然适用于单独开发人员,但是如果您是单独开发人员,则在实施CI之前应考虑以下事项。

  1. CI takes time: Depending on the complexity of your application's environment, it may require a lot of manpower to set up your CI environment in a way that mirrors your actual production environment. If your production environment changes, you'll need to reconfigure your CI environment too.

    CI需要时间:根据应用程序环境的复杂性,可能需要大量的人力来以与实际生产环境类似的方式设置CI环境。 如果您的生产环境发生变化,那么您还需要重新配置CI环境。
  2. Tests take time: Again, depending on the complexity of your application and the thoroughness of the tests running in the CI environment, your tests may take a while to run and confirm the health of the build. Since the build and tests should ideally run on every commit made, this could be an expensive operation. You may also need to spend valuable time optimising how your tests run. If you want to move fast and break things, this may be a little frustrating.

    测试需要时间:同样,取决于应用程序的复杂性和在CI环境中运行的测试的彻底性,测试可能需要一段时间才能运行并确认构建的运行状况。 由于理想情况下应在每次提交时都运行构建和测试,因此这可能是一项昂贵的操作。 您可能还需要花费宝贵的时间来优化测试的运行方式。 如果您想快速移动并弄碎东西,这可能会有些令人沮丧。
  3. CI is a way of life: Continous Integration begins and ends with the developer. It's a commitment not to be taken lightly. When you don't have a team that reminds you of the value of the process, it can be a lonely and tiring path to walk.

    CI是一种生活方式:持续集成始于开发人员,始于开发人员。 这是不可掉以轻心的承诺。 当您没有团队让您想起过程的价值时,这可能是一条孤独而累人的步行之路。
  4. CI can be a red herring: Developers implementing CI have to ensure that they are not lulled into a false sense of security by passing builds. This is even more important when you're working alone and without the benefit of other processes that could alert you to unseen problems. The build is only as good as the tests that run in it.

    CI可能会引起麻烦:实施CI的开发人员必须确保通过传递构建,不会陷入虚假的安全感。 当您独自工作并且没有其他可能使您警觉到未解决问题的流程的好处时,这一点尤为重要。 该版本仅与其中运行的测试一样好。

帐户设定 ( Account Setup )

Now that we've got a handle on what Continuous Integration is, let's see what it entails. We'll need accounts with the following services, so go ahead and register.

现在我们已经了解了什么是持续集成,让我们看看它的含义。 我们需要具有以下服务的帐户,因此请继续注册。

  1. Github: We'll use Github to host our git repository.

    Github :我们将使用Github来托管我们的git仓库。
  2. Circle CI: Registering on Circle CI with your Github account will make things easier in future. This has the advantage of adding CircleCI integration to your Github account.

    Circle CI :使用您的Github帐户在Circle CI上注册将使以后的工作变得更加轻松。 这具有将CircleCI集成添加到您的Github帐户的优势。
  3. Coveralls: Samsies here. Sign up with Coveralls using your Github account.

    工作服 :Samsies在这里。 使用您的Github帐户注册Coverovers。

全球依赖 ( Global Dependencies )

Before we start, you'll need to install a few applications globally.

在开始之前,您需要全局安装一些应用程序。

  1. git

    吉特
  2. Python: I'll be using v3.5.2

    Python :我将使用v3.5.2
  3. virtualenv: A recommended installation to isolate our application environment.

    virtualenv :建议安装,以隔离我们的应用程序环境。

项目结构 ( Project structure )

The following will be the scaffolding for our project, so go ahead and create these directories.

以下是我们项目的脚手架,因此继续创建这些目录。

+-- python-ci
    +-- src
    |   +-- math.py
    +-- test
    |   +-- math-test.py
    +-- requirements.txt

Next, copy and paste the following into the requirements.txt file at the root of the project.

接下来,将以下内容复制并粘贴到项目根目录下的requirements.txt文件中。

appdirs==1.4.0
astroid==1.4.9
click==6.7
coverage==4.3.4
coveralls==1.1
docopt==0.6.2
isort==4.2.5
lazy-object-proxy==1.2.2
mccabe==0.6.1
nose==1.3.7
packaging==16.8
pyparsing==2.1.10
requests==2.13.0
six==1.10.0
wrapt==1.10.8

Finally, create and activate the virtual environment for this project then run

最后,为该项目创建并激活虚拟环境,然后运行

pipinstall -r requirements.txt

This will install all the required dependencies for our project in your current virtual environment.

这将在您当前的虚拟环境中为我们的项目安装所有必需的依赖项。

Let's create a simple class with a function that returns the sum of two numbers. Add this into your src/math.py file.

让我们创建一个具有返回两个数字之和的函数的简单类。 将此添加到您的src / math.py文件中。

class Math():
    def addition(value1, value2):
        if not isinstance(value1, int) and not isinstance(value2, int):
            return 'Invalid input'
        else:
            return value1 + value2

Next, let's write a test to make sure our function is working as expected.

接下来,让我们编写一个测试以确保我们的功能按预期工作。

import unittest
from src.math import Math

class MathTest(unittest.TestCase):
    def test_addition(self):
        # Make test fail
        self.assertEqual(Math.addition(3, 4), 8)

You'll notice that we've made an incorrect assertion in the test. Let's let it slide for now.

您会注意到,我们在测试中做出了错误的断言。 让我们暂时滑动一下。

Now, run the following in your shell to make sure the test fails.

现在,在您的shell中运行以下命令以确保测试失败。

nosetests tests/math-test.py

Expect similar results

期待类似的结果

F======================================================================
FAIL: test_addition (math-test.MathTest)

----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/emabishi/Desktop/Projects/Personal/python-ci/tests/math-test.py", line 8, in test_addition
    self.assertEqual(Math.addition(3, 4), 8)
AssertionError: 7 != 8

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (failures=1)

Github ( Github )

Let's create a repository on Github that will hold our application.

让我们在Github上创建一个存储我们的应用程序的存储库。

Once you've signed up, create one. I'll be calling mine python-ci.

注册后,创建一个。 我将打电话给我的python-ci

Create New Github Repository

Next, it's time to initialise our local project directory as a git repository and add a reference to our github remote. At the root of your project, run the following commands:

接下来,是时候将本地项目目录初始化为git存储库,并添加对github远程服务器的引用。 在项目的根目录下,运行以下命令:

git init && git remote add origin <enter-your-github-repository-url-here>

A second option would be to clone the Github respository to our local machine. We can do this with a single command.

第二种选择是将Github存储库克隆到我们的本地计算机。 我们可以用一个命令来完成。

git clone <enter-your-github-repository-url-here>

Let's create a new branch called develop and check out to it with the command

让我们创建一个名为develop的新分支,并使用以下命令签出该分支

git checkout -b develop

After this, we can add and commit our previous changes using

之后,我们可以使用添加并提交之前的更改

git add . && git commit -m "<enter-short-commit-message-here>"

Whew, we're done. I'd pat you on the back if I could. I promise you that the hardest part's over.

ew,我们完成了。 如果可以的话,我会拍你的背。 我向你保证,最困难的部分已经过去。

圈CI ( Circle CI )

Circle CI is a service that helps us employ Continuous Integration by letting us build our application and run tests in a configurable environment.

Circle CI是一项服务,可帮助我们构建应用程序并在可配置的环境中运行测试,从而帮助我们采用持续集成。

It's also pretty handy because it can send us email and web notifications of the status of our current build. You can also integrate it with messaging services like Slack for real time notifications.

它也很方便,因为它可以向我们发送有关当前构建状态的电子邮件和网络通知。 您还可以将其与Slack等消息传递服务集成以进行实时通知。

We just created an account with Circle CI, so let's take full advantage of it. Log in to your Circle CI account using Github authentication.

我们刚刚使用Circle CI创建了一个帐户,因此让我们充分利用它。 使用Github身份验证登录到您的Circle CI帐户。

Once logged in, you'll be directed to your dashboard. Click on the fourth icon on the task bar on the left side of the screen. This is the projects tab and lists all your current Github repositories.

登录后,您将被定向到仪表板。 单击屏幕左侧任务栏上的第四个图标。 这是“项目”标签,其中列出了您当前的所有Github存储库。

You'll see a page like the one below.

您会看到类似下面的页面。

Circle CI projects page

Click on the build project button next to the name of the repository you created before. Sit back, relax and watch what happens.

单击您之前创建的存储库名称旁边的构建项目按钮。 坐下来,放松一下,看看会发生什么。

Circle CI runs our build but eventually errors out. Helpfully, it provides us with an error message.

Circle CI运行我们的构建,但最终会出错。 有用的是,它为我们提供了一条错误消息。

No circle.yml

No tests

To configure settings for the build on Circle CI, we'll need to create a configuration file in the form of a file with a .yml extension. Let's create a circle.yml file at the root of our project.

要配置Circle CI上的构建设置,我们需要以扩展名为.yml的文件的形式创建一个配置文件。 让我们在项目的根目录下创建一个circle.yml文件。

touch circle.yml

Fill it with the following:

填写以下内容:

machine:
  python:
    version: 3.5.2

dependencies:
  override:
    - pip install -r requirements.txt

test:
  override:
    - nosetests tests/math-test.py

The machine section configures the virtual machine we're using. Here we're explicitly defining that the machine should run python version 3.5.2.

机器部分配置我们正在使用的虚拟机。 在这里,我们明确定义了机器应运行python版本3.5.2。

We use the dependencies section to install our application prerequisites and the test section to specify the command which will trigger our tests.

我们使用依存关系部分来安装我们的应用程序先决条件,并使用测试部分来指定将触发我们的测试的命令。

As you can see, it's exactly the command we used to run our tests locally.

如您所见,这正是我们在本地运行测试所使用的命令。

As expected, with much pomp and circumstance, our tests have failed and failed loudly. We'll remedy this soon enough. Leave them in their imperfect state for now.

不出所料,在很多浮夸的情况下,我们的测试失败了,而且失败很大。 我们将尽快对此进行补救。 现在让它们保持不完美状态。

Failed Tests Circle CI

覆盖率报告 ( Coverage reporting )

To ascertain the extent to which the tests we've written cover our code, we'll use Coverage.py. We've already installed it so there's little we have to do now.

为了确定编写的测试覆盖代码的程度,我们将使用Coverage.py 。 我们已经安装了它,所以我们现在不需要做任何事情。

At the root of our project, run

在我们项目的根本,运行

coverage run src/*.py&& coverage report

Expect similar results

期待类似的结果

Name                 Stmts   Miss  Cover
----------------------------------------
tests/math-test.py       5      3    40%

You can tweak Coverage.py reporting in all sorts of interesting ways. If you're interested, have a look here. For our purposes, the default reporting will do for now.

您可以通过各种有趣的方式调整Coverage.py报告。 如果您有兴趣,请在这里看看。 就我们的目的而言,默认报告现在将执行。

工作服 ( Coveralls )

The next service we're going to take advantage of is Coveralls. Coveralls displays our test coverage for all to see, thus making efforts at Continuous Integration loud and visible.

我们要利用的下一个服务是Coveralls 。 Coveralls将显示我们的测试范围,以供所有人查看,从而使“持续集成”的工作变得清晰可见。

Once you sign in, you'll be directed to your dashboard.

登录后,您将被定向到仪表板。

Coveralls Dashboard

Click on the second icon on the task bar on the left side of the screen. This is the add repo tab and lists all your Github repositories that are currently synced to Coveralls.

单击屏幕左侧任务栏上的第二个图标。 这是“添加存储库”选项卡,其中列出了当前已同步到Coveralls的所有Github存储库。

To refresh the list of repositories under your account, click on the Options menu on the right of the page linked to your Github account

要刷新您帐户下的存储库列表,请单击链接到您的Github帐户的页面右侧的“选项”菜单。

Options Circle CI Refresh repositories

Next, search of the name of the repository we recently created. In this case, I'm looking for python-ci. Your repository name may be different. Click on the switch to turn it to its ON position.

接下来,搜索我们最近创建的存储库的名称。 在这种情况下,我正在寻找python-ci 。 您的存储库名称可能不同。 单击开关将其转到ON位置。

Coveralls Switch ON repository

Click the details button next to the name of the repository. You'll see something like this:

单击存储库名称旁边的详细信息按钮。 您会看到以下内容:

Coveralls set up

Take note of the repo token.

注意回购令牌。

To register our CI build with the Coveralls service, we have to perform some configuration. Let's do this by setting our Coveralls repository token as an environment variable on Circle CI.

要使用Coveralls服务注册我们的CI版本,我们必须执行一些配置。 让我们通过将Coveralls存储库令牌设置为Circle CI上的环境变量来完成此操作。

Screenshot of gear icon on Circle CI build page

Click on the gear button on the top right of the Circle CI build page. Follow along by clicking the Environment Variables tab on the left of the page. Finally, click the Add variable button and add the token like this.

单击Circle CI构建页面右上方的齿轮按钮。 单击页面左侧的“环境变量”选项卡。 最后,单击“添加变量”按钮并添加令牌。

coveralls repo token page on Circle CI

Now, let's edit our circle.yml file to send coverage data to the Coveralls service after our tests run. Edit the circle.yml to look like this.

现在,让我们编辑circle.yml文件,以在测试运行后将覆盖率数据发送到Coveralls服务。 编辑circle.yml以使其看起来像这样。

machine:
  python:
    version: 3.5.2
dependencies:
  override:
    - pip install -r requirements.txt
test:
  override:
    - nosetests tests/math-test.py
  post:
    - coverage run src/*.py
    - coverage report
    - coveralls

Add and push the changes to your remote Github repository. The push will trigger another build on Circle CI.

添加更改并将其推送到您的远程Github存储库。 该推送将触发Circle CI的另一个构建。

If we go back to our Coveralls dashboard, we'll notice our dashboard now displays our test coverage percentage. We can even tell at a glance how our coverage has changed over time.

如果返回到“工作服”仪表板,我们会注意到我们的仪表板现在显示了我们的测试覆盖率。 我们甚至可以一目了然地了解我们的覆盖范围是如何随着时间变化的。

Sometimes it takes a while for the data to reflect, so give it a few minutes.

有时数据需要一段时间才能反映出来,因此请花几分钟。

Coveralls graph of python ci repo

Github状态检查 ( Github Status Checks )

Going even further, in the spirit of Continuous Integration, we can prevent pushes or merges to the main branch of a repository until all required checks pass. In our case, these checks will be our tests.

更进一步,本着持续集成的精神,我们可以阻止推送或合并到存储库的主分支,直到所有必需的检查都通过。 就我们而言,这些检查将是我们的测试。

We'll implement this using a helpful tool by Github called Status Checks.

我们将使用Github的一个有用的工具“ 状态检查”来实现这一点。

Let's navigate to the settings tab of the repository we created on Github. Under the settings tab, select the Branches menu.

让我们导航到我们在Github上创建的存储库的设置选项卡。 在设置标签下,选择“分支”菜单。

On the protected branches menu, choose to protect the master branch.

在“受保护的分支”菜单上,选择“保护主分支”。

Protect master

From there, make the following selections and save your changes. You'll be prompted to enter your password to authorise the changes.

从此处进行以下选择并保存更改。 系统将提示您输入密码以授权更改。

Github save changes on branch status checks

Github branch protection options saved

To see the power of Github's Status Checks in action, let's make a Pull Request comparing the master branch of our repository with the develop branch.

要查看Github的状态检查功能的强大功能,让我们发出一个Pull Request,将我们存储库的master分支与developer分支进行比较。

As can be seen below, our tests failed on Circle CI. Therefore, because of the checks we put in place, merging to master is blocked.

如下所示,我们的测试在Circle CI上失败了。 因此,由于我们进行了检查,因此阻止了与master的合并。

Checks failing

Let's fix our tests, make a push to Github and watch our Pull Request for any changes.

让我们修复测试,推送到Github并观察我们的Pull Request是否有任何更改。

Wohoo! Green all the way. We can now merge our work to the master branch with assurances that all our Continuous Integration checks have passed.

哇! 一路绿色。 现在,我们可以确保将所有“持续集成”检查都通过了,从而将我们的工作合并到master分支中。

All checks passing

结论 ( Conclusion )

We've demonstrated that employing a Continuous Integration strategy in software development leads to major benefits in the quality of our work and the rapidity at which we resolve issues and conflicts within our application code.

我们已经证明,在软件开发中采用持续集成策略可以为我们的工作质量和快速解决应用程序代码中的问题和冲突带来重大好处。

Even more of interest is that when paired with Continuous Deployment strategies, CI becomes an even more powerful and capable tool. We've not gone into leveraging Continuous Deployment in our workflow, but I trust that you'll look into the possibilities that its use opens up.

更加令人感兴趣的是,与持续部署策略配合使用时,CI成为功能更强大,功能更强大的工具。 我们尚未在工作流程中充分利用“持续部署”,但我相信您会研究使用它的可能性。

However, there's a lot more we can do with CI which we've not gone into here. If you're interested, I'm leaving some links to a few resources which I believe will prove extremely helpful.

但是,CI还可以做很多事情,而我们在这里还没有涉及。 如果您有兴趣,我会留下一些资源的链接,我认为这些资源将非常有用。

资源资源 ( Resources )

If you'd like to read more about Continous Integration, here are a few places you can start with.

如果您想了解有关持续集成的更多信息,可以从这里开始。

  1. AWS

    AWS
  2. Martin Fowler

    马丁·福勒

A few other 3rd party CI tools are:

其他第三方CI工具是:

  1. Jenkins

    詹金斯
  2. TeamCity

    团队城市
  3. Bamboo by Atlassian

    竹由Atlassian
  4. Hudson

    哈德森
  5. goCD

    光盘

This list is by no means exhaustive.

此列表绝不是详尽无遗的。

I'd love some feedback on this guide. If you have a moment, drop me a comment in the box below. Also, if you have any questions, don't be shy, let me know.

我希望对本指南有一些反馈。 如果您有时间,请在下面的框中给我评论。 另外,如果您有任何疑问,请不要害羞,让我知道。

翻译自: https://scotch.io/tutorials/continuous-integration-with-python-and-circle-ci

python circle

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值