How to Set Up a CI/CD for WordPress with GitHub Actions and AWS
Setting up a Continuous Integration/Continuous Deployment (CI/CD) pipeline for a WordPress site can greatly enhance your development workflow, ensuring that code changes are automatically tested, built, and deployed. GitHub Actions and AWS provide a powerful combination to achieve this. In this blog post, we will walk you through the steps to set up a CI/CD pipeline for your WordPress site using GitHub Actions and AWS.
Prerequisites
Before we start, ensure you have the following:
- AWS Account: An AWS account with necessary permissions to create and manage resources.
- GitHub Repository: A GitHub repository where your WordPress project is stored.
- AWS CLI: Installed and configured with access to your AWS account.
Step 1: Set Up Your AWS Environment
1.1 Create an S3 Bucket:
S3 will store your WordPress site’s static files.
- Log in to the AWS Management Console.
- Go to S3 and create a new bucket (e.g.,
my-wordpress-site
). - Configure the bucket for public access or set up CloudFront for content delivery.
1.2 Set Up RDS for WordPress Database:
- Go to RDS and create a new MySQL or MariaDB instance.
- Configure security groups to allow access from your EC2 instances.
1.3 Create an EC2 Instance:
- Launch an EC2 instance using an appropriate AMI (Amazon Linux 2 is recommended).
- Install necessary software (Apache/Nginx, PHP, MySQL client).
- Configure security groups to allow HTTP/HTTPS traffic.
Step 2: Prepare Your GitHub Repository
2.1 Structure Your Repository:
Organize your WordPress project in the repository, ensuring you include all necessary files (themes, plugins, etc.).
2.2 Create a .github/workflows
Directory:
In the root of your repository, create a .github/workflows
directory to store your GitHub Actions workflow files.
Step 3: Configure GitHub Actions
3.1 Create a GitHub Actions Workflow:
Create a file named ci-cd.yml
in the .github/workflows
directory.
name: CI/CD for WordPress
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Sync files to S3
run: aws s3 sync . s3://my-wordpress-site --delete
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: 'us-east-1'
- name: Deploy to EC2
run: |
ssh -o StrictHostKeyChecking=no ec2-user@${{ secrets.EC2_PUBLIC_IP }} << 'EOF'
cd /var/www/html
git pull origin main
composer install
npm install
npm run build
sudo systemctl restart apache2
EOF
env:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
EC2_PUBLIC_IP: ${{ secrets.EC2_PUBLIC_IP }}
Step 4: Configure GitHub Secrets
- In your GitHub repository, go to
Settings
>Secrets
>Actions
. - Add the following secrets:
AWS_ACCESS_KEY_ID
: Your AWS access key ID.AWS_SECRET_ACCESS_KEY
: Your AWS secret access key.EC2_PUBLIC_IP
: The public IP address of your EC2 instance.SSH_PRIVATE_KEY
: Your EC2 instance’s SSH private key.
Step 5: Deploy Your WordPress Site
With the GitHub Actions workflow configured, any push to the main
branch will trigger the CI/CD pipeline.
- Build Phase:
- The code is checked out, dependencies are installed, and tests are run.
- Deploy Phase:
- Files are synchronized to the S3 bucket.
- The WordPress site is deployed to the EC2 instance using SSH, and the web server is restarted.
Conclusion
Setting up a CI/CD pipeline for WordPress using GitHub Actions and AWS automates the process of testing and deploying your site, ensuring that updates are seamlessly and consistently applied. This approach not only saves time but also reduces the risk of human error, leading to a more reliable and efficient development workflow.
By following this guide, you can leverage the power of automation to maintain a robust WordPress site capable of handling continuous updates and high traffic.