Multi-Environment git Deployment with CodeIgniter - CICON2011

4,955 views

Published on

Published in: Technology, Business
0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,955
On SlideShare
0
From Embeds
0
Number of Embeds
362
Actions
Shares
0
Downloads
74
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Multi-Environment git Deployment with CodeIgniter - CICON2011

    1. 1. Multi-Environment Git Deployment with CodeIgniter -Ben Edmunds
    2. 2. • What is Git? • Distributed Version Control• Why use version control? • Change tracking and roll-back • Multi-developer projects • Deployment
    3. 3. • Why use Git? • Branching • Feature branches • Environment deployment with branches
    4. 4. • What is Deployment? • Automated update and setup of code• Why use deployments? • FTP = nasty • Humans suck • Ease of use
    5. 5. Basic Deployment Scenario Local Staging Production(Development) (QA) (Live)
    6. 6. 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
    7. 7. Branch Setup$ git checkout -b staging$ git push origin staging$ git checkout -b production$ git push origin master
    8. 8. Deployment Code<?phpdefine(ENV, production);define(ENV_BASE_PATH, /home/benedmunds/domains/benedmunds.com/);class Deployment extends CI_Controller{ function post_receive() { ... }}?>
    9. 9. Deployment Codepost_receive() { ... } • json_decode($_POST[‘payload’]) • Check payload ref against ENV • Execute Git commands to clean and pull the correct branch
    10. 10. function post_receive() Deployment Code{ 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); } } }
    11. 11. Migrationshttps://github.com/philsturgeon/codeigniter-migrationsclass 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); }}
    12. 12. Migrationshttps://github.com/philsturgeon/codeigniter-migrations if ( ! $this->migration->latest()) { show_error($this->migration->error); }
    13. 13. Staging Server Setup$ git init$ git remote add origin yourRepo.git$ git pull origin staging
    14. 14. Production Server Setup$ git init$ git remote add origin yourRepo.git$ git pull origin production
    15. 15. Github Post Receive Hook
    16. 16. Pushing to Master$git checkout master$ git commit -am ‘Made code change’$ git push origin master Local Github
    17. 17. Pushing to Staging $ git checkout staging $ git merge master $ git push origin stagingLocal Github Staging
    18. 18. Pushing to Production $ git checkout production $ git merge staging $ git push origin productionLocal Github Production
    19. 19. Local Staging Production(Development) (QA) (Live)
    20. 20. Multi-Environment Git Deployment with CodeIgniter -Ben Edmunds

    ×