Setup Bitbucket Repository in cPanel

Bitbucket is a git based code hosting cloud built for team collaboration to make code development and deployment simple and easy. Learn how to setup Bitbucket private repository in cPanel instead traditional file uploads via file manager clients.

Setup Bitbucket Repository in Cpanel

Before you start make sure you have:

  1. Git Version Control support in cPanel.
  2. Terminal enabled in cPanel. Required for creating SSH keys etc.

To understand it in a simple way we split this post into following steps:

  1. Create a Bitbucket repository
  2. Create SSH Keys in cPanel
  3. Assign SSH Keys to Bitbucket repository
  4. Clone Bitbucket repository in cPanel 

Create Bitbucket Repository

  1. Login to your Bitbucket account.
  2. Navigate to correct workspace. If you need select different work space click on user avatar icon on top right. Then click "All Workspaces".
  3. Click on "Create" button in top menu and then click "Repository" in dropdown.
  4.  Select a project or create new one.
  5. Enter a name for repository. 
  6. Check Access Level to Private Repository.
  7. Click "Create repository".

setup-bitbucket-repository-01
 

Create SSH Keys in cPanel

Now that we have created our repository we need SSH keys to generate before cloning the repository.

  1. Login to your hosting server's client area and navigate to cPanel.
  2. Open "Terminal" and run this command to generate SSH key:
    ssh-keygen -t rsa -f ~/.ssh/bitbucket-cpanel -b 4096 -C "bitbucket-cpanel"
    -t rsa: indicates the algorithm of SSH key.
    -f ~/.ssh/bitbucket-cpanel: indicates the name of SSH key including the path where key will be stored.
    -b 4096: indicates the size of SSH key.
    -C "bitbucket-cpanel" indicates a comment for SSH key. Useful in case you have multiple keys for different repositories. This comment will help to identify the key.
    After you have run this command it will ask for passphrase leave it empty and press enter twice.
  3. Run this command to create SSH config file
    touch ~/.ssh/config
  4. Run this command to change permissions of SSH config file.
    chmod 0600 ~/.ssh/config
  5. Run this command to change the user of SSH config file.
    chown user:user ~/.ssh/config
    Replace user in "user:user" with your cPanel user name.
  6. Now open ~/.ssh/config file in file manager and add the following to file
    Host bitbucket.org
    AddKeysToAgent yes
    Hostname bitbucket.org
    IdentityFile ~/.ssh/bitbucket-cpanel

Assign SSH Key to Bitbucket Repository

Now that we have created our SSH keys we need to add it to our Bitbucket repository. So head back to Bitbucket repository. 

  1. Click "Repository Settings" in left side menu.
  2. Click "Access Keys" in left side menu.
  3. Click "Add Key" button.
  4. Enter a nice descriptive label for key.
  5. Copy and paste the contents of "~/.ssh/bitbucket-cpanel.pub" either by opening it in cpanel or running the command "cat ~/.ssh/bitbucket-cpanel.pub" which will output the content of key file in terminal. You can copy it from terminal.
  6. Finally click "Add SSH Key".

setup-bitbucket-repository-02
 

Clone Bitbucket Repository in cPanel

Now that we have added our key in Bitbucket we can clone the repository in cPanel in two different ways one is cloning it via Terminal command and other is using the Git Version Control tool of cPanel.

Clone Repository via Terminal:

  1. Go to repository home in Bitbucket.
  2. Clone the SSH url of repository in from clone modal.
  3. Head back to cPanel terminal.
  4. Navigate to directory where you want to clone this repository.
  5. Paste the clone command you copied from Bitbucket.
  6. Wait for cloning to complete and that's it.

setup-bitbucket-repository-03

Clone Repository via Git Version Control:

I personally recommend this way to create/clone repository via Git Version Control Tool. As it will also create logs of commits and pulls. To clone repository via Git Version Control Tool:

  1. Go to Git Version Control under "Files" section of your cPanel Tools.
  2. Click "Create" button.
  3. Turn the option "Clone Repository" on if its not on by default.
  4. Enter the clone URL of your repository in URL field. The url should be in this format "[email protected]:username/bitbucket-cpanel.git" where username is your Bitbucket username.
  5. Once the repository is detected the "Repository Path" and "Repository Name" will be automatically filled. You can still change it as per your directory structure.
  6. Click "Create" and that's it.

setup-bitbucket-repository-04
 

You can now pull changes in cPanel repository manually accessing the Terminal or via SSH Agent from your local machine. In next posts we will implement auto-deployment for this repository so whenever changes are pushed to this repository it will be updated on cPanel as well.

References: