Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Mirroring Repo with Github Only Pushes 1 Branch

Hi There

I'm trying to sync our repository on Bitbucket with a fresh repository on Github, such that when I push code to origin (Bitbucket), it pushes that commit on to the "mirrored" Github repository.

To do this, I created the Github repo and set up the ssh keys etc. I then added a Pipleline to Bitbucket called bitbucket-pipelines.yml which has the following code:

clone:
  depth: full
pipelines:
  default:
    - step:
        script:
          - git push --mirror git@github.com:orgname/nameofrepo.git

This brought over every commit and tag and the branch which I was currently on, but it did not bring over the other branches.

Do you know how to achieve this?
Thanks

2 answers

1 accepted

0 votes
Answer accepted

OK. I worked through this and will leave the answer here for the community.
The first step is to get an updated version of GIT.

As pipelines are run in docker containers, you just need to specify an updated image. (The images are on Docker Hub). That's the 1st line in my yml below.

The clone only creates/tracks the branch which you are currently on. So, you need to track all the other branches. You can also filter out branches which were already merged.

You do this with a for-each-ref statement (a git thing). Then, handle master and develop separately, because for some reason, the --no-merged flag excludes them.

Then, do the mirror statement as before.


image: atlassian/default-image:4.20230131

clone:

  depth: full

pipelines:

  default:    

    - step:

        script:

          - 'for i in $(git for-each-ref --format="%(refname:short)" --no-merged=origin/HEAD refs/remotes/origin); do echo $i; git show-ref --verify --quiet refs/heads/${i#origin/} || git switch --track $i; done; git switch --track origin/master; git switch --track origin/develop; git push --mirror git@github.com:nameOfYourOrganization/nameOfYourRepository.git'         

 

And that will push all your branches to Github.

This works for pipelines running on all commits on all branches, except the master one.

So if you do a PR the pipeline will do it's thing and pass. 

But once you merge the PR and the pipeline runs agains (this time it will git clone --branch="master") the pipeline fails when trying to git switch to master.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events