41
BUILDING AND DEPLOYING PHP APPLICATIONS MARTINS SIPENKO

Building and Deploying PHP Applications, PHPTour 2016

Embed Size (px)

Citation preview

Page 1: Building and Deploying PHP Applications, PHPTour 2016

BUILDING AND DEPLOYING PHP APPLICATIONS

MARTINS SIPENKO

Page 2: Building and Deploying PHP Applications, PHPTour 2016

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

Page 3: Building and Deploying PHP Applications, PHPTour 2016

BUILDING AND DEPLOYING PHP APPLICATIONS

@MARTINSSIPENKO

Page 4: Building and Deploying PHP Applications, PHPTour 2016

BUILDING AND DEPLOYING PHP APPLICATIONS

SOME TERMINOLOGY

Page 5: Building and Deploying PHP Applications, PHPTour 2016

BUILDING AND DEPLOYING PHP APPLICATIONS

BUILD (VERB) PROCESS

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

Page 6: Building and Deploying PHP Applications, PHPTour 2016

BUILDING AND DEPLOYING PHP APPLICATIONS

BUILD (NOUN)

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

Page 7: Building and Deploying PHP Applications, PHPTour 2016

BUILDING AND DEPLOYING PHP APPLICATIONS

DEPLOYMENT (VERB)

▸ Process of putting (deploying) the BUILD to servers

QA

Staging

Production

Page 8: Building and Deploying PHP Applications, PHPTour 2016

BUILDING AND DEPLOYING PHP APPLICATIONS

THE BUILD PROCESS

Page 9: Building and Deploying PHP Applications, PHPTour 2016

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

Page 10: Building and Deploying PHP Applications, PHPTour 2016

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

Page 11: Building and Deploying PHP Applications, PHPTour 2016

BUILDING AND DEPLOYING PHP APPLICATIONS

WHAT HAPPENS DURING THE BUILD PROCESS?

Page 12: Building and Deploying PHP Applications, PHPTour 2016

BUILDING AND DEPLOYING PHP APPLICATIONS

1. INSTALL PROJECT DEPENDANCIES

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

npm install

bower install

Page 13: Building and Deploying PHP Applications, PHPTour 2016

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

Page 14: Building and Deploying PHP Applications, PHPTour 2016

BUILDING AND DEPLOYING PHP APPLICATIONS

3. TESTING

▸ Run unit testsphpunit

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

▸ Testing frameworks can produce code coverage reports

Page 15: Building and Deploying PHP Applications, PHPTour 2016

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...

Page 16: Building and Deploying PHP Applications, PHPTour 2016

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/

▸ ...

Page 17: Building and Deploying PHP Applications, PHPTour 2016

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 .

Page 18: Building and Deploying PHP Applications, PHPTour 2016

BUILDING AND DEPLOYING PHP APPLICATIONS

6. STORE BUILD PACKAGE

▸ Upload the build package to some storage

▸ AWS S3

▸ Dropbox

▸ NFS

▸ ...

Page 19: Building and Deploying PHP Applications, PHPTour 2016

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

Page 20: Building and Deploying PHP Applications, PHPTour 2016

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

Page 21: Building and Deploying PHP Applications, PHPTour 2016

BUILDING AND DEPLOYING PHP APPLICATIONS

DEPLOYING THE BUILD

Page 22: Building and Deploying PHP Applications, PHPTour 2016

BUILDING AND DEPLOYING PHP APPLICATIONS

HOW DO YOU DEPLOY?

Page 23: Building and Deploying PHP Applications, PHPTour 2016

BUILDING AND DEPLOYING PHP APPLICATIONS

HOW TO DEPLOY?

▸ FTP

▸ SSH

▸ CRON

▸ 3rd party services

▸ custom solution

Page 24: Building and Deploying PHP Applications, PHPTour 2016
Page 25: Building and Deploying PHP Applications, PHPTour 2016

BUILDING AND DEPLOYING PHP APPLICATIONS

HOW TO DEPLOY?

▸ FTP

▸ SSH

▸ CRON

▸ Deployment Agent

Page 26: Building and Deploying PHP Applications, PHPTour 2016

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

Page 27: Building and Deploying PHP Applications, PHPTour 2016

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

Page 28: Building and Deploying PHP Applications, PHPTour 2016

BUILDING AND DEPLOYING PHP APPLICATIONS

DEPLOYMENT USING CRON

Pros:

▸ Fast

▸ Works well for small sites

▸ Easy to implement

Cons:

▸ No rolling deployment

Page 29: Building and Deploying PHP Applications, PHPTour 2016

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

Page 30: Building and Deploying PHP Applications, PHPTour 2016

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

Page 31: Building and Deploying PHP Applications, PHPTour 2016

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

Page 32: Building and Deploying PHP Applications, PHPTour 2016

BUILDING AND DEPLOYING PHP APPLICATIONS

OTHER OPTIONS

Page 33: Building and Deploying PHP Applications, PHPTour 2016

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

Page 34: Building and Deploying PHP Applications, PHPTour 2016

BUILDING AND DEPLOYING PHP APPLICATIONS

SOME TOOLS

Page 35: Building and Deploying PHP Applications, PHPTour 2016

BUILDING AND DEPLOYING PHP APPLICATIONS

CONTINUOUS INTEGRATION (CI)

▸ Hosted (SaaS)

▸ TravisCI

▸ CircleCI

▸ CodeShip

▸ ...

▸ Self hosted

▸ Jenkins

▸ Bamboo

▸ ...

Page 36: Building and Deploying PHP Applications, PHPTour 2016

BUILDING AND DEPLOYING PHP APPLICATIONS

CODE DEPLOYMENT

▸ 3rd party

▸ AWS CodeDeploy

▸ CodeShip

▸ ...

▸ Capistrano

▸ Fabric

▸ Custom

Page 37: Building and Deploying PHP Applications, PHPTour 2016

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

Page 38: Building and Deploying PHP Applications, PHPTour 2016

BUILDING AND DEPLOYING PHP APPLICATIONS

QUICK TIP 5:

READ THIS BOOK!CONTINUOUS DELIVERY Jez HumbleDavid Farley

Page 39: Building and Deploying PHP Applications, PHPTour 2016

BUILDING AND DEPLOYING PHP APPLICATIONS

Q&A?

Page 40: Building and Deploying PHP Applications, PHPTour 2016

BUILDING AND DEPLOYING PHP APPLICATIONS

MERCI!

https://joind.in/talk/[email protected]@gmail.com

Page 41: Building and Deploying PHP Applications, PHPTour 2016

BUILDING AND DEPLOYING PHP APPLICATIONS

PLEASE GIVE FEEDBACK

https://joind.in/talk/[email protected]@gmail.com