A BitBucket CI/CD Pipeline to Sync Branches With GitHub

Sync branches between BitBucket and GitHub repos

Albin Issac
Dec 3, 2020 · 5 min read
Waves washing up on shore
Waves washing up on shore
Photo by César Couto on Unsplash.

Most of the time, we will have a requirement to sync branches between two different repositories (e.g. BitBucket repository to GitHub repository).

This is a common scenario when working on a cloud repository, but the day-to-day development is managed through local repositories. In that case, the local repository branches should be synced to the cloud repository for deployment. Handling the sync manually creates a lot of issues and also consumes more time from the development team. The better option is to automate the branch sync between the local repository and cloud repository. Complete repository mirroring can be enabled, but that will sync every branch. My aim is to sync only specific branches.

Diagram of pipeline to sync branches
Diagram of pipeline to sync branches
Photo by the author.

In this tutorial, we’ll see an approach to sync the branches between a BitBucket repository and GitHub through a BitBucket CI/CD pipeline. The same approach can be followed with minimal changes to sync the branches of any two repositories.

Prerequisites

  • Bit Bucket repository
  • Empty GitHub repository

Set Up Bitbucket Pipeline

I have created a branch with the name dev in the BitBucket repository. The dev branch should be synced with the GitHub repository on any changes. The dev branch will be created in the GitHub repository during the first sync.

The pipeline will be executed on every change pushed into the specified branches (e.g. dev and sync the changes with the remote GitHub repository).

Log in to the BitBucket repository, click on “Pipelines,” and click on “Create your first pipeline”:

Creating a pipeline
Creating a pipeline

Select “Starter pipeline”:

Choosing a pipeline template
Choosing a pipeline template

Enable the configurations below in bitbucket-pipelines.yml and commit the changes. The file will be committed to the root of the main branch. Copy the file into the other required branches (e.g. dev):

Modify the branch name references (dev, dev1, and uat) based on your configurations. Also, modify the remote (GitHub) repository URL accordingly.

On commit of changes to the dev, dev1, and uat branches, the pipeline is triggered and the environment-specific scripts below are executed to sync the local branches (BitBucket) with the remote repository (GitHub):

git remote add sync git@github.com:techforum-repo/test1.git - add GitHub repository as the additional remote repository and tag with name "sync"
git checkout dev - Check out the latest changes from local dev bracnh( modify branch name accordingly), this step can be skipped as BitBucket pipeline already checked out the laetst branch
git pull - pull the latest change from local branch, this step can be skipped as BitBucket pipeline already pulled out the laetst branch
git push sync dev - push the local(BitBucket) branch to the remote GitHub repository (modify branch name accordingly)

Generate SSH Keys

The HTTPS Git URL of the remote repository (https://techforum-repo:password@github.com/techforum-repo/test1.git) can be used along with credentials in the pipeline to push the local changes to the remote repository (store the credentials in the repository or deployment variables and refer to them in the pipeline), but the best practice is to use the SSH URL (git@github.com:techforum-repo/test1.git).

The SSH keys should be configured to enable SSH-based integration. As a first step, generate the SSH keys by executing the command below (you can execute through GIT bash). The SSH keys can be generated through BitBucket UI if required:

ssh-keygen -t ed25519 -C "test@techforum.com"
Generating public and private keys
Generating public and private keys

This will generate the public (id_ed25519.pub) and private (id_ed25519) keys under the .ssh folder.

Set Up SSH Keys — GitHub

Now log in to the GitHub repository and create a new deployment key:

Creating a deployment key
Creating a deployment key

Add a title to the key (sync-bitbucket). Enter the public key from the file id_ed25519.pub.

Select “Allow write access.” Click on “Add Key”:

Adding a key
Adding a key

Set Up SSH Keys — BitBucket

Log in to the BitBucket repository and click on “Repository settings”:

Repo settings
Repo settings

Click on “SSH Keys” and “Use my own keys”( as discussed earlier, you can use the “Generate Keys” button to generate private and public keys).

Add the private key from the id_ed25519 file, public key from the id_ed25519.pub file, and save the key pair.

Saving a key pair
Saving a key pair

Now push some changes to one of the branches (e.g. dev) in BitBucket. This will trigger the corresponding pipeline steps:

Pushing changes
Pushing changes
Result of pushing changes
Result of pushing changes

Once the pipeline is successfully completed, specific branches (e.g. dev) from the BitBucket repository are synced to the GitHub repository (the corresponding branches will be created in GitHub for the first sync):

Syncing the repos
Syncing the repos

A CI/CD pipeline can help us to sync branches between two different repositories. Here, we are syncing specific branches from a BitBucket repository to a GitHub repository, but the same approach can be followed to sync two different repositories. If required, the external CI/CD tools (e.g. Jenkins) can be used to sync two different repositories.

In the next tutorial, we will explore how to use the Jenkins pipeline to sync branches between two repositories.

Advice for programmers.

By Better Programming

A weekly newsletter sent every Friday with the best articles we published that week. Code tutorials, advice, career opportunities, and more! Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Thanks to Zack Shapiro

Albin Issac

Written by

Working as a Software Architect on Marketing Technologies. Reach out to me on Linkedin: https://www.linkedin.com/in/albin-issac-56917523/

Better Programming

Advice for programmers.

Albin Issac

Written by

Working as a Software Architect on Marketing Technologies. Reach out to me on Linkedin: https://www.linkedin.com/in/albin-issac-56917523/

Better Programming

Advice for programmers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store