A BitBucket CI/CD Pipeline to Sync Branches With GitHub
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.
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.
- 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”:
Select “Starter pipeline”:
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.
Modify the branch name references (
uat) based on your configurations. Also, modify the remote (GitHub) repository URL accordingly.
On commit of changes to the
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 email@example.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:firstname.lastname@example.org/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 (email@example.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 "firstname.lastname@example.org"
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:
Add a title to the key (
sync-bitbucket). Enter the public key from the file
Select “Allow write access.” Click on “Add Key”:
Set Up SSH Keys — BitBucket
Log in to the BitBucket repository and click on “Repository 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.
Now push some changes to one of the branches (e.g.
dev) in BitBucket. This will trigger the corresponding pipeline steps:
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):
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.