Using GitLab CI to Deploy WordPress Sites

So I work with a team that use Git to manage all the theme code for a large WordPress site. It’s a corporate, and as is the way with big organisations, they wanted their version control in house. Hence I set them up a GitLab box. Want to make an alteration to CSS or whatever?

  1. Code & test on your local dev stack
  2. Commit via git & push up to Gitlab
  3. SSH into the server, `git pull` the new code down
  4. Run `gulp build` to update the compiled assets

Job done. Wouldn’t it be great if you didn’t need steps 3 & 4 though? They don’t take long in the short term, but get very tedious after a while. They also introduce an element of risk if you have less technical people needing to deploy code that shouldn’t be let loose on the terminal of your production web server…. Hence: GitLab CI to the rescue!

Continue reading “Using GitLab CI to Deploy WordPress Sites”

Git version control and WordPress

I use Git for pretty much any decent sized coding project I do, and that includes WordPress sites. There are a number of different ways to set things up, but I thought I’d share mine as I think it’s the cleanest and nicest way to go about it. …although I would, wouldn’t I!

First thing: Don’t keep the core files under version control. WordPress has a great update system baked in so this is not necessary.

Second thing: Make sure you can bring anything you wrote yourself under version control. As well as the template, you may make custom site specific plugins, have special things going on in your .htaccess etc. etc.

So, make a new git repo, copy the core files into it, tell git to ignore them all with .gitignore and start to commit and push our own code.

Make yourself an ssh key pair:

[bash]ssh-keygen -t rsa[/bash]

Accept the defaults, get the new public key:

[bash]cat ~/.ssh/[/bash]

Copy that into Github or Beanstalk or wherever your repository is hosted.

Now lets put it up on a server. As Git doesn’t like cloning into non-empty directories, we need to download the wordpress core into a separate dir and merge it in using rsync (mv will not work here):

[bash]cd /var/www/your_site
git clone html
rsync -a wordpress/ html/
rm -rf wordpress[/bash]

Sharing Git Branches

Say you’re working on a site and want to try something out, then show it to someone else or push it to a test server, all without touching your master/production code on Git. It’s branches you need! As I forgot exactly how to do this the past couple of times, thought it best to write it down. Come on memory.

Create a new local branch:

[bash]git checkout -b new_exciting_thing[/bash]

You are now on a new branch:

[bash]git branch -a[/bash]

See it with a little star next to it. Good. Write some code and commit. Now push the branch to the remote repo (could be called origin, or beanstalk if you named it strangely like me):

[bash]git push -u origin new_exciting_thing[/bash]

Get on your test server that also tracks that remote and pull:

[bash]git pull[/bash]

You should see your new branch on the remote:

[bash]git branch -a

* master
remotes/origin/HEAD -> origin/master

Check it out and you’re done:

[bash]git checkout –track origin/new_exciting_thing[/bash]

Hooray! …or “Boom!” or “Booyah!” if you want to sound like a twat.