CI/CD Pipeline Integration Atlassian Bitbucket to AWS EC2 for PHP Application

Amit Yadav
3 min readNov 8, 2021
CI/CD Toolchain

Continuous Integration and Continuous deployment pipeline is the series of steps to deliver a new version of the software.

CI/CD pipeline introduces monitoring and automation to improve the process of software development.

In this article, we will show how we can integrate the CI/CD pipeline into the Atlassian Bitbucket to deploy the software on AWS EC2

STEP 1:

Create a Repository on Bitbucket. Follow the link for more info on repository creation

STEP 2:

Select your repository and click on the Pipelines section to create a bitbucket pipeline configuration file. For this article, we will use a PHP application pipeline template from the recommended template.

STEP 3:

Once the bitbucket-pipelines.yml file is created now we need to create pipelines steps and scripts which will be executed in every step.

We will use the Atlassian rsync-deploy tool for deployment. RSYNC deploy need below-given things to setup SSH connection with EC2 instance

I. EC2 username

Ii. EC2 host

Iii. SSH Key

We can store EC2 username and host in repository variable securely and SSH key under repository settings.

To add repository variable follow the given steps

i. Click on Repository Settings

ii. Under Repository, Settings select the Repository Variables option

iii. Give variable name and value then select the secured check box and click on Add button to add the variable.

To add a server SSH key follow the given steps

i. Click on Repository Settings

ii. Under Repository, Settings select the SSH Keys option

iii. From the SSH key section select the Use my own keys option.

iv. To add a private key open .pem file (which is used to connect with EC2 using SSH) in a text editor and copy the whole content then paste the same into the Private key box.

v. Now we need to add the public key. To get the public key run the following command in the system terminal

ssh-keygen -y -f <Path to .pem file>

Now copy the output of the command and paste it into the Public key box. After that click on Save key pair button to save the keys.

We are done with our credentials setting. Now we need to update the bitbucket-pipelines.yml

We can add multiple branches and set up the steps and scripts for that particular branch.

We can pass files, folder names in EXTRA_ARGS and those will be escaped during deployment.

To know more about rsync-deploy click here.

Our pipeline setup is complete. Now, whenever you will push a commit into the branches pipelines will be activated automatically and deploy the changes to the EC2 instance.

Hope this article will help you to set up your CI/CD pipeline

Happy Learning :)

You can follow me on Linkedin

--

--