This post is an extended article for the early post: WordPress Deployment Workflow: Amazon LightSail, GitHub and Revisr.
Revisr is a WordPress plugin to fastly deploy GitHub codes to WordPress websites (and it is a free tool). This plugin can be installed from WordPress plugin marketplace.
Let’s review this situation:
- We have a staging environment which is on the same dev branch with our local host, for development purpose
- 3 instances of production websites which run under a load balancer, on the master branch (this branch should have the most stable version of the website)
Revisr will help:
- Each time there is a Git Push from localhost to GitHub, code auto deployed to the staging live site, because both localhost and staging site have Revisr watched over the same branch
- Each time there is a merge from dev branch into master on Github, code auto deployed to all 3 production instances, at the same time.
So no need to login to the SSH terminal and run git pull on each website instance each time we want to see the changes alive.
Basic Use / Requisites
Install Revisr plugin on the website instances that you want to pull content / updates.
In case you are running several instances under a load balancer, install Revisr on all of them.
In this post, we will see how to install and run Revisr on HTTPS method.
Server requirements
The Revisr needs to be able to write on the .git/config and .git/objects so we have to make some changes to the LightSail instances.
- For each instance, go to Amazon LightSail > SSH into the system.
$ sudo chown bitnami:bitnami -R /opt/bitnami/apps/wordpress/htdocs/
$ sudo chmod 777 -R /opt/bitnami/apps/wordpress/htdocs/
To make sure the system works fine:
- Go to Revisr Settings (from WP Dashboard) > Help. Repository Writable has to be true
- Revisr > Dashboard. Make a test Pull Changes, it should not inform any errors.
Revisr Settings
General
Fill in Git Username and Email. Others like .gitignore will be from the current repo that Revisr recognizes.
Remote
We are using HTTPS method, stating the Git username and password in the Remote URL itself. To make it secure, obtain a PAT from Github so that we can use a token to replace our current password.
- Remote URL: https://username:{PAT-token}@github.com/{user}/{repo-link}.git
- Check this option: Automatically pull new commits?. It will generate a payload URL for the Github webhooks which we will implement a bit later.
Database
In this case, we don’t need to track database at all because the database is from a cloud storage (RDS / Google Cloud Platform) so each time the local server’s database change it will take effect on the live site also.
- Database Tables to track: None
Creating Webhooks on GitHub
After installing and complete the Revisr settings on WordPress, we create webhooks on Github. Basically we just need to go to the Github repo > Settings > Webhooks > Add webhook and paste in the link that Revisr provides as the Payload URL.
Each instance’s website URL can be assigned with a static IP on Amazon LightSail so the URLs that Revisr auto pull generate are quite stable (it does not change if the instance is rebooted or restarted).
Steps
For each Amazon LightSail instances:
- Copy the URL from Revisr Settings > Remote > Automatically pull new commits? as the Payload URL. Choose event push. Assume that we have 3 instances of websites running under a load balancer, we will have then 3 webhooks.
- If you want to trigger the autopull based on a merge, choose push and then set the plugin to watch on master (supposed that you join the new codes into dev branch and then finally merge the perfect version into master).
Even though the database is shared, this URL will be different for each instance, based on the IP address of the web instance.
Webhooks will be created from GitHub
Securing .git folder and .gitignore
Make a 404 redirect to all access starting at .git after the domain name by configuring Apache:
$ sudo nano /opt/bitnami/apache2/conf/bitnami/bitnami.conf
Add to the last line:
redirectMatch 404 /.git
Restart Apache:
$ sudo /opt/bitnami/ctlscript.sh restart apache