920 likes | 957 Views
持续集成 —— 改善软件质量并降低风险的方法. Continuous Integration. 主要内容. 简介 CI 原理 CI 实践 建立一个特征全面的 CI 系统 CI 的未来. 1 简介. 内容. The components of a CI system Features of CI Questions. 1.1 The components of a CI system. Developer. 一旦修改完代码后,会进行本地 build 通过后,将变化提交到版本控制库 这一步可以随时发生,而且并不影响 CI 过程的后续步骤
E N D
持续集成——改善软件质量并降低风险的方法 Continuous Integration
主要内容 • 简介 • CI原理 • CI实践 • 建立一个特征全面的CI系统 • CI的未来
内容 • The components of a CI system • Features of CI • Questions
Developer • 一旦修改完代码后,会进行本地build • 通过后,将变化提交到版本控制库 • 这一步可以随时发生,而且并不影响CI过程的后续步骤 • 本地build是减轻集成负担的必要步骤
Version Control Repository • 版本控制是项目管理的标准 • 版本控制库为源代码和文档提供“single source point” • 配置管理软件 • Subversion,PVCS, ClearCase, MKS,
CI Server • CI服务器在改变提交的时候运行build脚本 • CI服务器也可以是硬调度的 • 这样的服务器不能真正成为CI • 必要时,也可以手工运行build脚本
Build Script • Build脚本应该包括 • 编译、测试、审查和发布等活动
Feedback Mechanism • CI技术的一个关键目的是提供集成build的反馈
Integration Build Machine • 运行CI服务器的单独计算机,便于快速build • 以最快速度产生反馈
1.2 Features of CI • Four features requiredfor CI • A connection to a version control repository • A build script • Some sort of feedback mechanism (such as e-mail) • A process for integrating the source code changes (manual or CI server)
CI系统特征 • 源代码编译是CI系统最基本和普遍的特征 • 有人认为源代码编译和CI是同义词 • 这种看法有些片面,因为CI具有许多其他特有特征 • 动态语言的源代码编译特征更为明显 • Python,Ruby,PHP等
Database Integration • 数据库集成和源代码集成是一个完整的过程,因为数据库是软件的一个重要集成成分
Testing • 测试是CI有别于源代码编译的重要特征 • 它在CI系统中的重要性是毋庸置疑的 • 但是,必须寻求一种自动测试的方法 • 这种测试可以是验收测试
Inspection • 自动化代码审查有助于增强软件质量 • 代码审查必须强制一些规则,比如不允许出现坏味道等 • Checkstyle是一款较好的开源软件
Documentation and Feedback • 有些人认为文档应归于源代码 • 但是具有精选类、变量、实例和方法名字本身就是很好的文档 • 有许多很好的工具可以生成文档,如JavaDoc • 一个良好CI系统的重要特征是速度
1.3 Questions • Are you using a version control repository (or SCM tool)? • Is your project’s build process automated and repeatable? Does it occur entirely without intervention? • Are you writing and running automated tests? • Is the execution of your tests a part of your build process? • How do you enforce coding and design standards? • Which of your feedback mechanisms are automated? • Are you using a separate integration machine to build software?
内容 • What Is the Value of CI? • Adopt CI • CI Complement Other Development Practices • Seven Practices • Reducing Risks Using CI
2.1 What Is the Value of CI? • Reduce risks • Reduce repetitive manual processes • Generate deployable software at any time and at any place • Enable better project visibility • Establish greater confidence in the software product from the development team
Reduce Risks • Defects are detected and fixed sooner • Health of software is measurable • Reduce assumptions
Reduce Repetitive Processes • The process runs the same way every time. • An ordered process is followed • The processes will run every time a commit occurs in the version control repository.
Generate Deployable Software • CI can enable you to release deployable software at any point in time.
Enable Better Project Visibility • Effective decisions • Noticing trends
2.2 What Prevents Teams from Using CI? • Increased overhead in maintaining the CI system • Too much change • Too many failed builds • Additional hardware/software costs • Developers should be performing these activities
How Do I Get to “Continuous” Integration? • These steps can be applied one by one to virtually every activity you conduct on a project. • Identify • Build • Share • Make it continuous
When and How Should a Project Implement CI? • It is best to implement CI early in the project. • it is more difficult to implement CI late in a project • CI is not just a technical implementation; it is also an organizational and cultural implementation. • How? • At first the build can just compile the source code and package the binaries • Then, learn the testing tool • running these tests (and inspections) with every change.
2.3 How Does CI Complement Other Development Practices? • Developer testing • Coding standard adherence • Refactoring • Small releases • Collective ownership
2.4 Seven Practices • Commit code frequently • Don’t commit broken code • Fix broken builds immediately • Write automated developer tests • All tests and inspections must pass • Run private builds • Avoid getting broken code
2.5 Reducing Risks Using CI • Risk: Lack of Deployable Software • Risk: Late Discovery of Defects • Risk: Lack of Project Visibility • Risk: Low-Quality Software
It Works on My Machine • Eliminating tight coupling between your IDE and your build processes. • Use a separate machine solely for integrating the software.
Synching with the Database • Place all database artifacts in your version control repository. • Rebuild the database and data from your build script, by dropping and recreating your database and tables. Next, apply the stored procedures and triggers, and finally, insert the test data. • Test (and inspect) your database.
The Missing Click • automated the deployment process by adding it to the Ant build scripts that use the application server command-line options. • reduced the bottleneck of waiting for someone else to deploy the software and eliminated mistakes.
Regression Testing • Write test code for all of your source code (an xUnit framework is a good place to start). • Run tests from your build script (Ant or NAnt are the most common). • Run tests continuously as a part of your CI system so that they are executed at every checkin to the version control repository (using CruiseControl or a similar CI server).
Test Coverage • run a code coverage tool to assess the amount of source code that is actually executed by the tests. • Using CI can ensure this test coverage is always up to date.
“Did You Get the Memo?” • we installed and configured a CruiseControl CI server on our projects with the automated mechanism that sends e-mails to affected parties when a build fails. • we added SMS notifications so that we received text messages on our mobile phones
Inability to Visualize Software • we began generating diagrams of the design using the CI system. • we also chose to create a simple one- or two-page architecture document that described the software architecture, identifying the key components and interface for new developers.
Coding Standard Adherence • we created a onepage annotated class that contained all of the coding standards. • We enforced the coding standard by using automated inspection tools as a part of the build scripts initiated by CruiseControl. • used Checkstyle and PMD to report any lines of code that were not meeting the established standards.
Architectural Adherence • Add automated inspection tools to assess adherence to the project’s architectural standards.
Duplicate Code • Add automated inspection tools such as PMD’s CPD7 or the Simian8 static analysis tools to report duplicate source code. • Refactoring!!!
内容 • Automate Builds • Perform Single Command Builds • Separate Build Scripts from Your IDE • Centralize Software Assets • Create a Consistent Directory Structure • Fail Builds Fast • Build for Any Environment
内容 • Build Types and Mechanisms • Use a Dedicated Integration Build Machine • Use a CI Server • Run Manual Integration Builds • Run Fast Builds • Stage Builds
3.1 Automate Builds • By writing automated build scripts, you reduce the number of manual, repetitive, and error-prone processes performed on a software project. • the more processes added to a build, the slower the feedback. • if you are just trying out a couple of changes and do not intend to commit anything, you may choose to run a lighter-weight build that may only perform a compile and then execute a few unit tests. • Some of the most popular build tools include Ant for Java and NAnt for .NET.
3.2 Perform Single Command Builds • “Get everything you need into source control and get it so that you can build the whole system with a single command.” • The high-level steps for building your software go something like: 1. Create your build using a build scripting tool like NAnt, Rake, Ant, or Maven. Keep it simple at first; you can add more processes as you go. 2. Add each process (clean, compile, etc.) of the Integrate button within the build script. 3. Run the script from an IDE or the command line to build software.