Embed WordPress blog in site with version control and automated deploy

January 18, 2019

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
/<site>/blog/*
/<site>/blog/wp-admin/
/<site>/blog/wp-content/plugins/
/<site>/blog/wp-content/uploads/
/<site>/blog/wp-includes/
!/<site>/blog/wp-content/

# Ignore all content except theme
/<site>/blog/wp-content/*
!/<site>l/blog/wp-content/themes/

# Ignore hidden system files
*.DS_Store
*[Tt]humbs.db
*.Trashes

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:

  1. Remove the oldest backup
  2. Rename third to last backup
  3. Rename second to last backup
  4. Rename last backup
  5. 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:

  1. Copy .htaccess back to website
  2. Copy all *.php files in WordPress root back to website
  3. Copy wp-admin folder back to website
  4. Copy wp-includes folder back to website
  5. Copy all *.php files wp-content back to website
  6. Copy all plugins back to website
  7. 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.

Blog-on.

Leave a Reply

Your email address will not be published. Required fields are marked *