Dzone Refcard84 Cipatternsandantipatterns
Dzone Refcard84 Cipatternsandantipatterns
Dzone Refcard84 Cipatternsandantipatterns
CONTENTS
öö WHAT IS CONTINUOUS
INTEGRATION?
CI Patterns and
öö PATTERNS AND ANTI-PATTERNS
öö BUILD MANAGEMENT
öö BUILD PRACTICES
öö BUILD CONFIGURATION
Anti-Patterns öö
öö
DATABASES
WHAT IS CONTINUOUS INTEGRATION? • Production Snapshot - Building from scratch means that the
Conventionally, the term "Continuous Integration" refers to the build on QA is going to be the same on UAT, which is going to
"build and test" cycle. Individual developers merge their code into be the same as production.
DZO N E .CO M/ RE FCA RDZ
1
Build and ship software
faster with the largest open
source developer community.
GitHub Enterprise
Code, collaborate,
secure, and ship. Faster.
TASK-LEVEL COMMIT not be expected to kick off CI builds manually to avoid impacting
Most modern version control software integrates this best practice development progress.
into its core functionality. A developer moves code into the VCS
Setting up an automatic build between Git and Jenkins uses the
when they have made enough progress to consider saving it. Usually
following procedure. Inside the project's .git directory, update the
the process involves adding the changed code files to a commit, and
hooks/post-receive file to send the repository to your CI tool (in this
then committing them with a relevant message.
case Jenkins). Your CI tool should be installed with a Git plugin to
For example, in Git, a task-level commit uses the following commands: accept the curl.
git tag -a annotation -m "message" (i.e., git tag -a The procedure in Git works using the project's .git directory. Update
v1.2.12 -m "version 1.2.12") the hooks/pre-push file to run the CLI command for your project's
git push remote --tags build/compilation/bundling tool. For example, a JavaScript or Node
project might use a tool called Gulp:
Tag or Label the build with unique name so
PATTERN that you can refer to run the same build at gulp <my project>
another time.
Verify that your changes will not break the
Not labeling builds, using revisions or
ANTI-PATTERNS integration build by performing a pre-merge
branches as "labels." PATTERN
build---either locally or using Continuous
Integration.
BUILD MANAGEMENT
Code being merged into master should cause the CI server to Checking in changes to a version-control
automatically build the project, including the new changes. There ANTI-PATTERNS repository without running a build on a
should be no developer or DevOps engineer involvement required. developer's workstation.
• Hipchat
DEVELOPER DOCUMENTATION
• Slack
The build process is an excellent opportunity to generate
• SMS
documentation for your source code. Developers tend to dislike
• Web Push Notifications
writing documentation manually, and keeping documentation up
• Campfire
to date manually can be time-consuming. The preferred approach is
• Any infrastructure tool with extensions that your
to incorporate documentation into your code, and then having the
organization uses
build process generate documentation from the code. This keeps
documentation up-to-date and does not create more work for the
Send automated feedback from the CI server
development team.
PATTERN to development team members involved in
the build.
Generate developer documentation with
PATTERN
Sending minimal feedback that provides builds based on checked-in source code.
no insight into the build failure or is non-
actionable. Sending too much feedback, Manually generating developer
ANTI-PATTERNS
including to team members uninvolved with documentation. This is both a burdensome
the build. This is eventually treated like spam, ANTI-PATTERNS process and one in which the information
which causes people to ignore messages. becomes useless quickly because it does
DZO N E .CO M/ RE FCA RDZ
handle and resolve the problem immediately. The worst possible Builds should happen the same way on all machines that run them.
way to handle a failure in a build is to ignore it, or expect it to A build on a developer machine should run the same procedure as
be resolved in a future build. You should consider taking all the the CI server. Therefore, train developers to not use the IDE build
following steps: process. Instead, the IDE can be configured to run the required build
scripts so that building can still happen from the IDE. Every project
• Fix Broken Builds Immediately - Although it is the team's should include its own build scripts so it can be built from anywhere
responsibility, the developer who recently committed code it is being worked on.
must be involved in fixing the failed build. It is possible the
problem was a result of a lack of knowledge, so it is a good idea Create build scripts that are decoupled from
to have a seasoned developer available to assist if needed. IDEs, but can be invoked by an IDE. These
PATTERN
• Always Pull Master and Build - Developers should pull the build scripts will be executed by a CI system as
latest code into their branch from master before pushing well so that software is built at every change.
DATABASE
Ensure all build processes can be run Databases are the cornerstones of all modern software projects. No
PATTERN
through a single command. project of any scale beyond a prototype can function without some
form of database. For this reason, databases should be included in
Requiring people or servers to enter
the Continuous Integration process. They should be treated with the
multiple commands and procedures in the
same extensibility and care as project software code.
deployment process, such as copying files,
ANTI-PATTERNS
modifying configuration files, restarting SCRIPTING DATABASE CHANGES
a server, setting passwords, and other All changes made to a database during development should be
repetitive, error-prone actions. recorded via database scripts. The CI process can then run scripts
as the project is built. It is an anti-pattern to expect any manual
DEDICATED RESOURCES manipulation of a database during the build process. A database for
CI builds of master should be performed on servers (real or virtual) the project should be able to be migrated to new changes regardless
DZO N E .CO M/ RE FCA RDZ
that are only tasked with building the project. These dedicated of timing or platform.
machines should have sufficient resources to build the project
smoothly and swiftly to limit developer downtime. Performing
All changes made to a database during
builds on a dedicated machine ensures a clean environment that
development should be recorded into
doesn't introduce unexpected variables. Clean builds give a certain PATTERN database scripts that can be run on every
degree of reassurance that the project will build successfully when
database on every platform hosting the project
being deployed to other environments.
(including developer machines, see below).
DATABASE SANDBOX
EXTERNALIZE AND TOKENIZE CONFIGURATION Every instance of the project should have its own version of the
Configuration information that is specific to a machine or deployment
database with a relevant set of data. This should include development
environment should be a variable in any build and configuration
machines, build machines, testing machines, testing servers, and QA
scripts. These values should come from the build process so they
machines. No individual or server working with the project should
can be build or environment specific. Files that use this information
have to worry about the integrity of their data, or the integrity of some
should use tokens so that the build process can replace them with
other entity's data, while coding, building, and testing.
actual values. For example, a build might be supplied a hostname,
then any configuration file that needs hostname should use This is true of schema, but not necessarily data. The data for each
$hostname for that value. The build process will go through all config environment should be a subset of the whole, and scrubbed of
files and replace $hostname with the correct value every time the sensitive information. For example, a developer should have
server is built. This lets the build process create the project on as access to a lightweight version of the database with a very small
many different machines as possible. subset of records. However, the data they do have shouldn't have
real social security numbers, addresses, etc. Use discretion and the project. Tests should be as comprehensive as possible and can
ahere to any relevant regulations when deciding what data to use include unit tests, end-to-end tests, smoke tests, or UI tests.
for database population.
Testing should be done on all new code. Tests for new code should
The CI process should include a way to import the data correctly into be a requirement of a successful build. All code being merged
the database. Any data import or manipulation should be scripted into the project should be required to meet an appropriate code
so it can be performed via command line. Developers, testers, and coverage level. All modern test running suites will have some form of
build machines shouldn't have to know the intricacies of the data. coverage reporter that can be tied into the build process. Any code
The build process in particular will need a command line command submitted without sufficient test coverage should fail.
to call during the build.
Write automated tests for each code path,
PATTERN
both success testing and failure testing.
• Create a lightweight version of your
• Not running tests
database (only enough records to test
ANTI-PATTERNS • No regression testing
functionality)
• Manual testing
• Use a command line interface to populate
PATTERN
the local database sandboxes for each
BUILD QUALITY THRESHOLD
developer, tester, and build server The build is also an appropriate time to check for code quality and
• Use this data in development coverage percentages. The project team should have a minimum
environments to expedite test execution coverage percentage that the project is not allowed to dip below. If
new code is merged in without sufficient tests, that percentage will
ANTI-PATTERNS Sharing development database.
be lower than expected, and that should trigger a failure.
DZO N E .CO M/ RE FCA RDZ
UPDATE SCRIPTS STORED IN THE VCS Code quality is important to the long-term maintainability of a
All scripts to perform database and data operations should be stored project, and the build step is a great place to verify code quality. The
in the version control system being used by the codebase. Scripts project team should have standards and best practices for the code
should be named and/or annotated to refer to their appropriate so most of it looks and works the same way. The build step should
version number to simplify automation. Keeping old versions is verify that new code meets those standards.
useful for doing multiple scripts if necessary, and to maintain a
history of changes. • Notify team members of code aberrations
such as low code coverage or the use of
Store the scripts for updating the database coding anti-patterns.
PATTERN
PATTERN and its data in the version control system with • Fail a build when a project rule is violated.
the code and annotate appropriately • Use continuous feedback mechanisms to
notify team members.
Storing update scripts in an alternative
ANTI-PATTERNS
location, i.e. a shared file server • Deep dive reviews of every code change.
ANTI-PATTERNS • Manually calculating or guesstimating
AUTOMATED TESTS This is also useful for Continuous Deployment. Smoke tests can be
Tests should run with every build. The build scripts described in designed to check functionality most sensitive to changes in the
the sections above should include running tests against all code in environment. It is an easy way to confirm that the deployment will work.
[1]
Addison-Wesley, Software Configuration Management Patterns,
2003, Berczuk and Appleton.
DZone, Inc.
DZone communities deliver over 6 million pages each 150 Preston Executive Dr. Cary, NC 27513
month to more than 3.3 million software developers, 888.678.0399 919.678.0300
architects and decision makers. DZone offers something for
Copyright © 2018 DZone, Inc. All rights reserved. No part of this publication
everyone, including news, tutorials, cheat sheets, research
may be reproduced, stored in a retrieval system, or transmitted, in any form
guides, feature articles, source code and more. "DZone is a or by means electronic, mechanical, photocopying, or otherwise, without
developer’s dream," says PC Magazine. prior written permission of the publisher.