continuous delivery - automate & build better software with travis ci

70
Continuo us Delivery @ wajrcs folio3, 26 Jan 2017 https://github.com/waqar-alamgir/l earning-travis

Upload: wajrcs

Post on 11-Apr-2017

90 views

Category:

Software


3 download

TRANSCRIPT

Page 1: Continuous Delivery - Automate & Build Better Software with Travis CI

ContinuousDelivery

@wajrcsfolio3, 26 Jan 2017

https://github.com/waqar-alamgir/learning-travis

Page 2: Continuous Delivery - Automate & Build Better Software with Travis CI

CaseStudies

Page 3: Continuous Delivery - Automate & Build Better Software with Travis CI

Agile 101&

The first sprint of a Team

Page 4: Continuous Delivery - Automate & Build Better Software with Travis CI

Iteration

Analysis + Design

Development

Testing + Showcase

0 1 2 3 4

Integration + QA Release and operation

Customer

Centralized QA IT Operations

"Agile" team

The "last mile"

Agile 101

Page 5: Continuous Delivery - Automate & Build Better Software with Travis CI

Barnes&Noble | NOOK

Page 6: Continuous Delivery - Automate & Build Better Software with Travis CI

disrupting traditional businesses

http://code.flickr.net/

Flickr, a Yahoo company - Web 2.0

Page 7: Continuous Delivery - Automate & Build Better Software with Travis CI

Why Releasing

Frequently?

Page 8: Continuous Delivery - Automate & Build Better Software with Travis CI

1. Build the right thing

Eric Ries, “The Lean Startup” http://bit.ly/8ZoX5F

Customer development

Agile product development

Releasing Frequently

Page 9: Continuous Delivery - Automate & Build Better Software with Travis CI

Do Less

“Far More than 50% of functionality in the Software is never or rarely used. Many are no value features.”

Page 10: Continuous Delivery - Automate & Build Better Software with Travis CI

•Create hypothesis

•Deliver minimum viable product

•Get feedback (repeat)

Eric Ries, “The Lean Startup” http://bit.ly/8ZoX5F

Data

Ideas

BuildLearn

Scientific Method

Measure

Code

Page 11: Continuous Delivery - Automate & Build Better Software with Travis CI

“Howlong would it take your organization to deploy a change that involvedjust one single line ofcode? Do you do this ona repeatable, reliable basis?”

Mary and Tom Poppendieck, Implementing Lean Software Development, p59.

Ask this Question

Page 12: Continuous Delivery - Automate & Build Better Software with Travis CI

1. build the rightthing

2. reduce risk of release

John Allspaw: “Ops Metametrics” http://slidesha.re/dsSZIr

Releasing Frequently

Page 13: Continuous Delivery - Automate & Build Better Software with Travis CI

Optimize for MTRS

Page 14: Continuous Delivery - Automate & Build Better Software with Travis CI

Mean Time Between Failures

Mean Time Restore Service

John Allspaw: “Building Resilience in Web Development and Operations” http://www.usievents.com/en/conferences/8-paris-usi-2011/sessions/968-john-allspaw

Optimize for MTRS

Page 15: Continuous Delivery - Automate & Build Better Software with Travis CI

1. build the rightthing

2. reduce risk of release

3. real projectprogress

Releasing Frequently

Page 16: Continuous Delivery - Automate & Build Better Software with Travis CI
Page 17: Continuous Delivery - Automate & Build Better Software with Travis CI

Releasing Frequently

Page 18: Continuous Delivery - Automate & Build Better Software with Travis CI

Our highest priority is to satisfy the customer throughearly and continuous delivery of valuable software

Agile Manifesto

Page 19: Continuous Delivery - Automate & Build Better Software with Travis CI

Software always be production ready.Releases tied to business needs, not operational constraints.

Continuous Delivery

Page 20: Continuous Delivery - Automate & Build Better Software with Travis CI

Fast, automated feedback on the production readiness of yourapplications every time there is achange -to code, infrastructure, or configuration

Production-ready Software

Page 21: Continuous Delivery - Automate & Build Better Software with Travis CI

automation

patterns and practices

collaboration

Continuous Delivery

