Bitbucket Auto Deploy with Pipelines (CI/CD)

Bitbucket Pipelines is a CI/CD (Continuous Integration/Continuous Deployment) service integrated in the Bitbucket SCM platform. Learn how to deploy Bitbucket Pipelines in simple steps.

Bitbucket Auto Deploy with Pipelines

Bitbucket Pipelines helps developers to automate the process of developing and deploying code. It makes it easy to build, test and deploy the source code. This article demonstrates how to set up Bitbucket auto deployment with pipelines and push changes to a cPanel hosted repository. To understand it simply, the flow will be as follows:

  • User pushes changes to the Bitbucket repository.
  • Bitbucket repository receives new changes.
  • Bitbucket Pipelines, if enabled, deploys the changes based on configurations set in the bitbucket-pipelines.yml file.
  • Commands in cPanel or any other remote server will be executed to pull new changes.
 

Bitbucket Pipelines (CI/CD)

Bitbucket Pipelines is a built-in CI/CD service to automate the testing and deployment process. Developers should use Bitbucket Pipelines (CI/CD) to skip the extra step of pulling changes in cPanel or a remote server where the repository is hosted publicly. Bitbucket Pipelines are configured with a single YAML file in the repository. The key features that encourage the use of Bitbucket Pipelines are:

  • Automated builds and tests support.
  • The configurations for builds, tests and deployments are in a single bitbucket-pipelines.yml file.
  • The configuration is simple and easy.
  • Pipelines support multiple steps/stages for builds, tests and deployments.
  • Pipelines support Docker images, so Docker images can be used for building, testing and deploying phases.
  • Support for environment variables to keep credentials secure.
  • Pipelines can be integrated for different platforms like AWS, Google Cloud and Azure.
  • Faster and more flexible deployment of code.
 

Auto Deploy with Bitbucket Pipelines

To implement automated deployment with Bitbucket CI/CD, we first need to have a repository configured in cPanel or any remote hosting server. Visit this post Configure Bitbucket Repository in cPanel to set up a Bitbucket repository and generate SSH keys. We will use the same repository in this post.

We will be using the ssh-run 0.4.3 pipe to run bash commands on the remote server. So make sure SSH access is enabled on the remote server. Let us dive into configuring continuous deployment with Bitbucket Pipelines. Follow these simple steps to deploy a pipeline in Bitbucket and push changes to the remote server:

 

Step 1: Enable Bitbucket Pipelines

Bitbucket Pipelines allows us 50 free build minutes per month. The first step to deploy a pipeline is to enable pipelines in Bitbucket:

  • Log in to our Bitbucket account.
  • Navigate to the workspace of the repository that we intend to configure pipelines for.
  • Click "Repository settings" in the left-side menu.
  • Click "Settings" in the left-side menu.
  • Click the switch button to "Enable Pipelines".
Enable Bitbucket Pipelines

Step 2: Configure Bitbucket Pipelines

After enabling pipelines, it is time to configure the bitbucket-pipelines.yml file using a default Atlassian image with the ssh-run pipe to run bash commands on the remote server.

  • Click the Configure bitbucket-pipelines.yml button.
  • Select Starter Pipeline in the next window.
  • Add the following configuration to the bitbucket-pipelines.yml file.
  • image: atlassian/default-image:3 means we are using the default image.
  • The branches: indicates the deployment will be only for specific branches, which are defined in the master: in the next line.
  •  -step: Adds a step to deploy changes. The name: is the string name of the step, and deployment: specifies that the deployment is for production.
  • Then script: defines the commands to run in this pipe, which is a pipe atlassian/ssh-run:0.4.3.
  • The variables: are the environment variables for the SSH user, Remote Server IP, Remote Server Port and the SSH command script on the remote server. We will set these variables in the next step.
  • Finally, click the "Commit file" button after adding this configuration code. This will add the bitbucket-pipelines.yml file to the repository.
image: atlassian/default-image:3

pipelines:
branches:
master:
- step:
name: 'Deployment to Production'
deployment: production
script:
- pipe: atlassian/ssh-run:0.4.3
variables:
SSH_USER: $SSH_USER
SERVER: $SSH_SERVER
PORT: $SSH_PORT
COMMAND: $SSH_COMMAND
 

Step 3: Set Repository Variables

After the bitbucket-pipelines.yml file has been added to the repository, the constants defined in the pipeline file need to be configured. In the left side navigation, click "Repository variables" to configure environment variables for the following:

  • SSH_USER: The username of the remote server that is used for SSH access.
  • SSH_SERVER: The hostname or IP of the remote server to connect via SSH.
  • SSH_PORT: The port number that will be used for the SSH connection.
  • SSH_COMMAND: The bash command to pull changes from the repository or the file location of .sh file. We will create the deploy-bitbucket-cpanel.sh file with commands to pull changes.
Set Bitbucket Repository Variables
 

Step 4: Set Bitbucket SSH Keys

SSH keys are required to be saved in the Bitbucket repository to connect to a remote server. Steps to add SSH keys in a Bitbucket repository:

  • Click "SSH Keys" in the left side menu of the Repository settings.
  • Copy the contents of the private key from cPanel/remote server and paste it in the Private key box.
  • Copy the contents of the public key from cPanel/remote server and paste it in the public key box.
  • Click the "Save key pair" button to save SSH keys.
  • On the same page, we might also need to add the fingerprint of the host. Enter the server IP address along with the port number, like this 199.199.199.199:22. It will fetch the host fingerprint and click Add Host.
Set SSH Keys in Bitbucket Repository
 

Step 5: Create a Bash Command File in cPanel

We need to create a shell command file in cPanel or the remote server where our Bitbucket repository is hosted.

  • Create a file with the deploy-bitbucket-cpanel.sh name under the .ssh/ directory in cPanel.
  • The file should have the following content to pull changes from Bitbucket and update the server repository.
#!/bin/bash

# Change the directory to repository directory
cd /home/user/repositories/bitbucket-cpanel

# Pull the latest changes from Bitbucket
git pull origin master # or use a different branch name


# Any additional commands can be executed here (e.g., build, restart services, etc.)

composer install

npm run prod
 

Step 6: Test Pipelines Deployment Configuration

This is the final step where we test our continuous deployment implementation to confirm it is working as expected.

  • Make some changes to the repository on the local computer and commit them to the Bitbucket repository.
  • Click "Pipelines" in the left-side navigation of the repository.
  • We should see successful builds like in the screenshot below when the pipeline is successfully deployed.
Bitbucket Pipeline Builds

We have just configured continuous deployment with Bitbucket Pipelines that automatically deploys changes to production with minimal manual effort. It provides a simple and easy way to release the updates faster with low risk for errors. Make sure the SSH keys are properly configured and authorized in cPanel in case of any error.