Skip to content

Instantly share code, notes, and snippets.

@farhanangullia
Last active July 16, 2021 06:31
Show Gist options
  • Select an option

  • Save farhanangullia/18b3245367d9154c5c1d2bcd0330c372 to your computer and use it in GitHub Desktop.

Select an option

Save farhanangullia/18b3245367d9154c5c1d2bcd0330c372 to your computer and use it in GitHub Desktop.
Steps for mirroring empty gitlab repository and codecommit

Mirroring an empty Gitlab repository with existing Codecommit repository

Note: Repeat the required steps/checks below for all repositories as needed

Setup the gitlab repository (prerequisite):

  1. Create the GitLab repository if not done yet
  2. Make a temporary bare clone of the CodeCommit repository by using the --bare arg: example -> git clone --bare codecommit://sample-repo sample-repo
  3. Navigate to the cloned directory: example -> cd sample-repo
  4. Verify if all branches are there: git branch -a
  5. Change the origin remote to the empty Gitlab repository you created: git remote set-url origin
  6. Execute: git push origin --all
  7. All refs / branches would be pushed to the gitlab repository, however, if you have multiple branches, the default branch will be set at random. So change the default branch to main/master (according to what’s the default in your codecommit repo). Go to the gitlab repository and click Settings -> Repository -> Default branch -> change the default branch to the correct one and save changes
  8. Verify the contents of the gitlab repository. Verify the default branch in the gitlab repo is the same as codecommit repo’s default branch and all the branches are the same. If no differences, you can continue with the cleanup + mirroring steps below.
  9. Clean up the temp bare clone: cd .. then -> rm -rf sample-repo . Replace sample-repo with the name of the directory u used in step 2/3.

Mirroring steps:

  1. Create IAM User: gitlab-mirroring-user with the permissions for all the codecommit repositories (requires ARN of all repositories)
  2. Go to IAM User gitlab-mirroring-user -> Security credentials -> Generate HTTPS Git credentials for AWS CodeCommit and save the credentials (user id and password)
  3. Go to the source Gitlab repository for mirroring and click Settings -> Repository -> Under mirroring repositories, ensure the mirror director is push and then input the Git repo URL (formulate it with “https://<your_aws_git_userid>@git-codecommit.ap-southeast-1.amazonaws.com/v1/repos/<your_codecommit_repo_name>“) and password you copied earlier then click Mirror repository -> You can then force push to codecommit by clicking the refresh button under the mirrored repository

Fallback plan:

If the repositories aren’t in sync for some reason, with the codecommit repo having diverged commits/branches from the gitlab repository, you can try to check the box for keep divergent refs when mirroring the repository.

If that doesn’t work, I would suggest making the codecommit repo (which is the remote) to have no diff with the Gitlab repository. Two ways to do this: Remove all contents and branches of the Codecommit repository. Then attempt the mirroring from gitlab to codecommit. This will push all content that exist in the gitlab repo to codecommit and establish the mirror. or Ensure all branches between codecommit and gitlab repo has no diff. If there are diff, manually modify accordingly to keep the parity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment