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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.