Wordpress Automated Deployments using Bitbucket and Jenkins

Jenkins — Wordpress — Bitbucket

Ive been assigned to a task where I need to automate our Wordpress deployment process and be able to track our changes in our codebase. Currently, our code deployment is all being done thru FTP. Yes you heard it right, we deploy our code via FTP including our production site. By doing so, we’ve experience a lot of issues e.g long queues of transfer, missing files, new feature or bug fix not working, untracked files and worst is 500 errors. Unless you started your coding career recently, chances is you’ve gone through this as well.

FTP is old and it’s also simple, which makes the process of transferring files very easy. There is no point in denying that plain old FTP is widely used and almost always available while better technologies often pulled the short straw.

After couple of searches in Google, Jenkins seems to be the right choice for simplifying our workflow paired with bitbucket server for our repo storage. To learn more about Jenkins browse to their official guide here https://jenkins.io/doc/book/getting-started/

I wont go through the whole process of how I installed and configured our Jenkins server as this is kind of tedious but you should be fine as long as you read and follow the official guide carefully.

To make the deployment of Wordpress automated, I’ve created a bash script which is being ran in the Build Step section of the configuration of the Job Item. Check the script here https://gist.github.com/kcabading/ab1db4c42bf596ec43679a2f252c48e3 . Feel free to use the script and just make sure to replace all the placeholders (e.g [STAGING_FOLDER], [STAGING_URL]).

Bash scripts that does all the work for copying repo changes.

Below is the steps on what the script does when triggered:

  1. Check which repository branch the build (origin/staging or origin/master) is triggered.
  2. If this is the first build, create database and restore the db.sql which is also included in the repository for initial deployment. Also creates the user, password and assign the correct permission to the db.
  3. Additionally, if the build comes from the master branch, we run a different script that creates a cpanel account for the wordpress instance.
  4. I have a base Wordpress files located in the root folder which I rsync to the project webroot then issue another rsync this time from the target jenkins workspace which looks like /var/lib/jenkins/workspace/[JOBNAME]
  5. At this point, we have all the required and updated files from the repo to make the website work except for the wp-config.php file in which the scripts creates by copying the content wp-config-sample file in the base Wordpress directory. It then replace all placeholders (e.g $DB_HOST, $DB_USER, $DB_PASSWORD) with real data we generated in the script.
  6. The next builds after the initial now will just issue rsync command from the target jenkins workspace to the project webroot.

And below is the Complete workflow I’ve presented to my team to follow when developing Wordpress application.

A. Setting up Version control and automated deployments

1. Create bitbucket repository.

2. Open the repository and go to Settings.

a. Under Webhooks, add a new one and enter this under URL field http://[SERVER_URL]:8080/bitbucket-hook/ and name it Jenkins Webhook.

b. Under Triggers, Select “Choose from a full list of triggers” and tick both Push and Merged triggers.

3. Login to Jenkins Server http://[SERVER_URL]:8080/

4. Create a New Item

a. Item name should be the same as the Bitbucket repo name

b. Under Copy from field below, enter gobro( job template preconfigured ) and select or enter

c. Under Source Code Management, replace the repository URL.

d. Leave everything as is and click on Apply then Save.

B. Setting up local Wordpress Development environment

1. Setup your local WordPress instance just like what you normally do.

2. Create .gitignore file and copy the content from here https://gist.github.com/kcabading/a95fd9e8533bb1296e87b29300816409

3. Dump your database in your root directory and name it as db.sql

4. Work on your local environment until it’s ready for staging.

5. Open your Sourcetree App.

6. Create a Local Repository and find the project you’re working on.

7. From here, you should be able to review all your file changes under the wp-content folder under file status.

8. Staged/Commit all your file changes first. Upon doing this, this will create a master branch.

9. Once done with the Commit, add the remote repository URL. (This is for the local repo to know where it will store/push our files)

a. Go to Repository Menu.

b. Click on Repository Settings

c. Go to Remotes Tab, and then click on Add.

d. Enter “origin” in the Remote Name and fill out the URL/path with the repository URL and click on OK.

10. Create a “staging” branch and make sure to checkout on this branch.

11. Push your changes and upon success.

a. Bitbucket will trigger the Webhook to our server

b. Jenkins receive the trigger and if it identifies that this is the first build, and we are currently working in staging environment. The build script will create a staging instance at http://[STAGING_URL]/{REPOSITORY_NAME}

c. Check the site in staging, if you are using less, you may need to manually compile it.

12. Before you start working on any additional changes, make sure you trigger PULL request in Source tree to get the latest commit in the server. We should always work on our local environment for any further changes, feature request or bug fixes and just commit and push our changes to the staging branch

C. Production Deployment

1. Login to Jenkins Server and select the Job Item for the instance and click on configure.

2. Under Build section, update the parameters “domain.com” to the client’s domain name and

“cpaneluser” to the cpanel username. See below for screenshot:

3. Checkout to master branch in the Sourcetree app. Create one if not yet available.

4. Select the latest commit in the staging branch. Right click on it and select Merge.

5. Commit and then Push.

6. Then in the Jenkins Server Job Item, check on the logs to see the generated cpanel username and password as well as check if the deployment is succesful or not.