Continuous Integration

Is not a rocket science

slides.com/lbajsarowicz/continuous-integration

Łukasz Bajsarowicz

Magento Developer / Trainer

© 2018 Magento. An Adobe Company

#MageConf18

@lbajsarowicz

© 2018 Magento. An Adobe Company

#MageConf18

Continuous Integration

What actually is

Background: Audi AG Media Center, https://www.audi-mediacenter.com/en/production-232

@lbajsarowicz

What it is not?

Develop

Build

Test

Release

Deploy

Continuous Development

Continuous Integration

Continuous Delivery

Continuous Deployment

© 2018 Magento. An Adobe Company

#MageConf18

@lbajsarowicz

How it works?

Version Control System

Continuous Integration

Run tests

Build environment

Manual

Automatic

Code Change

Trigger

Feedback

Maintainer

Developers

Review & Merge

Information about errors

© 2018 Magento. An Adobe Company

#MageConf18

@lbajsarowicz

Available solutions

Getting on with Continuous Integration with ease

© 2018 Magento. An Adobe Company

#MageConf18

@lbajsarowicz

© 2018 Magento. An Adobe Company

#MageConf18

@lbajsarowicz

+ Cloud-based system

+ Free plan

+ SSH debug

Out of the box integration:

Based on:

© 2018 Magento. An Adobe Company

#MageConf18

@lbajsarowicz

Getting Started

© 2018 Magento. An Adobe Company

#MageConf18

@lbajsarowicz

© 2018 Magento. An Adobe Company

#MageConf18

Code Repository

CircleCI settings:

- .circleci/ directory

Your own code:

- app/

Tests and it's configuration:
- dev/

@lbajsarowicz

CircleCI setup

.circleci/config.yml

At least in theory...

version: 2
jobs:
  build:
    docker:
      - image: php:7.1
      - image: mysql:5.7
        environment:
          MYSQL_USER: magento
          MYSQL_PASSWORD: magento
          MYSQL_DATABASE: magento
          MYSQL_RANDOM_ROOT_PASSWORD: yes
    working_directory: ~/magento
    steps:
      - checkout
      - run: composer install -n --prefer-dist

© 2018 Magento. An Adobe Company

#MageConf18

@lbajsarowicz

CircleCI setup

.circleci/config.yml

At least in theory...

© 2018 Magento. An Adobe Company

#MageConf18

@lbajsarowicz

Customize Docker

FROM circleci/php:7.1.23-fpm-stretch-node-browsers

USER root

# Install common extensions
RUN apt install -y zlib1g-dev libfreetype6-dev libjpeg62-turbo-dev \
    libpng-dev libjpeg-dev libmcrypt-dev libxslt1-dev zip libxml2-dev
RUN docker-php-ext-configure gd --with-png-dir=/usr --with-jpeg-dir=/usr \ 
    && docker-php-ext-install gd
RUN docker-php-ext-install bcmath mbstring mcrypt opcache pdo_mysql soap xsl

# Install `mysql` and `mysqldump` commands
RUN apt install -y mysql-client

USER circleci

CMD ["/bin/sh"]

© 2018 Magento. An Adobe Company

#MageConf18

@lbajsarowicz

Customize Docker

You need your own Docker repository at DockerHub

version: 2
jobs:
  build:
    docker:
      - image: m2coach/continuousintegrationdocker
      - image: mysql:5.7
...
.circleci/config.yml

© 2018 Magento. An Adobe Company

#MageConf18

@lbajsarowicz

First Success!

© 2018 Magento. An Adobe Company

#MageConf18

@lbajsarowicz

What we have?

© 2018 Magento. An Adobe Company

#MageConf18

@lbajsarowicz

Perform Unit Tests

.circleci/config.yml
...
    - run: vendor/bin/phpunit -c dev/tests/unit/phpunit.xml
...
dev/tests/unit/phpunit.xml
...
    <testsuite name="Application Unit Tests">
        <directory suffix="Test.php">../../../app/code/*/*/Test/Unit</directory>
    </testsuite>
...
    <filter>
        <whitelist addUncoveredFilesFromWhiteList="true">
            <directory suffix=".php">../../../app/code/*</directory>
            <exclude>
                <directory>../../../app/code/*/*/Test</directory>
            </exclude>
        </whitelist>
    </filter>
...

© 2018 Magento. An Adobe Company

#MageConf18

@lbajsarowicz

Another goal!

© 2018 Magento. An Adobe Company

#MageConf18

@lbajsarowicz

Run static tests

.circleci/config.yml
...
    - run: vendor/bin/phpunit -c dev/tests/static/phpunit.xml
...
dev/tests/static/phpunit.xml
...
    <testsuites>
        <testsuite name="Less Static Code Analysis">
            <file>testsuite/Magento/Test/Less/LiveCodeTest.php</file>
        </testsuite>
        <testsuite name="PHP Coding Standard Verification">
            <file>testsuite/Magento/Test/Php/LiveCodeTest.php</file>
        </testsuite>
        <testsuite name="Code Integrity Tests">
            <directory>testsuite/Magento/Test/Integrity</directory>
        </testsuite>
        <testsuite name="Xss Unsafe Output Test">
            <file>testsuite/Magento/Test/Php/XssPhtmlTemplateTest.php</file>
        </testsuite>
    </testsuites>