Page 22: Continuous Delivery - Automate & Build Better Software with Travis CI

configuration managementMore: http://www.sl ideshare.net/wajrcs/infrastructure-automation-with-chef-ansible

continuous integration

automated testing

Ingredients

Page 23: Continuous Delivery - Automate & Build Better Software with Travis CI

Develop

Build

Mainline Server

Build

pull

Local Workstation

Buildpush

✔Done!

Page 24: Continuous Delivery - Automate & Build Better Software with Travis CI

“Cease dependence on massinspection to achieve quality.Improve the process and build quality into the product in the first place”

W. EdwardsDeming

Build quality

Page 25: Continuous Delivery - Automate & Build Better Software with Travis CI

Business facing

Technology facing

Critique project

Sup

port

prog

ram

min

g

AUTOMATED

Functional

acceptance tests

MANUAL

Showcases

Usability

testing

Exploratory

testing

Unit tests Non-functional

Integration tests acceptance testsSystem tests (performance, scaling, ...)

AUTOMATED MANUAL / AUTOMATED

Diagram invented by Brian Marick

Different Kinds of Testing

Page 26: Continuous Delivery - Automate & Build Better Software with Travis CI

an automated implementation of your system’s build, deploy, test, release process

visibility

feedback

control

Deployment pipeline

Page 27: Continuous Delivery - Automate & Build Better Software with Travis CI

Delivery team Version control Build & unit tests

Automated acceptance tests

User acceptance tests

Release

Check in

Feedback

Trigger

Check in

Feedback

Trigger

Trigger

Check inTrigger

Trigger

ApprovalApproval

Feedback

Feedback

FeedbackFeedback

Deployment Pipeline

Page 28: Continuous Delivery - Automate & Build Better Software with Travis CI

Why?• Easier to test• Easier to measure• Easier to follow• Streamlines the development process

Code Quality

When someone doesn’t follow the coding style guidelines

Page 30: Continuous Delivery - Automate & Build Better Software with Travis CI

• Use an indent of 4 spaces, with no tabs.• Control Structures:

<?phpif ((condition1) || (condition2)) {

action1;} elseif ((condition3) && (condition4)) {

action2;} else {

defaultAction;}?>

PEAR Coding Standard

Page 31: Continuous Delivery - Automate & Build Better Software with Travis CI

• Broad• Strict, but flexible• Based on a “standard” standard• Everyone must follow

Custom Standards

Page 32: Continuous Delivery - Automate & Build Better Software with Travis CI

"tokenises your PHP, JavaScript and CSS files and detects violations of a defined set of coding standards“ http://pear.php.net/package/PHP_CodeSniffer

• PEAR package• Single file or entire directory• Preset and customizable

PHP_CodeSniffer

Page 33: Continuous Delivery - Automate & Build Better Software with Travis CI

$ phpcs /myDir/myFile.php

FILE: /myDir/myFile.php-------------------------------------------------------------------------------- FOUND 3 ERROR(S) AFFECTING 3 LINE(S)--------------------------------------------------------------------------------2 | ERROR | Missing file doc comment20 | ERROR | PHP keywords must be lowercase;

expected "false" but found "FALSE" 47 | ERROR | Line not indented correctly;

expected 4 spaces but found 1--------------------------------------------------------------

------------------

Output

Page 34: Continuous Delivery - Automate & Build Better Software with Travis CI

• Unit - the smallest piece of testable code within my application or script.

• Unit test - a piece of code that executes the unit and then evaluates the result returned.

Unit Tests

Page 35: Continuous Delivery - Automate & Build Better Software with Travis CI

• Make sure the unit is small enough so the test is testing a single function.

• Make sure the test is efficient enough to run repeatedly, perhaps even a thousand times a day.

• Make sure the tests do not depend on each other. Each test should be able to run completely separately from other tests.

Tips

Page 36: Continuous Delivery - Automate & Build Better Software with Travis CI

function validateName($name) {if ((strlen($name) > 1) && (strlen($name) < 50)) {

if (ctype_alpha(str_replace(array(" ",",","-","'"),"",$name))) { return true;

}else {

return false;}}else {return false;}}

assert(validateName("Beth's Test Name"));

