Building and Deploying PHP Applications, PHPTour 2016

Preview:

Citation preview

BUILDING AND DEPLOYING PHP APPLICATIONS

MARTINS SIPENKO

BUILDING AND DEPLOYING PHP APPLICATIONS

ABOUT ME

▸ Located in Riga, Latvia

▸ Worked with IT since around 2002

▸ Lead engineer @KASKO, a fintech startup

▸ Student at University of Latvia

▸ AWS Certified

BUILDING AND DEPLOYING PHP APPLICATIONS

@MARTINSSIPENKO

BUILDING AND DEPLOYING PHP APPLICATIONS

SOME TERMINOLOGY

BUILDING AND DEPLOYING PHP APPLICATIONS

BUILD (VERB) PROCESS

▸ Transform which converts a code repo into an "executable" bundle known as a build

BUILDING AND DEPLOYING PHP APPLICATIONS

BUILD (NOUN)

▸ An artifact which is being produced during the build process that includes source code and compiled assets

BUILDING AND DEPLOYING PHP APPLICATIONS

DEPLOYMENT (VERB)

▸ Process of putting (deploying) the BUILD to servers

QA

Staging

Production

BUILDING AND DEPLOYING PHP APPLICATIONS

THE BUILD PROCESS

BUILDING AND DEPLOYING PHP APPLICATIONS

THE BUILD PROCESS

▸ Keep source code in version control

▸ Automatically generated things never are committed to version controlvendor/, node_modules/, generated JavaScript, CSS, documentation, etc..

▸ Continuous Integration configured to build each commit that is pushed to GitHub

BUILDING AND DEPLOYING PHP APPLICATIONS

QUICK TIP 1:

▸ Your project is kept in a known good state

▸ All developers operate on the same code-base

▸ Tests do not suddenly fail because of changes in one of its dependencies

ALWAYS COMMIT YOUR COMPOSER.LOCK FILE INTO YOUR SOURCE CODE REPOSITORY

BUILDING AND DEPLOYING PHP APPLICATIONS

WHAT HAPPENS DURING THE BUILD PROCESS?

BUILDING AND DEPLOYING PHP APPLICATIONS

1. INSTALL PROJECT DEPENDANCIES

composer install --optimize-autoloader --prefer-dist --no-interaction

npm install

bower install

BUILDING AND DEPLOYING PHP APPLICATIONS

2. STATIC CODE ANALYSIS

▸ codesniffer - coding standards

▸ phpmd - potential code issue detection

▸ phpcpd - duplicate code detection

▸ phploc - measure size of your project

BUILDING AND DEPLOYING PHP APPLICATIONS

3. TESTING

▸ Run unit testsphpunit

▸ Run "local" integration/acceptance testsphpunit, codeception, behat

▸ Testing frameworks can produce code coverage reports

BUILDING AND DEPLOYING PHP APPLICATIONS

4. COMPILE

▸ Bundle and minify JavaScript

▸ LESS/SASS -> CSS

▸ Optimize images

▸ Generate fonts from SVG

▸ Cache bust your static assets

▸ And many more...

BUILDING AND DEPLOYING PHP APPLICATIONS

5. CREATE BUILD PACKAGE

▸ a .tar.gz or .zip archive

▸ Only include things you need to run your application

▸ Exclude:

▸ things that were transformed into compiled assets (LESS, SASS, Coffee)

▸ .git

▸ node_modules/

▸ tests/

▸ ...

BUILDING AND DEPLOYING PHP APPLICATIONS

5. CREATE BUILD PACKAGE

$ rsync -a \ --exclude .git/ \ --exclude ./build/ \ --exclude node_modules/ \ --exclude tests/ \ --exclude vendor/phpunit/ \ --exclude .travis.yml \ --exclude .gitignore \ --exclude .env.example \ . /tmp/build-$BUILD_NUMBER

$ cd /tmp/build-$BUILD_NUMBER

$ tar -zcf /tmp/$REPO_NAME-$BUILD_NUMBER.tar.gz .

BUILDING AND DEPLOYING PHP APPLICATIONS

6. STORE BUILD PACKAGE

▸ Upload the build package to some storage

▸ AWS S3

▸ Dropbox

▸ NFS

▸ ...

BUILDING AND DEPLOYING PHP APPLICATIONS

QUICK TIP 2:

▸ Know which exact commit you have in the build

▸ Handy when you know the build number that is currently deployed to some environment

▸ You can check out and debug the code that was in particular build

TAG THE COMMIT WITH THE BUILD NUMBER AND PUSH THAT TAG TO GITHUB

