Keeping WordPress Under [Version] Control with Git – Warning: – Advantages of Version Control



Keeping WordPress Under [Version] Control with Git – Warning: – Advantages of Version Control

1 17


wordpress-git

Reveal.js version of my "Keeping WordPress Under [Version] Control with Git" blog post

On Github stevegrunwell / wordpress-git

Keeping WordPress Under [Version] Control with Git

Steve Grunwell / @stevegrunwell

Play along at home! stevegrunwell.github.io/wordpress-git

Who am I?

Warning:

This is not an "Introduction to Git"-type of course. While previous Git/version control experience is not required it is recommended!

Want to learn Git basics? Buy me a beer!

Git resources

WTF is Git?

  • Distributed Version Control System (VCS)
  • Created by Linus Torvalds (see: Linux)

Advantages of Version Control

  • Keep track of what changed, who made it, and when it happened
  • Easily roll back changes when necessary
  • With some systems (including Git), branching is cheap and easy, which makes it easier to develop features in parallel
  • Makes it easier to collaborate with other developers

Jesse, it's time to code!

What version control is not

Version control is not meant to be a replacement for real, scheduled backups.

Since every little change is tracked, it's not a great place to do debugging. Avoid committing untested or broken code to the repository!

Using Git with WordPress

Goals of the Workflow

Make setup + deployments easier Keep sensitive information out of the repository Never have a git status return modified files on production

Bonus: Go home on time, free of headaches.

What's included?

This strategy includes WordPress core and plugins in the repository.

Components could be adapted for another workflow that excludes these items.

  • Could be loaded via Git sub-modules, Composer, etc.

New environments in four easy steps:

Clone repository Import database Fill out wp-config.php Setup virtual host (if necessary)

The .gitignore file

# Keep these files out of the repo
/wp-content/themes/twenty*
/wp-content/upgrade
/wp-content/uploads
/sitemap.*
/wp-config.php
*.sql

# Hidden files
*.DS_Store
*Thumbs.db
*.sass-cache*
*~imageoptim*

Why keep out so much?

  • Config information will vary between environments
  • Generated content (user- or otherwise) has no place in a code repository
  • Remember: Version control !== backup

Reasons to keep uploads out of the repo

  • Uploads in the repo mean as soon as anything's uploaded your repo is out of date
  • Uploads + thumbnails = heavy repo w/ no benefit

Behold: A Better Way

<IfModule mod_rewrite.c>
  RewriteEngine on

  # Attempt to load files from production if
  # they're not in our local version
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule wp-content/uploads/(.*) \
    http://{PROD}/wp-content/uploads/$1 [NC,L]
</IfModule>

What does it do?

Does the requested file exist in the local filesystem?

  • Yes

    Load it as we normally would
  • No

    Attempt to load the file from production

What does it mean?

No more FTP-ing files to your dev/staging environments!

Dealing with wp-config.php

Create a fresh wp-config.php file

$ git clone {REPO} {DIRECTORY}
$ cd {DIRECTORY}
$ cp wp-config-sample.php wp-config.php
$ vi wp-config.php

WP_SITEURL and WP_HOME

Defining these values in wp-config.php eliminate the need to edit these values in the wp_options database table

// NO trailing slash!
define( 'WP_SITEURL', 'http://example.com' );
define( 'WP_HOME', WP_SITEURL );

This doesn't impact URLs already in post content!

DISALLOW_FILE_MODS or DISALLOW_FILE_EDIT

Prevent non-developers from installing/updating plugins or themes.

// Disable the theme and plugin installers and editors...
define( 'DISALLOW_FILE_MODS', true );

// ...or just disable the theme/plugin editor
define( 'DISALLOW_FILE_EDIT', true );

WordPress automatic updates

WordPress automatic updates won't run when evidence of version control is found, which is pretty baller.

It's not necessarily a bad thing to enable them, either

Other important items in wp-config.php

  • Database credentials
  • Keys + salts
  • Debug mode (where applicable)

Put it all together

Clone repository Import database Fill out wp-config.php Setup virtual host (if necessary)

Deploying new updates

$ git pull

…and that's it.

(unless you use better deployment tools)

Additional Tips

Has anything changed on the target server?

Add to deployment script to ensure nothing has changed on server:

$ git ls-files -dmo --exclude-standard

Will be empty if there are unchanged files, easier than parsing git status

Example: Capistrano Task

# Bail if there are modified files on the target server
task :untracked_files do
  is_dirty = capture "cd #{current_path} && git ls-files -dmo --exclude-standard"
  abort "There are modified files on #{app_environment}, please commit or reset \
  these before deploying!" unless is_dirty.empty?
end

before "deploy", "deploy:untracked_files"

Be sure to add REVISION to your .gitignore file!

Questions?

Steve GrunwellSenior Web Engineer, 10upstevegrunwell.com@stevegrunwell

Slides:stevegrunwell.github.io/wordpress-git

Keeping WordPress Under [Version] Control with Git Steve Grunwell / @stevegrunwell Play along at home! stevegrunwell.github.io/wordpress-git