Page 37: Continuous Delivery - Automate & Build Better Software with Travis CI

How Many Tests?

Enough to test every basic function of the code.

Page 38: Continuous Delivery - Automate & Build Better Software with Travis CI

• Standardize test format• Easily run tests• Analyze results

Testing Frameworks

Page 39: Continuous Delivery - Automate & Build Better Software with Travis CI

PHPUnit - http://www.phpunit.de

Pros:• Good documentation• Lots of examples online• Integrates with many other popular tools and

platforms

Cons:• Command line only

Page 40: Continuous Delivery - Automate & Build Better Software with Travis CI

SimpleTest - http://www.simpletest.org/

Pros:• Run on command line or in browser• Can test front-end functionality

Cons:• Not as integrated as PHPUnit

Page 41: Continuous Delivery - Automate & Build Better Software with Travis CI

Selenium - http://seleniumhq.org/

Pros:• Can test front-end functionality• Selenium-WebDriver (makes direct calls to the

browser using each browser’s native support for automation)

Cons:• Not a native PHP tool, but bindings are

available from several third-parties

Page 42: Continuous Delivery - Automate & Build Better Software with Travis CI

• Perform a DB query to update the schema, clear a cache, upload files, run cron tasks, etc.

Automate The Build

Page 43: Continuous Delivery - Automate & Build Better Software with Travis CI

Phing - http://phing.info

• PHP project build system• Based on Apache Ant• XML build files and PHP "task" classes• Integrates with both PHPUnit and SimpleTest

as well as phpDocumentor• Platform independent• No required external dependencies

Page 44: Continuous Delivery - Automate & Build Better Software with Travis CI

Phing Buildfile:<?xml version="1.0" encoding="UTF-8"?>

<project name="FooBar" default="dist">

<!-- ============================================ --><!-- Target: prepare --><!-- ============================================ --><target name="prepare">

<echo msg="Making directory ./build" /><mkdir dir="./build" />

</target>

<!-- ============================================ --><!-- Target: build --><!-- ============================================ --><target name="build" depends="prepare">

<echo msg="Copying ./about.php to ./build directory..." /><copy file="./about.php" tofile="./build/about.php" />

</target><!-- ============================================ --><!-- (DEFAULT) Target: dist --><!-- ============================================ -->

<target name="dist" depends="build"><echo msg="Creating archive..." /><tar destfile="./build/build.tar.gz" compression="gzip">

<fileset dir="./build"><include name="*" />

</fileset></tar>

<echo msg="Files copied and compressed in build directory OK!" />

</target></project>

Page 45: Continuous Delivery - Automate & Build Better Software with Travis CI

Maven - http://maven.apache.org

• Supports Ant tasks• Large library of third-party plug-ins to

integrate other continuous integration tools• Helps shield you from the details of the

build• For Java-based projects, so you’ll need Maven

for PHP: http://www.php-maven.org/

Page 46: Continuous Delivery - Automate & Build Better Software with Travis CI

Capistrano - http://www.capistranorb.com

• A remote server automation and deployment tool written in Ruby.

Page 47: Continuous Delivery - Automate & Build Better Software with Travis CI

Capistrano File:

set :application, "yii_blog" #Edit your app nameset :repo_url, "https://github.com/waqar-alamgir/yii-capistrano.git" # EDIT your git repository

if ENV['user']set :repo_url, "https://" + ENV['user'] + "@github.com/waqar-alamgir/yii-capistrano.git" # EDIT your git repository

end

set :deploy_to, "/var/www/html/yii-capistrano/" # EDIT folder where files should be deployed toset :scm, "git"set :branch, ENV['branch'] || "master"

namespace :deploy do

# Writing permissions to /protected/runtime/ and /assets/desc "Restart"

after :updated, :build do on roles(:app) do within release_path do execute :chmod, "-R 777 #{release_path}/protected/runtime/"

execute :chmod, "-R 777 #{release_path}/assets/" end end end

# Copy yii configuration files /protected/config/desc "Copy config file"after :updated, :build do

on roles(:all) do |host|%w[ yii-config/main.php , yii-config/console.php ].each do |f|

upload! "" + f , "#{release_path}/protected/config/" + fend