...

© 2018 Magento. An Adobe Company

#MageConf18

@lbajsarowicz

Run static tests

© 2018 Magento. An Adobe Company

#MageConf18

@lbajsarowicz

Run static tests

.circleci/config.yml
...

      - run:
          name: PHPUnit / Static Tests / Coding Standard Verification
          command: vendor/bin/phpunit -c dev/tests/static/phpunit.xml \
            --testsuite "PHP Coding Standard Verification" || exit 0
      - run:
          name: PHPUnit / Static Tests / Xss Unsafe Output Test
          command: vendor/bin/phpunit -c dev/tests/static/phpunit.xml \
            --testsuite "Xss Unsafe Output Test" || exit 0
      - run:
          name: PHPUnit / Static Tests / Less Static Code Analysis
          command: vendor/bin/phpunit -c dev/tests/static/phpunit.xml \
            --testsuite "Less Static Code Analysis" || exit 0
...

© 2018 Magento. An Adobe Company

#MageConf18

@lbajsarowicz

© 2018 Magento. An Adobe Company

#MageConf18

You should know!

If you run out of memory, add to job:

-d memory_limit=1024M

You can name your tasks:

-run:
  name: My Custom task name
  command: vendor/bin/phpunit

© 2018 Magento. An Adobe Company

#MageConf18

Use cache!

  - restore_cache:
    keys:
      - v2-dependencies-{{ checksum "composer.json" }}
  - save_cache:
      paths:
        - ~/.composer/cache/
      key: v2-dependencies-{{ checksum "composer.json" }}

Try to use existing cache:

Save newly pulled dependencies:

© 2018 Magento. An Adobe Company

#MageConf18

Pull Requests

© 2018 Magento. An Adobe Company

#MageConf18

Extra checks

extdn/extdn-phpcs

.circleci/config.yml

-run:
  name: ExtDN CodeSniffer
  command: vendor/bin/phpcs --standard=./vendor/extdn/phpcs/Extdn app/code/

.circleci/config.yml

-run:
  name: Static Content Deploy
  command: bin/magento setup:static-content:deploy

Testing the release

© 2018 Magento. An Adobe Company

#MageConf18

@lbajsarowicz

Set up Magento

  - run:
      name: Magento / Setup / Install
      shell: /bin/sh
      command: |
        bin/magento setup:install \
        --admin-firstname='FirstName' \
        --admin-lastname='LastName' \
        --admin-email='mail@u-2.pl' \
        --admin-user='admin' \
        --admin-password='Test123!' \
        --db-host='127.0.0.1' \
        --db-name='magento' \
        --db-user='magento' \
        --db-password='magento' \
        --backend-frontname='admin'
.circleci/config.yml

© 2018 Magento. An Adobe Company

#MageConf18

@lbajsarowicz

Run integration tests

.circleci/config.yml
...
      - run:
          name: PHPUnit / Integration Tests
          command: vendor/bin/phpunit -c ~/magento/dev/tests/integration/phpunit.xml
...

© 2018 Magento. An Adobe Company

#MageConf18

@lbajsarowicz

dev/tests/integration/phpunit.xml
...
    <php>
        <ini name="memory_limit" value="-1"/>
...

© 2018 Magento. An Adobe Company

#MageConf18

@lbajsarowicz

Background: Amasty Blog --- https://amasty.com/blog/magento-wallpapers-pack-amasty/

... and take another cup of coffee

Acceptance Testing

© 2018 Magento. An Adobe Company

#MageConf18

@lbajsarowicz

.circleci/config.yml
...
      - run:
          name: Acceptance Tests / Setup
          command: cd ~/magento/dev/tests/acceptance && composer install
      - run:
          name: Acceptance Tests / Running Tests
          command: cd ~/magento/dev/tests/acceptance && vendor/bin/robo functional
...
dev/tests/acceptance/.env

#*** Set the base URL for your Magento instance ***#
MAGENTO_BASE_URL=http://127.0.0.1:3000/

#*** Set the Admin Username and Password for your Magento instance ***#
MAGENTO_BACKEND_NAME=backend
MAGENTO_ADMIN_USERNAME=m2coach
MAGENTO_ADMIN_PASSWORD=Test123!

Acceptance Testing

© 2018 Magento. An Adobe Company

#MageConf18

@lbajsarowicz

Not so easy!

Hope to be able to write and run
own acceptance tests
with Magento 2.2.7 or 2.3

Questions?

lbajsarowicz

/in/lbajsarowicz

Thank you!

© 2018 Magento. An Adobe Company

#MageConf18

lbajsarowicz

Continuous Integration is not a rocket science!

By Łukasz Bajsarowicz

Continuous Integration is not a rocket science!

  • 221