Skip to content

Setting up a mirror of a GitHub Repo using GitLab Community Edition Server

TerrenceMcGuinness-NOAA edited this page Sep 10, 2024 · 1 revision

For the Community Edition of GitLab, which does not support pull mirroring directly, you can set up a workaround using a scheduled CI/CD pipeline to pull changes from the GitHub repository and push them to the GitLab repository. Here’s how you can do it:

  1. Create a Personal Access Token on GitHub:

    • Go to GitHub settings.
    • Navigate to "Developer settings" -> "Personal access tokens".
    • Generate a new token with repo scope.
  2. Create a GitLab CI/CD Pipeline:

    • Go to your GitLab repository.
    • Navigate to "Settings" -> "CI / CD".
    • Expand the "Variables" section and add the following variables:
      • GITHUB_REPO_URL: The URL of the GitHub repository.
      • GITHUB_TOKEN: The personal access token you created on GitHub.
  3. Create a .gitlab-ci.yml File:

    • Add a .gitlab-ci.yml file to your GitLab repository with the following content:
stages:
  - mirror

mirror:
  stage: mirror
  script:
    - git config --global user.name "Your Name"
    - git config --global user.email "your.email@example.com"
    - git clone --mirror https://oauth2:${GITHUB_TOKEN}@${GITHUB_REPO_URL} repo.git
    - cd repo.git
    - git remote add gitlab https://gitlab-ci-token:${CI_JOB_TOKEN}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git
    - git push -f gitlab --all
    - git push -f gitlab --tags
  only:
    - schedules
  1. Schedule the Pipeline:
    • Go to "CI / CD" -> "Schedules".
    • Create a new pipeline schedule to run the mirroring job at your desired frequency (e.g., daily).

Explanation:

  • Personal Access Token: Used to authenticate with GitHub.
  • CI/CD Variables: Store sensitive information securely.
  • .gitlab-ci.yml: Defines the CI/CD pipeline to clone the GitHub repository and push it to GitLab.
  • Pipeline Schedule: Ensures the mirroring job runs at regular intervals.

This setup will periodically pull changes from the GitHub repository and push them to the GitLab repository, effectively mirroring the repositories.