WordPress Automation with Bitbucket Pipeline and Amazon s3

Until recently, my workflow involved writing theme/plugin code, pushing it to Git repository, downloading the plugin and uploading to WooCommerce or EDD for sale. If there was any problem, I would have to fix, push, download and upload.

As you know, it wasn’t fun and so I decided to look for a solution.

With the power of WordPress, Bitbucket Pipeline CI/CD and Amazon s3, I was able to automate my workflow quite a bit. Not only was my flow reduced to only git push, but it also allows for the flexibility of automating PHPUnit, PHPCS, PHPBCF and many more.

How I did it is what I want to show you.

Amazon s3

We need 4 data from Amazon s3, namely:

  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY
  • AWS_DEFAULT_REGION
  • S3_BUCKET

1. Head over to Amazon s3, create an account and log in

2. Create a new bucket

Right now, we have two out of four (4) data needed from Amazon s3 – region and bucket name. To get the remaining two, you would need to sign in to AWS Management Console and open the IAM console. Please follow this tutorial.

Bitbucket Pipeline

Thanks to the Atlassian team!!!

Bitbucket offers free CI/CD service with Pipeline. We need to do four (4) things on Bitbucket, namely:

  • Configure bitbucket-pipeline.yml file
  • Create a Bitbucket repository
  • Configure repository (or environment) variables
  • Add repository (or environment) variables

1. Configure bitbucket-pipeline.yml file

In your theme or plugin directory, create a file named bitbucket-pipelines.yml and a directory named dist. Also, add the following code to it:

https://gist.github.com/davexpression/5e30ae129b9a9f5a62f349587eafab1c

This code has two steps. The first step zips your folder and excludes folders like vendor and file extensions like .yml, .lock etc. Don’t forget you may have to edit this to suit your needs. This will store the zip folder in dist folder. Immediately you push to your repository, this step will be triggered automatically.

The second step stores your zipped folder on your Amazon s3 bucket. It’s triggered manually but you may remove that line if you want to trigger it automatically. The variables in the portion of code eg $AWS_ACCESS_KEY_ID will be defined shortly.

Remember to rename plugin-cicd with your plugin folder name.

2. Create a repository

Let’s name our repository plugin-cicd

After creating your bitbucket repository, please connect your existing repository to Bitbucket and push your code to Bitbucket. Here is what my folder structure looks like:

3. Configure repository (or environment) variables

After creating your repository, go to Settings > Settings > Configure bitbucket-pipeline.yml to set up your environment variables.

After creating your repository, go to Settings > Pipelines > Repository variables to set up your environment variables.

4. Deploy

Now push your code to Bitbucket again and check your Pipelines, click on the latest pipeline and you will see that the first step “Build and Upload to s3” was automatically completed. To complete the process, hit the “Deploy” button.

That’s it! This will zip your folder to your Bitbucket repository.