Professional Documents
Culture Documents
84
CONTENTS
Continuous Integration:
About Continuous Integration
Patterns and Anti-Patterns
ABOUT CONTINUOUS INTEGRATION coverage reports can be run automatically to ensure all tests
pass and code coverage does not slip.
Continuous Integration (CI) is about minimizing code conflicts
and maximizing efficiency. It describes an automated process • Production Snapshot - Building from scratch means that the
designed to build a project whenever the codebase changes. build on QA is going to be the same on UAT, which is going
Continuous Integration starts with developers committing code to be the same as production.
to a shared repository one or more times a day. It ends with the
CI system successfully building the project from scratch. • Early Error Detection - Code that passes all the linting,
unit tests, and e-2-e tests can still fail to build for unexpected
CI has been around for a long time, and during that time, reasons. A build failure is identified immediately after the
the communities around it have developed best practices. code is merged in, making it easy to identify the broken
Explaining these best practices can be done by describing commit(s).
patterns (i.e., a solution to a problem in a particular context) and
anti-patterns (i.e., generally ineffective approaches to solving a • Project Confidence - All of this adds up to increased
problem). Anti-pattern solutions tend to produce adverse effects. confidence in the product from developers, managers,
and customers.
WHAT IS CONTINUOUS INTEGRATION?
CI needs to be implemented properly to reap these benefits.
Conventionally, the term “Continuous Integration” refers to the
Everything starts from the point of change. Every time a change
“build and test” cycle. Individual developers merge their code into
is merged into the master branch, then a build job must be run.
one shared project master branch. CI servers rebuild the project
from scratch every time a code merge happens.
PATTERNS AND ANTI-PATTERNS
Conversations about Continuous Integration tend to couple CI
Patterns, and corresponding anti-patterns, are discussed in
with Continuous Delivery (CD). CD differs from CI by referring to
the following sections. According to Google trends, the most
the movement of code from one environment to another, such as
searched for version control tool is Git and the most commonly
development to QA to User Acceptance Testing. CI servers will searched CI software is Jenkins.
usually do CD as well. The two concepts are inexorably linked
since they work hand in hand. There are other DZone Refcardz Continued on next page
that you should consider downloading if you wish to understand
Continuous Delivery better, such as, “Preparing for Continuous
Delivery” or “Continuous Delivery: Patterns and Anti-Patterns in
the Software Lifecycle”. This Refcard is going to focus specifically
on CI patterns, although there will occasionally be some overlap
since the two are so closely related.
Rapid7 InsightOps combines log management with IT asset search for a new
approach to infrastructure and application monitoring and troubleshooting.
It takes only minutes to setup and is free to try.
Centralize
Easily centralize data across your infrastructure, from
system logs to IT assets.
Monitor
Monitor your IT environment for anomalies, key
metrics, and critical events.
Answer
Get the answers you need, when you need them, to
resolve critical issues and maintain uptime.
• Master - The main branch from which builds are run should
be the master, or mainline, branch. This branch should be
heavily protected. Developers should be able to merge code
Where examples are provided, we will use the Git Command Line into the master branch but should not be able to commit
Interface commands or Git Plugin hooks for Jenkins. This Refcard code directly. The master branch will host your project’s
will use those two technologies for examples based solely on main history and milestone builds. No code should ever be
search popularity. added to it directly.
Organize source code changes by task- Not labeling builds, using revisions or branches
Anti-patterns
Pattern oriented units of work and submit changes as a as “labels.”
Task-Level Commit.
BUILD PRACTICES
Keeping changes local to development for
several days and stacking up changes until PRE-MERGE BUILD
Anti-patterns
committing all changes. This often causes build The automated build discussed above occurs on the CI server
failures or requires complex troubleshooting. using shared resources. VCS systems can be configured to
perform a fast, stripped-down build locally first to pre-check
LABEL BUILD the code.
When master has reached an important development or release
The procedure in Git works using the project’s .git directory.
milestone, give it a name to mark that code state. Typically, this
Update the hooks/pre-push file to run the CLI command for
will be, or will incorporate, the release version number using
your project’s build/compilation/bundling tool. For example, a
semantic versioning (major.minor.patch, i.e. 1.10.23).
JavaScript or Node project might use a tool called Gulp:
Git uses the term “tagging” instead of “labeling”. To create a tag
gulp <my project>
for the current build, use the following commands:
git tag -a annotation -m “message” (i.e., git tag -a Verify that your changes will not break the
v1.2.12 -m “version 1.2.12”) integration build by performing a pre-merge
Pattern
git push remote --tags build—either locally or using Continuous
Integration.
ready to handle and resolve the problem immediately. The worst the required build scripts so that building can still happen from
possible way to handle a failure in a build is to ignore it, or expect the IDE. Every project should include its own build scripts so it
it to be resolved in a future build. You should consider taking all can be built from anywhere it is being worked on.
the following steps:
Create build scripts that are decoupled from
• Fix Broken Builds Immediately - Although it is the team’s
IDEs, but can be invoked by an IDE. These
responsibility, the developer who recently committed code Pattern
build scripts will be executed by a CI system as
must be involved in fixing the failed build. It is possible the well so that software is built at every change.
problem was a result of a lack of knowledge, so it is a good
idea to have a seasoned developer available to assist if Relying on IDE settings for Automated Build.
Anti-patterns
needed. Build cannot run from the command line.
DEVELOPER DOCUMENTATION
Ensure all build processes can be run through a
The build process is an excellent opportunity to generate Pattern
single command.
documentation for your source code. Developers tend to dislike
writing documentation manually, and keeping documentation Requiring people or servers to enter
up to date manually can be time-consuming. The preferred multiple commands and procedures in the
approach is to incorporate documentation into your code, and deployment process, such as copying files,
Anti-patterns
modifying configuration files, restarting a
then having the build process generate documentation from the
server, setting passwords, and other repetitive,
code. This keeps documentation up-to-date and does not create error-prone actions.
more work for the development team.
DEDICATED RESOURCES
Generate developer documentation with builds
Pattern CI builds of master should be performed on servers (real or
based on checked-in source code.
virtual) that are only tasked with building the project. These
Manually generating developer documentation. dedicated machines should have sufficient resources to build
This is both a burdensome process and one in the project smoothly and swiftly to limit developer downtime.
Anti-patterns which the information becomes useless quickly Performing builds on a dedicated machine ensures a clean
because it does not reflect the checked-in environment that doesn’t introduce unexpected variables. Clean
source code.
builds give a certain degree of reassurance that the project will
build successfully when being deployed to other environments.
BUILD CONFIGURATION
Run master builds on a separate dedicated
INDEPENDENT BUILD Pattern
machine or cloud service.
Builds should happen the same way on all machines that run
them. A build on a developer machine should run the same Relying on existing environmental and
Anti-patterns configuration assumptions (can lead to the
procedure as the CI server. Therefore, train developers to not use
“but it works on my machine problem”).
the IDE build process. Instead, the IDE can be configured to run
EXTERNALIZE AND TOKENIZE CONFIGURATION servers, and QA machines. No individual or server working with
Configuration information that is specific to a machine or the project should have to worry about the integrity of their data,
deployment environment should be a variable in any build and or the integrity of some other entity’s data, while coding, building,
configuration scripts. These values should come from the build and testing.
process so they can be build or environment specific. Files that
use this information should use tokens so that the build process This is true of schema, but not necessarily data. The data for
can replace them with actual values. For example, a build might each environment should be a subset of the whole, and scrubbed
be supplied a hostname, then any configuration file that needs of sensitive information. For example, a developer should have
hostname should use $hostname for that value. The build access to a lightweight version of the database with a very small
process will go through all config files and replace $hostname subset of records. However, the data they do have shouldn’t
with the correct value every time the server is built. This lets the have real social security numbers, addresses, etc. Use discretion
build process create the project on as many different machines and ahere to any relevant regulations when deciding what data
as possible. to use for database population.
Anti-patterns
Hardcoding values in configuration files or • Create a lightweight version of your
using GUI tools to do the same. database (only enough records to test
functionality)
• Use a command line interface to populate
DATABASE Pattern
the local database sandboxes for each
Databases are the cornerstones of all modern software projects. developer, tester, and build server
No project of any scale beyond a prototype can function • Use this data in development environments
without some form of database. For this reason, databases to expedite test execution
should be included in the Continuous Integration process. They
should be treated with the same extensibility and care as project
Anti-patterns Sharing development database.
software code.
and that the code is maintaining superior quality. CI cannot be AUTOMATED SMOKE TEST
successful without a robust testing method. Without testing, CI Smoke tests are a subset of tests used to confirm the functionality
can become chaotic. of the most important elements of a project. They function
as gatekeeper to confirm that building, full testing, or QA can
AUTOMATED TESTS continue. A suite of well-designed smoke tests can save QA
Tests should run with every build. The build scripts described in personnel time and effort by checking the most likely candidates
for failure first.
the sections above should include running tests against all code
in the project. Tests should be as comprehensive as possible and
This is also useful for Continuous Deployment. Smoke tests can
can include unit tests, end-to-end tests, smoke tests, or UI tests.
be designed to check functionality most sensitive to changes
Testing should be done on all new code. Tests for new code
in the environment. It is an easy way to confirm that the
should be a requirement of a successful build. All code
deployment will work.
being merged into the project should be required to meet an
appropriate code coverage level. All modern test running suites
will have some form of coverage reporter that can be tied into Create smoke tests that can be used by CI
the build process. Any code submitted without sufficient test servers, developers, QA, and testing as a
coverage should fail. Pattern pre-check to confirm the most important
functionality as they work, or before
committing resources to a full build.
A B O U T T H E AU T H O R
DAVID POSIN has been involved in the Information Technology Industry for two decades.
Fifteen years of that time was spent consulting with many companies in a wide range of industries to
build solid technology stacks and robust application architectures. David has watched the Cloud and
the World Wide Web grow from their infancy, and now spends every day fully entrenched in those
worlds. Currently, David builds high-performance web applications and offers professional technical
writing services.
DZone communities deliver over 6 million pages each month to more than 3.3 million
software developers, architects and decision makers. DZone offers something for
everyone, including news, tutorials, cheat sheets, research guides, feature articles,
source code and more.
DZONE, INC. REFCARDZ FEEDBACK
"DZone is a developer's dream," says PC Magazine. 150 PRESTON EXECUTIVE DR. WELCOME
refcardz@dzone.com
CARY, NC 27513
Copyright © 2017 DZone, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval SPONSORSHIP
system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior 888.678.0399 OPPORTUNITIES
written permission of the publisher. 919.678.0300 sales@dzone.com