endend

# Run yii database migrationsdesc "Build"

after :updated, :build do on roles(:app) do within release_path do execute :php, "#{release_path}/protected/yiic migrate --interactive=0" end end end end

https://github.com/waqar-alamgir/yii-capistrano

Page 48: Continuous Delivery - Automate & Build Better Software with Travis CI

• phpDocumentor 2: http://www.phpdoc.org/

• Merging phpDocumentor and DocBlox

• Automates documentation

• Tutorial:

http://manual.phpdoc.org/HTMLSmartyConverte r/HandS/phpDocumentor/tutorial_phpDocument or.howto.pkg.html

Documentation

Page 49: Continuous Delivery - Automate & Build Better Software with Travis CI

http://stackoverflow.com/questions/1310050/php-function-comments/3823007#3823007

/** * Does something interesting * * @param Place $where Where something interesting takes place * @param integer $repeat How many times something interesting should happen * * @throws Some_Exception_Class If something interesting cannot happen * @author Waqar Alamgir <[email protected]> * @return Status */

/** * Short description for class * * Long description for class (if any)... * * @copyright 2016 Zend Technologies * @license http://www.zend.com/license/3_0.txt PHP License 7.0 * @version Release: @package_version@ * @link http://dev.zend.com/package/PackageName * @since Class available since Release 1.2.0 */

Page 50: Continuous Delivery - Automate & Build Better Software with Travis CI
Page 51: Continuous Delivery - Automate & Build Better Software with Travis CI
Page 52: Continuous Delivery - Automate & Build Better Software with Travis CI

Technical Debt

Assigns a technical debt value• The debt• The cost to reimburse• The work to reimburse• The breakdown

When the technical debt unrolls

Page 53: Continuous Delivery - Automate & Build Better Software with Travis CI

1. Email pluginSends a build status email.

2. Lint plugin/ Php parallel lint pluginThis plugin runs PHP's built in Lint (syntax / error check) functionality.Similar to the standard PHP Lint plugin, except that it uses the PHP Parallel Lint project to run.

3. Pdepend pluginRuns PDepend software metrics.

4. PHP Code Sniffer pluginRuns PHP Code Sniffer against your build.

5. Php copy paste detector pluginRuns PHP Copy / Paste Detector against your build.

6. PHP Docblock CheckerRuns the PHP Docblock Checker against your build. This tool verifies that all classes and methods have docblocks.

7. Php loc pluginRuns PHPLoc against your project and records some key metrics.

8. Php mess detector pluginRuns PHP Mess Detector against your build. Records some key metrics, and also reports errors and warnings.

9. PhpUnit PluginRuns PHPUnit tests against your build.

10. Technical Debt PluginChecks all files in your project for TODOs and other technical debt.

Page 54: Continuous Delivery - Automate & Build Better Software with Travis CI

• Cruise Control• Hudson / Jenkins• PHP CI• Travis CI

Continuous Integration Tools

Page 55: Continuous Delivery - Automate & Build Better Software with Travis CI

PHPCI - Reporting

Page 56: Continuous Delivery - Automate & Build Better Software with Travis CI

• Project is small, budget is small…• Evaluate which tools are worthwhile to your

specific project.

Yes, But…

Page 57: Continuous Delivery - Automate & Build Better Software with Travis CI

• Consider including unit tests or code cost/coverage reports in your deliverables to your customers as an added value to them (and you down the road).

Make It a Deliverable

Page 58: Continuous Delivery - Automate & Build Better Software with Travis CI

Project:

A customer hires you to create a registration form for a one-time event. It’s a small customer with a small budget. It should take a couple hundred lines of code in a single file, results will be e-mailed. It will be tested by the event staff and the marketing department on the live site as they do not have a test environment, and it will only be live for two months.

Page 59: Continuous Delivery - Automate & Build Better Software with Travis CI

What they need:1. If they do not have an in-house standard for

you to follow, write it using one of the main coding standards, like PEAR.

2. Create unit tests for it.

What they don’t need:3. In-depth reporting4. Full automation, including build.5. Documentation

Page 60: Continuous Delivery - Automate & Build Better Software with Travis CI

Project:

