Embed WordPress blog in site with version control and automated deploy

Here’s one way to embed a WordPress blog into an existing website already managed by a version control system and a continuous deployment service workflow to a cloud server.

In a version controlled website, programming and content changes begin in a code repository on a developers workstation. These code changes are committed to a shared repository for others to further develop or test. When the code is approved for a release, it is then deployed to the web server manually or with a continuous deployment service that automates release deploys or rollbacks without interrupting users of the website.

In contrast, changes to WordPress posts, theme, plugins, media and etc. are typically managed through the WordPress wp-admin/ pages hosted on the web server.

These completely different management approaches tend to cause pain when integrating them together. Adding WordPress to a version control system can expose admin passwords and make a site more difficult to maintain. And some continuous deployment services will wipe out any wp-admin changes on the server when a new repo release is deployed.

In my case, WordPress happens to fit nicely into my site’s <root>/blog/ folder as a couple dozen php files along with wpadmin, wp-content and wp-includes folders.

Of these files and folders, version controlling only the wp-content/themes folder is necessary, because the goal is simply to keep the blog look-and-feel consistent with the rest of the website and to manage everything else WordPress through the wp-admin page.

My local BitBucket repo’s .gitignore file, therefore, limits versioning to the theme folder while keeping all other WordPress files intact:

# Ignore all files from blog except wp-content

# Ignore all content except theme

# Ignore hidden system files

For updating the server automatically with repo changes, my site’s DeployBot workflow runs pre-version and post-version server console commands which preserve all non-theme WordPress files. For small and simple websites like mine, this can be an opportunity to collect a few snapshots of website code for disaster recovery.

These before and after commands are found in the DeployBot server’s General Settings, currently under Overview -> Production -> Servers & Settings -> Servers configuration -> Settings -> Run commands after a new version is uploaded and Run commands after a new version becomes active sections.

Here’s an example of my Run commands after a new version is uploaded config that does the following:

  • Remove the oldest backup
  • Rename third to last backup
  • Rename second to last backup
  • Rename last backup
  • Backup current website to home folder
rm -rf ~/current3
mv ~/current2/ ~/current3
mv ~/current1/ ~/current2
mv ~/current0/ ~/current1
cp -avr /var/www/html/current/. ~/current0

And, here’s an example of my Run commands after a new version becomes active config that does the following:

  • Copy .htaccess back to website
  • Copy all php files in WordPress root back to website
  • Copy wp-admin folder back to website
  • Copy wp-includes folder back to website
  • Copy all php files wp-content back to website
  • Copy all plugins back to website
  • Copy all uploads back to website
cp -avr ~/current0/<your path here>/blog/.htaccess /var/www/html/current/<your path here>/<where it should be>
cp -avr ~/current0/<your path here>/blog/*.php /var/www/html/current/<your path here>/blog/
cp -avr ~/current0/<your path here>/blog/wp-admin/ /var/www/html/current/<your path here>/blog/
cp -avr ~/current0/<your path here>/blog/wp-includes/ /var/www/html/current/<your path here>/blog/
cp -avr ~/current0/<your path here>/blog/wp-content/*.php /var/www/html/current/<your path here>/blog/wp-content/
cp -avr ~/current0/<your path here>/blog/wp-content/plugins/ /var/www/html/current/<your path here>/blog/wp-content/
cp -avr ~/current0/<your path here>/blog/wp-content/uploads/ /var/www/html/current/<your path here>/blog/wp-content/

With this setup, my existing website and WordPress play nicely together. Updates to website content don’t interfere with WordPress, and WordPress updates don’t interfere with other website pages.