BUILDING AND DEPLOYING PHP APPLICATIONS

OUTCOME: BUILD PACKAGE

▸ Can be deployed to any environment

▸ Does not change as you move it around

▸ Does not contain unnecessary assets

▸ Easy to download, and install

BUILDING AND DEPLOYING PHP APPLICATIONS

DEPLOYING THE BUILD

BUILDING AND DEPLOYING PHP APPLICATIONS

HOW DO YOU DEPLOY?

BUILDING AND DEPLOYING PHP APPLICATIONS

HOW TO DEPLOY?

▸ FTP

▸ SSH

▸ CRON

▸ 3rd party services

▸ custom solution

BUILDING AND DEPLOYING PHP APPLICATIONS

HOW TO DEPLOY?

▸ FTP

▸ SSH

▸ CRON

▸ Deployment Agent

BUILDING AND DEPLOYING PHP APPLICATIONS

DEPLOYMENT USING SSH

Pros:

▸ Easy to implement

▸ Rolling deployment possible

Cons:

▸ Needs access to server via SSH

▸ Needs to know location of each instance

BUILDING AND DEPLOYING PHP APPLICATIONS

DEPLOYMENT USING CRON

▸ CRON task that runs script every minute

▸ Script checks versions file stored somewhere (could also be S3)

▸ If required version differs from actual:

▸ download build package

▸ extract

▸ run post deployment hooks

▸ swap symlinks

BUILDING AND DEPLOYING PHP APPLICATIONS

DEPLOYMENT USING CRON

Pros:

▸ Fast

▸ Works well for small sites

▸ Easy to implement

Cons:

▸ No rolling deployment

BUILDING AND DEPLOYING PHP APPLICATIONS

DEPLOYMENT USING AGENT

▸ Agent runs on every instance

▸ Agent announces itself to centralized deployment tool

▸ Deployment tool orchestrates code deployments to every instance

BUILDING AND DEPLOYING PHP APPLICATIONS

DEPLOYMENT USING AGENT

Pros:

▸ Deployment does not require access via SSH

▸ Deployments are orchestrated so many strategies can be used: one by one, 50/50, all

Cons:

▸ Hard to implement

BUILDING AND DEPLOYING PHP APPLICATIONS

QUICK TIP 3:

▸ Humans make mistakes

▸ Works the same on each run

▸ Deployment scripts can be considered as documentation

▸ Less human time spent doing manual labor

AUTOMATE YOUR DEPLOYMENT PROCESS TO MAXIMUM

BUILDING AND DEPLOYING PHP APPLICATIONS

OTHER OPTIONS

BUILDING AND DEPLOYING PHP APPLICATIONS

OTHER OPTIONS

▸ Build VM Image (AMI) with code and software to run it

▸ Pro: Very robust,

▸ Con: but quite slow process

▸ Build Docker images

▸ Pro: Much faster and smaller than VM Images

▸ Con: Not really clear how to do automated deployments

BUILDING AND DEPLOYING PHP APPLICATIONS

SOME TOOLS

BUILDING AND DEPLOYING PHP APPLICATIONS

CONTINUOUS INTEGRATION (CI)

▸ Hosted (SaaS)

▸ TravisCI

▸ CircleCI

▸ CodeShip

▸ ...

▸ Self hosted

▸ Jenkins

▸ Bamboo

▸ ...

BUILDING AND DEPLOYING PHP APPLICATIONS

CODE DEPLOYMENT

▸ 3rd party

▸ AWS CodeDeploy

▸ CodeShip

▸ ...

▸ Capistrano

▸ Fabric

▸ Custom

BUILDING AND DEPLOYING PHP APPLICATIONS

QUICK TIP 4:

▸ Application should use environment variables for configuration

▸ Values of env variables are different for in environment

▸ Env vars are easy to change between deploys without changing any code

DO NOT PUT ANY CONFIG IN GIT OR BUILD PACKAGE

BUILDING AND DEPLOYING PHP APPLICATIONS

QUICK TIP 5:

READ THIS BOOK!CONTINUOUS DELIVERY Jez HumbleDavid Farley

BUILDING AND DEPLOYING PHP APPLICATIONS

Q&A?

BUILDING AND DEPLOYING PHP APPLICATIONS

MERCI!

https://joind.in/talk/ad2c5@martinssipenkomartins.sipenko@gmail.com

BUILDING AND DEPLOYING PHP APPLICATIONS

PLEASE GIVE FEEDBACK

https://joind.in/talk/ad2c5@martinssipenkomartins.sipenko@gmail.com

Recommended