Bitbucket Auto Deploy with Pipelines (CI/CD)

Bitbucket pipelines are a CI/CD (Continuous Integration/Continuous Deployment) service integrated in the Bitbucket SCM platform. Learn the pipeline deployment of Bitbucket in simple steps.

Bitbucket Auto Deploy with Pipelines

Bitbucket pipelines help developers to automate the process of development and deployment of 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 in a simple way, the flow will be as follows:

  • User pushes changes to the Bitbucket Repository.
  • Bitbucket repository receives new changes.
  • Bitbucket pipelines, if enabled, deploy 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 are 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 to pull changes in cPanel or a remote server where the repository is hosted for the public. 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 one 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 allow 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 deploy the pipeline and 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 remote server. We will set these variables in the next step.
  • Finally, after adding this configuration code, click the "Commit file" button. 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 on which the SSH connection will be established.
  • SSH_COMMAND: The bash command or file location of the command that will run to pull changes from the Bitbucket repository. We will create the deploy-bitbucket-cpanel.sh file, which will contain the 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

Now that everything is configured in Bitbucket, create a shell command file in cPanel or a remote server where our Bitbucket repository is hosted for the public. 

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

# Navigate to the deployment directory
cd /home/user/repositories/bitbucket-cpanel

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


# Additional commands if needed (e.g., build, restart services, etc.)

composer install

npm run prod
 

Step 6: Test Pipelines Deployment

Now it's time to test the continuous deployment of Bitbucket pipelines. 

  • Add a file or commit changes from our local machine to the Bitbucket repository. 
  • Click "Pipelines" in the left side navigation of the repository.
  • When pipelines are deployed successfully, we should see successful builds like in the screenshot below.
Bitbucket Pipeline Builds

That is it; by following these steps, we just configured continuous deployment in Bitbucket using pipelines. Make sure the SSH keys are properly configured and are authorized in cPanel in case of any error.