A customer hires you for an ongoing project. On the 15th of every month, they need you to go in and add a new survey to collect data and write it to a database. The previous month’s survey data needs to be backed up and cleared out of the database when the new survey goes live.

Page 61: Continuous Delivery - Automate & Build Better Software with Travis CI

What they need:1. If they do not have an in-house standard for

you to follow, write it using one of the main coding standards, like PEAR.

2. Create unit tests for it and use a testing framework.

3. Automate the build.

What they don’t need:4. In-depth reporting (Maybe)5. Documentation (Maybe)

Page 62: Continuous Delivery - Automate & Build Better Software with Travis CI

Project:

A customer hires you to write one part of a very large application. Other consultants that you do not have access to will be working on other parts of the application at the same time.

Page 63: Continuous Delivery - Automate & Build Better Software with Travis CI

What they need:1. All of it

In this situation, see if you can convince them to get everyone working on a unified continuous integration platform utilizing a complete suite of continuous integration tools from standards to documentation and fully automated everywhere in between.

Page 64: Continuous Delivery - Automate & Build Better Software with Travis CI

Not everything is beneficial enough to use in every

situation, so choose the right tools for your project and

needs.

Take Away #1

Page 65: Continuous Delivery - Automate & Build Better Software with Travis CI

The fewer steps I have to remember to do manually, the

more successful my project will be.

Take Away #2

Page 66: Continuous Delivery - Automate & Build Better Software with Travis CI

release != deployment

No builds. No change. No upgrades. Is it read-only Day?

NAH! NOT NECESSARILY

Take Away #3

Page 67: Continuous Delivery - Automate & Build Better Software with Travis CI

[featureToggles] wobblyFoobars: true flightyForkHandles:

false

Config File

<?if ($wobblyFoobars) {?>... various UI elements

<?}?>

some.php

$fork_handle = ($featureConfig->isOn(‘flightlyForkHandles‘)) ? new flightyForkHander(aCandle) :

new forkHandler(aCandle);

other.php

Stolen from Martin Fowler http://martinfowler.com/bliki/FeatureToggle.html

Feature Toggles

Page 68: Continuous Delivery - Automate & Build Better Software with Travis CI

https://github.com/waqar-alamgir/learning-travis

Travis Demo

Page 69: Continuous Delivery - Automate & Build Better Software with Travis CI

Resources• CruiseControl - http://cruisecontrol.sourceforge.net

• Guide to writing your own PHP_CodeSniffer standards (Official) - http://pear.php.net/manual/en/package.php.php-codesniffer.coding-standard-tutorial.php

• Guide to writing your own PHP_CodeSniffer standards (Alternate) - http://luhman.org/blog/2009/12/17/setting- custom-coding-standards-php-codesniffer

• Hudson - http://hudson-ci.org• Jenkins - http://jenkins-ci.org• Maven - http://www.php-maven.org• PEAR coding standard - http://pear.php.net/manual/en/standards.php• PEAR Package Manager Installation - http://pear.php.net/manual/en/installation.php• PEAR Packages Installation - http://pear.php.net/manual/en/guide.users.commandline.installing.php• PEAR2 coding standard - http://pear.php.net/manual/en/pear2cs.rules.php• Phing - http://phing.info• PHP Standards Working Group - http://groups.google.com/group/php-standards• PHP_CodeSniffer - http://pear.php.net/package/PHP_CodeSniffer• phpDocumentor 2 - http://www.phpdoc.org/• PHPUnit - http://www.phpunit.de/manual/3.6/en/automating-tests.html• phpUnderControl - http://phpundercontrol.org• Selenium - http://seleniumhq.org/• SimpleTest - http://www.simpletest.org• Sonar - http://www.sonarsource.org• Sonar PHP Plug-in - http://docs.codehaus.org/display/SONAR/PHP+Plugin• Sonar Technical Debt Plugin - http://docs.codehaus.org/display/SONAR/Technical+Debt+Plugin• Template for Jenkins Jobs for PHP Projects by Sebastian Bergmann - http://jenkins-php.org

Page 70: Continuous Delivery - Automate & Build Better Software with Travis CI

QA

Please send your Feedback at@wajrcs / [email protected]

Thank Y u