Search code examples
gitlabbitbucketbranchmirror

Need to setup a gitlab mirror of a bitbucket repo with only ONE branch exposed


I need to only expose one branch from my bitbucket repo to a new gitlab repo. My students are using the gitlab.com world and I have all my teaching stuff in bitbucket repo's. And I would like to have specific branches for specific classes.

For example:

Bitbucket has the repo APCSA with the branches: master per1 per2 per3 dev

I want to have different repo's setup on gitlab under different gitlab groups.

So under one group I'd like to have them see only:

APCSA:per1 branch (and I'd love to figure out how to mirror this)

I've tried lots of combo's of mirroring but it always mirrors the entire repo, all branches.


Solution

  • You can create a GitLab CI/CD pipeline to clone, or pull, a single branch. Your pipeline can run on a different branch, in order to avoid conflicts and such.

    Using the web ui,

    • create a new branch called mirror
    • in Settings, populate CI/CD variables: your upstream, the branch to mirror, and your repo address
    • create a new file called .gitlab-ci.yml in the mirror branch
      • the pipeline will be trigged upon (each) commit, and you'll be able to check if it's working
    • create a scheduled pipeline to update your repository with the desired frequency

    This is my .gitlab-ci.yml to mirror an upstream branch:

    job:
        script:
            - git remote show writable || git remote add writable $WRITABLE_URL
            - git checkout $UPSTREAM_BRANCH || git checkout -b $UPSTREAM_BRANCH
            - git pull $UPSTREAM_URL $UPSTREAM_BRANCH
            - git push writable $UPSTREAM_BRANCH
        only:
            - mirror
    

    The variables are:

    You will need to use the command line to install gitlab-runner, if you don't have one already. You will also need to create a deploy key (Settings / Repository) in order to allow push to work (or use deploy token, but write access is not available in the free plan for deploy tokens).