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

Bitbucket pipelines helps 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 deploy Bitbucket repository changes to a cPanel hosted repository using pipelines. To understand it in simple way, the flow will be as below:
- User pushes changes to Bitbucket Repository.
- Bitbucket repository receives new changes.
- Bitbucket pipelines if enabled deploys the changes based on configurations set in bitbucket-pipelines.yml file.
- Commands in cPanel or any other remote server will be executed to pull new changes.
Bitbucket Pipelines
Bitbucket pipelines is a built-in CI/CD service to automate the testing and deployment process. You should use Bitbucket pipelines to skip extra step to pull changes in cPanel or remote server where your repository is hosted for public. Bitbucket pipelines are configured with a single YAML file in 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 file bitbucket-pipelines.yml.
- 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 flexible deployment of code.
Auto Deploy with Bitbucket Pipelines
In order to implement automated deployment in Bitbucket you 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 ssh-run 0.4.3 pipe to run bash commands on remote server. So make sure SSH access is enabled on remote server. Let's dive into configuring continuous deployment with Bitbucket pipelines. Follow these simple steps to deploy pipeline in Bitbucket and push changes to remote server:
Step 1: Enable Bitbucket Pipelines
Bitbucket pipelines allows you 50 free build minutes per month. First step to deploy a pipeline is to enable pipelines in Bitbucket:
- Login to your Bitbucket account.
- Navigate to workspace of repository which you intend to configure pipelines for.
- Click on "Repository settings" in left side menu.
- Click on "Settings" in left side menu.
- Click the switch button to "Enable 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 ssh-run pipe to deploy pipeline and run bash commands on remote server.
- Click Configure bitbucket-pipelines.yml button.
- Select Starter Pipeline in next window.
- Add the following configuration to bitbucket-pipelines.yml file.
- image: atlassian/default-image:3 mean we are using default image.
- The branches: indicates the deployment will be only for specific branch which is defined master: in next line.
- -step: Adds a step to deploy changes. The name: is string name of step and deployment: specifies 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 SSH user, Remote Server IP, Remote Server Port and the SSH command script on remote server. We will set these variables in next step.
- Finally after adding this configuration code click "Commit file" button. This will add bitbucket-pipelines.yml file to 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 bitbucket-pipelines.yml file has been added to repository the constants defined in pipeline file need to be configured. In left side navigation click on "Repository variables" to configure environment variables for following:
-
SSH_USER
: The username of remote server which is used to for SSH access. SSH_SERVER
: The hostname or IP of 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 command that will run to pull changes from Bitbucket repository. We will create deploy-bitbucket-cpanel.sh file which will contain the commands to pull changes.

Step 4: Set Bitbucket SSH Keys
SSH keys are required to be saved in Bitbucket repository in order to connect to remote server. Steps to add SSH keys in Bitbucket repository:
- Click "SSH Keys" in left side menu of Repository setting.
- Copy the contents of private key from cPanel/remote server and paste it in Private key box.
- Copy the contents of public key from cPanel/remote server and paste it in public key box.
- Click "Save key pair" button to save SSH keys.
- On same page you might also need to add finger print of host. Enter the server IP address along with port number like this 199.199.199.199:22. It will fetch the host finger print and click add host.

Step 5: Create Bash Command File in cPanel
Now that everything is configured in Bitbucket, create a shell command file in cPanel or remote server where your Bitbucket repository is hosted for public.
- Create a file under .ssh/ directory in cPanel with name deploy-bitbucket-cpanel.sh.
- 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 its time to test the continuous deployment of Bitbucket pipelines.
- Add a file or commit changes from your local machine to Bitbucket repository.
- Click on "Pipelines" in left side navigation of repository.
- When pipelines are deployed successfully you should see successful builds like in screenshot below.
