multi-environment git deployment with codeigniter - cicon2011

20
Multi-Environment Git Deployment with CodeIgniter -Ben Edmunds

Upload: codeigniter-conference

Post on 29-Jan-2018

4.984 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Multi-Environment git Deployment with CodeIgniter - CICON2011

Multi-Environment Git Deployment with CodeIgniter-Ben Edmunds

Page 2: Multi-Environment git Deployment with CodeIgniter - CICON2011

• What is Git?

• Distributed Version Control

• Why use version control?

• Change tracking and roll-back

• Multi-developer projects

• Deployment

Page 3: Multi-Environment git Deployment with CodeIgniter - CICON2011

• Why use Git?

• Branching

• Feature branches

• Environment deployment with branches

Page 4: Multi-Environment git Deployment with CodeIgniter - CICON2011

• What is Deployment?

• Automated update and setup of code

• Why use deployments?

• FTP = nasty

• Humans suck

• Ease of use

Page 5: Multi-Environment git Deployment with CodeIgniter - CICON2011

Local Staging Production(Development) (QA) (Live)

Basic Deployment Scenario

Page 6: Multi-Environment git Deployment with CodeIgniter - CICON2011

Local Development Setup

$ git init$ git remote add origin yourRepo.git$ git pull origin master

$ git checkout -b staging$ git push origin staging

$ git checkout -b production$ git push origin master

Page 7: Multi-Environment git Deployment with CodeIgniter - CICON2011

Branch Setup

$ git checkout -b staging$ git push origin staging

$ git checkout -b production$ git push origin master

Page 8: Multi-Environment git Deployment with CodeIgniter - CICON2011

Deployment Code

<?php

define('ENV', 'production');define('ENV_BASE_PATH', '/home/benedmunds/domains/benedmunds.com/');

class Deployment extends CI_Controller{

function post_receive() { ... } }

?>

Page 9: Multi-Environment git Deployment with CodeIgniter - CICON2011

Deployment Code

post_receive() { ... }

• json_decode($_POST[‘payload’])

• Check payload ref against ENV

• Execute Git commands to clean and pull the correct branch

Page 10: Multi-Environment git Deployment with CodeIgniter - CICON2011

Deployment Codefunction post_receive(){ if (isset($_POST['payload']) && !empty($_POST['payload'])) { $payload = json_decode($_POST['payload']); //make sure this is the correct environment if ($payload->ref == 'refs/heads/' . ENV) { log_message('debug', 'DEPLOYMENT: Post-receive hook - '. ENV); //reset, clean and pull shell_exec('/usr/bin/git --git-dir="' . ENV_BASE_PATH . '.git" ‘ . ‘--work-tree="' . ENV_BASE_PATH . '" ‘ . ‘reset --hard HEAD'); shell_exec('/usr/bin/git --git-dir="' . ENV_BASE_PATH . '.git" ‘ . ‘--work-tree="' . ENV_BASE_PATH . '" clean -f'); shell_exec('/usr/bin/git --git-dir="' . ENV_BASE_PATH . '.git" ‘ . ‘--work-tree="' . ENV_BASE_PATH . '" ‘ . ‘pull origin ' . ENV); } } }

Page 11: Multi-Environment git Deployment with CodeIgniter - CICON2011

class Migration_Create_accounts extends Migration { function up() { $this->migrations->verbose AND print "Creating table accounts...";

if ( ! $this->db->table_exists('accounts')) { // Setup Keys $this->dbforge->add_key('id', TRUE);

$this->dbforge->add_field(array( 'id' => array('type' => 'INT', 'constraint' => 5, 'unsigned' => TRUE, 'auto_increment' => TRUE), 'company_name' => array('type' => 'VARCHAR', 'constraint' => '200', 'null' => FALSE), 'first_name' => array('type' => 'VARCHAR', 'constraint' => '200', 'null' => FALSE), 'last_name' => array('type' => 'VARCHAR', 'constraint' => '200', 'null' => FALSE), 'phone' => array('type' => 'TEXT', 'null' => FALSE), 'email' => array('type' => 'TEXT', 'null' => FALSE), 'address' => array('type' => 'TEXT', 'null' => FALSE), 'Last_Update' => array('type' => 'DATETIME', 'null' => FALSE) ));

$this->dbforge->add_field("Created_At TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP"); $this->dbforge->create_table('accounts', TRUE); } }

function down() { $this->dbforge->drop_table('accounts'); }}

Migrationshttps://github.com/philsturgeon/codeigniter-migrations

Page 12: Multi-Environment git Deployment with CodeIgniter - CICON2011

if ( ! $this->migration->latest()){ show_error($this->migration->error);}

Migrationshttps://github.com/philsturgeon/codeigniter-migrations

Page 13: Multi-Environment git Deployment with CodeIgniter - CICON2011

Staging Server Setup

$ git init$ git remote add origin yourRepo.git$ git pull origin staging

Page 14: Multi-Environment git Deployment with CodeIgniter - CICON2011

Production Server Setup

$ git init$ git remote add origin yourRepo.git$ git pull origin production

Page 15: Multi-Environment git Deployment with CodeIgniter - CICON2011

Github Post Receive Hook

Page 16: Multi-Environment git Deployment with CodeIgniter - CICON2011

Pushing to Master

$git checkout master$ git commit -am ‘Made code change’$ git push origin master

Local Github

Page 17: Multi-Environment git Deployment with CodeIgniter - CICON2011

Pushing to Staging

$ git checkout staging$ git merge master$ git push origin staging

Local Github Staging

Page 18: Multi-Environment git Deployment with CodeIgniter - CICON2011

Pushing to Production

$ git checkout production$ git merge staging$ git push origin production

Local Github Production

Page 19: Multi-Environment git Deployment with CodeIgniter - CICON2011

Local Staging Production(Development) (QA) (Live)

Page 20: Multi-Environment git Deployment with CodeIgniter - CICON2011

Multi-Environment Git Deployment with CodeIgniter-Ben Edmunds