Steps to better manage and deploy code

Matteo Magni
https://magni.me
The Joel test : 12 Steps to Better Code
  1. Do you use source control?
  2. Can you make a build in one step?
  3. Do you make daily builds?
  4. ...
Version control, also known as revision control or source control,[1] is the management of changes to documents, computer programs, large web sites, and other collections of information.
Changes are usually identified by a number or letter code, termed the "revision number", "revision level", or simply "revision"
For example, an initial set of files is "revision 1". When the first change is made, the resulting set is "revision 2", and so on. Each revision is associated with a timestamp and the person making the change. Revisions can be compared, restored, and with some types of files, merged.

Git

https://git-scm.com/
Git was created by Linus Torvalds in 2005 for development of the Linux kernel
Torvalds sarcastically quipped about the name git (which means unpleasant person in British English slang):
"I'm an egotistical bastard, and I name all my projects after myself.
First 'Linux', now 'git'."
The man page describes Git as "the stupid content tracker".
https://rogerdudler.github.io/git-guide/
https://ohshitgit.com/

Git as a service

Build

That is, the construction of something that has an observable and tangible result
The term build may refer to the process by which source code is converted into a stand-alone form that can be run on a computer or to the form itself.

repetitive tasks

  • Install dependencies
  • Compile
  • Minify code
  • Run tests
  • ...

Install dependencies

don't versioning the code of others

Composer

https://getcomposer.org/
Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.
composer init
					
{
	"name": "ilbonzo/wp-site",
	"type": "project",
	"require": {}
}
					
Repetitive tasks in multi environments
  • Development
  • Testing
  • Staging
  • Production
  • Apache version and modules
  • Php version and extensions
  • Mysql version
  • Linux filesystem or Windows
  • ...

Developer

Vagrant

Vagrant is a tool for building and managing virtual machine environments in a single workflow. With an easy-to-use workflow and focus on automation, Vagrant lowers development environment setup time, increases production parity, and makes the "works on my machine" excuse a relic of the past.

Vagrant support VirtualBox, Hyper-V, VMWare and Docker as provider

    vagrant up
    vagrant halt
    vagrant ssh
    vagrant destroy

Vagrant boxes

https://www.vagrantup.com/docs/boxes.html

Wordpress

  • Linux
  • Apache
  • MySQL
  • PHP
https://app.vagrantup.com/beet/boxes/box
	vagrant init beet/box
	vagrant up
					
	mkdir -p site/web
	vagrant ssh
	sudo ln -s /vagrant/site/web /var/beetbox
	mysqladmin -u root password password
					
	// site/index.php
	<?php
	phpinfo();
					
	composer require johnpbloch/wordpress
					
	"require": {
		"johnpbloch/wordpress": "^5.2"
	}
					
	{
		"name": "ilbonzo/wp-site",
		"type": "project",
		"config": {
			"vendor-dir": "web/content/vendor"
		},
		"extra": {
			"wordpress-install-dir": "web/wp"
		},
		"require": {
			"johnpbloch/wordpress": "^5.2"
		}
	}
					
						// wp-config.php
	<?php
	// Load Composer’s autoloader
	require_once (__DIR__.'/../content/vendor/autoload.php');

	// Move the location of the content dir
	define('WP_CONTENT_DIR', dirname(__FILE__).'/../content');
	define( 'WP_CONTENT_URL', '/content');
					
						// index.php
	<?php
	/** Loads the WordPress Environment and Template */
	require( dirname( __FILE__ ) . '/wp/wp-blog-header.php' );
					
	RewriteEngine on
	RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
	RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
	RewriteCond %{REQUEST_URI} !^/phpmyadmin
	RewriteRule ^(.*)$ /wp/$1

					
{
	...
	"repositories": [
		{
			"type": "composer",
			"url" : "https://wpackagist.org"
		}
	],
	"extra": {
		...
		"installer-paths": {
			"web/content/plugins/{$name}/": ["type:wordpress-plugin"],
			"web/content/themes/{$name}/": ["type:wordpress-theme"]
		}
	},
	"require": {
		...
		"wpackagist-theme/twentynineteen": "*"
	}
}

					
	// wp-config.php
	define('WP_THEME_DIR',  dirname(__FILE__).'/../content/themes' );
	define('WP_PLUGIN_DIR',  dirname(__FILE__).'/../content/plugins' );
					
	composer update
					

.gitignore

	site/web/wp/
	site/web/content/vendor/
	site/web/content/themes/*
	site/web/content/plugins/*

	.vagrant
					

Wordpress Child theme

luna-fl-theme
/*
Theme Name:   Luna FL partner theme
Description:  Twenty Nineteen Child Theme
Author:       John Doe
Author URI:   http://example.com
Template:     twentynineteen
Version:      1.0.0
License:      GNU General Public License v2 or later
License URI:  http://www.gnu.org/licenses/gpl-2.0.html
Tags:         light, dark, two-columns
Text Domain:  twentynineteenchild
*/
					

Negate gitignore pattern

	!site/web/content/themes/luna-fl-theme
					
	$ git status

	new file:   .gitignore
	new file:   Vagrantfile
	new file:   site/composer.json
	new file:   site/composer.lock
	new file:   site/web/content/themes/luna-fl-theme/style.css
	new file:   site/web/index.php
					

Do the same for a plugin

Does it works?

Deploy

Deployer

https://deployer.org/
	curl -LO https://deployer.org/deployer.phar
	mv deployer.phar /usr/local/bin/dep
	chmod +x /usr/local/bin/dep
					
	dep init
					
	namespace Deployer;

	require 'recipe/common.php';

	// Project name
	set('application', 'my_project');
	...
	// Hosts

	host('project.com')
		->set('deploy_path', '~/{{application}}');


	// Tasks
	...

	// [Optional] If deploy fails automatically unlock.
	after('deploy:failed', 'deploy:unlock');

					
	desc('Deploy your project');
	task('deploy', [
		'deploy:info',
		'deploy:prepare',
		'deploy:lock',
		'deploy:release',
		'deploy:update_code',
		'deploy:shared',
		'deploy:writable',
		'deploy:vendors',
		'deploy:clear_paths',
		'deploy:symlink',
		'deploy:unlock',
		'cleanup',
		'success'
	]);
					
https://github.com/ilbonzo/steps-to-better-manage-and-deploy-code-demo/blob/master/deploy.php
		dep deploy
					
		dep rollback
					

Demo repository

https://github.com/ilbonzo/steps-to-better-manage-and-deploy-code-demo

Matteo Magni

Twitter @ilbonzo

Github @ilbonzo