How to set up Statamic on webfaction and use the Spock addon to push new content to your repo
Lately I’ve been getting more and more excited about Statamic, so it was just a matter of time before I would use it to build an actual site. But using it for real also meant thinking about a good version control strategy. The nice Statamic people already wrote some good things about version control strategies which you can read here. Ofcourse I wanted to have all content in version control, and I didn’t want to do that manually. I also didn’t want to download the entire site to my localhost before I could start developing. So only one option remained: Spock.
My host of choice for some time now is Webfaction. It almost feels I like working with nice people ;-). Well,
it took some time to figure out how to set everything up, but I’ve got it working and thought it would be nice to write a proper manual for others trying to figure this out. These steps probably work for other hosts and git repo’s too, or maybe you’ll have to tweak it a little. Let me know in the comments!
Add the entire site to your repo
I started out using just the site directory as a repo, but for this to work you have to use the entire directory. Yes, this means you’ll have version control on the assets and statamic dirs. This may feel a little dirty, but the upside is you’ll have a complete backup of the live site.
Add the spock addon
This is simple. Download the Spock addon and add it to the addons dir. Boom. Don’t forget to add it to the repo!
SSH to webfaction
By now you’ll need to use the terminal to access your site. There’s a tutorial for webfaction here. Git is already installed, but for some reason my github credentials kept failing. The *ahem* problem was 2 factor authentication (which you really should use!). For this to work I had to generate an ssh key and add the public part as a deploy key to my github repo. You’ll also have to add a personal access token which you have to use instead of your regular Github password. It’s important to use the git@github link for cloning the repo!. Otherwise you’ll have to enter your Github credentials every time.
The deploy keys can be found in Your repo -> Settings -> Deploy keys. Add a new key and paste the public part. Your terminal should say in which file this is. Don’t forget to tick the option for write access in Github!
Cloning your repo in webfaction
Ok, back to the terminal! Giving your repo and webapp the same name makes things a little easier. Just git clone the repo from your webapps dir. If you want to clone your repo into the current directory do this: git clone email@example.com:user/my-project.git . (The added dot achieves this). If you properly installed the deploy keys you shouldn’t have any problem doing this:
$ git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY
If everything was successful you’re almost done. Or so I thought. The site should now add, commit & push changes to my repo. Which it did on my localhost. But not online. So after searching for the problem I ran into the following problem: the repo didn’t know who I was! The solution was adding this info using these commands:
$ git config user.name "John Doe"
$ git config user.email firstname.lastname@example.org
Use your github credentials for this. At first I tried setting them globally, but for some reason this didn’t work. So after setting them for this repo only I finally saw my first commits. *inside dance*
Just one more thing
How did this impact my workflow? I usually just work in master since I’m the only user on most of my projects, but this wasn’t the best solution now. So here’s what I came up with: the master branch would be the live site. Which meant I would have to stop developing in the master branch. I would continue to develop in a new branch which I called ‘dev’. After completing something I would switch to master, pull from master, merge with dev, push changes, and switch back to dev.
Upload changes automatically
The best thing to do is to update the site automatically. This actually isn’t that hard. Make a new php file in the root of your site and add this code. After uploading that file log into Github and add a webhook. Add the url to the php file you just created as a Payload URL and you’re done. We only want the site to pull if the master branch was pushed. Technically it doesn’t really matter though. The site could pull def, but since it’s on master that wouldn’t affect anything. This just feels cleaner.
- Spock doesn’t seem to include changes such as adding a new user. This could probably be achieved by editing the code.
- Because you’ve also installed Spock on your localhost it will commit and push local changes too. Avoid this by setting the site to development in your .env file.
- Got any brighter ideas? Let me know!