Continuous Delivery/ Deployment from Git Repository to CPanel/ FTP Server


These days a lot of developers and DevOps engineers implement CI/CD (Continuous Integration/ Continuous Delivery) to make the deployment in the cloud easy and fast. Yes of course... There are so many tutorials to teach you to implement CI/CD to deploy apps, that are developed by using latest front-end and back-end frameworks, on firebase, aws servers, google cloud platform, etc. But this tutorial is to deploy a simple website or a web-app on a shared hosting. There are still many of us, who host their sites and apps in shared hosting, because the app is simple and the cost is low as well. Lets start to learn how to deploy your code to shared hosting via FTP (File Transfer Protocol) whenever something new is pushed to Bitbucket repository.

Bitbucket has an integrated CI/CD service called Bitbucket Pipelines that allows us to automatically build, test and deploy our code. We are going to setup a pipeline by creating bitbucket-pipelines.yml file in the root directory of our repository. In this file, we’ll add steps to deploy our app to FTP server when code is pushed to the master branch. Lets follow these steps to successfully implement a CI/CD to deploy your app.
  1. Enable bitbucket pipeline
  2. Configure initializing yml file for FTP deployment
  3. Setting up environment variables
  4. Triggering a build

Enable Bitbucket Pipeline

This is the first thing you have to do. Go to the project you need to implement CI/CD pipeline. Then go to the left column and click on the "Settings" section.

Then you will see another menu. There's a section called "Pipeline" and under that section, you will see another tab called "Settings". Click on it.

Then enable pipelines.
Then click on the "create bitbucket-pipelines.yml" button. This will create a new file in the root of your repository called bitbucket-pipelines.yml. This file controls:
  • The Docker image that should be used to run your tests in
  • For which branches the CI/ CD should be triggered
  • What steps it should go through (which commands should be run).

Configure Initializing yml File for FTP Deployment

After clicking the “create bitbucket-pipelines.yml” button, BitBucket will automatically open up the editor. Now you have to copy below configurations and paste it on the editor and commit the changes.
Warning - Keep spacing as it is. Otherwise it will give errors. You can learn how to write a yml file from the beginning through https://confluence.atlassian.com/bitbucket/configure-bitbucket-pipelines-yml-792298910.html


Setting Up Environment Variable

As you see... $FTP_USERNAME, $FTP_PASSWORD, $FTP_URL are environment variable and we have to set up them. First of all, you have to create FTP username and password from your CPanel dashboard. Log into your CPanel dashboard and go to "FTP Accounts" section. There you can add a new FTP account.
Remember your 'Log In' and 'Password'. And keep the 'Directory' field empty. Now go to your Bitbucket repository > Settings > Pipelines > Repository variables.



There you can set up $FTP_USERNAME, $FTP_PASSWORD, $FTP_URL variables. Mine will be like below if I created the CPanel FTP account just like I mentioned above.
  • FTP_USERNAME = ravindu
  • FTP_PASSWORD = *********
  • FTP_URL = ftp://ftp.sixthflow.com/public_html
Note - Your FTP_URL should be ftp://ftp.your_domain/public_html

Triggering an Init and Push Changes to Server.

Now you have to go to "Pipelines" section of your repo and click on "Run Pipeline" button. Then you will get a successful pipeline initialization.

If it gives a "Failed" or "Error" message, you have done something wrong. Just follow up the tutorial again carefully and fix the issues.

Note - The initialization pipeline will run successfully only once. After that you have to change the bitbucket-pipelines.yml file to run a pipeline again and get a success message.

After you run the first successful pipeline, then you have to change the bitbucket-pipelines.yml file as follows to automatically run a pipeline whenever you push something new to the repository.
You can view all your pipeline activities in the "Pipelines" section and view how that pipeline ran by clicking on any pipeline.
That's all. Now whenever you push new commits to your repo, they will be automatically deployed to your shared hosting space. Just try it out. If you need to limit this CI/CD pipeline to a specific branch in the repo, you can click here to learn how to write the bitbucket-pipelines.yml file to do so and change your yml file. You can do so many different new things if you know how to write your own yml file. Hope you learnt something new and very useful.

Do not hesitate to comment here if you stuck in any step or if you have any other problem related to this topic. Thanks!