Create a CI/CD Pipeline for your Wordpress code using AWS CodeCommit

Wordpress is the largest content management system (CMS) in the World and the main engine behind at least half of the websites hosted globally. It is a considered an old school CMS based on PHP and Apache, yet it is quite a powerful platform and flexible enough to adopt to the modern CI/CD pipelines. AWS CodeCommit is highly scalable source control service for hosting private git repositories.

In this article we will create Wordpress Integration with AWS CodeCommit to push your code to a private git repository.

Steps

1. Create a Repository in Code Commit called “Demo-Website” and Create a Wordpress instance with a custom theme on an EC2 CentOS instance

2. Upload Public SSH key of the user with which you have created the code commit repository.

Go to AWS console > IAM > Users > Select User > Security Credentials > Upload SSH Public Key

For eg : On your client machine generate Public/Private Key

$ cd .ssh

$ ssh-keygen -o

#generates 2 files id_rsa.pub is the public keyfile and id_rsa is the private key file
id_rsa
id_rsa.pub

$ cat id_rsa.pub

Paste this content into the upload public ssh key and then Copy the SSH Key ID For eg : AP**************W4Z

3. Go back to client machine .ssh folder

Create a file called config and paste below contents and change the SSH Key ID to yours

$ vi config

Host git-codecommit.*.amazonaws.com
User AP***********Z
IdentityFile ~/.ssh/id_rsa

$ chmod 600 config

4. Test SSH connectivity

$ ssh git-codecommit.us-east-2.amazonaws.com
Warning: Permanently added the RSA host key for IP address ‘52.95.17.51’ to the list of known hosts.
You have successfully authenticated over SSH. You can use Git to interact with AWS CodeCommit. Interactive shells are not supported.Connection to git-codecommit.us-east-2.amazonaws.com closed by remote host.
Connection to git-codecommit.us-east-2.amazonaws.com closed.

If you configured everything well then you can see the “successfully authenticated over SSH” message as above

5. Now it is time to add the WP Theme to your Repo.

First go to your Repository in code commit and you will see the ssh option in “Clone URL” for you repository. Click on it and copy it and keep it handy for the next step.

$ cd /var/www/html/wp-content/themes/wha_Demo

$ git init

$ git add .

$ git commit -m ‘first commit to Demo website’

# Now Create a Remote Server using the SSH URL for you repository. You now need to set up the connection to your remote server. Make sure you’re still in your child theme directory and run the following command:

$ git remote add codecommit ssh://git-codecommit.ap-southeast-2.amazonaws.com/v1/repos/Demo-Website

You can replace ‘codecommit’ with any name you want to give to the server. Your local and server repositories will now be connected, which means that you can push changes to your server using this command:

$ git push codecommit +master:refs/heads/master

You’ll only need to enter this command the first time you want to push a change. Afterward, you should use the following command instead:

$ git push codecommit

That’s it, you’re done! Yes it is that easy and you have successfully created local and server repositories, connected the two, and pushed your local data to your server.

==== PUSH/PULL Code =======

Go to parent .git diretory

$ cd /var/www/html/wp-content/themes/wha_Demo

edit page.php file and add a comment

$ git commit -m “Change in page.php”

$ git push codecommit

counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 357 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To ssh://git-codecommit.ap-southeast-2.amazonaws.com/v1/repos/Demo-Website
dcf7cb2..726811f master -> master

You have successfully pushed local server changes to the repo. This can be done from a developers workstation to the code repository and then to an EC2 instance for your Wordpress instance.

Principal Cloud Solutions Architect at Oracle