Mirror repository từ GitLab sang GitHub
Code được cập nhật trên GitLab, GitHub tự động nhận bản mới nhất. Chính repository này là bản demo: source nằm trên GitLab và được push mirror sang GitHub.
Khi tạo repository mới trên GitHub, GitHub cho phép import repository từ nguồn git khác.
Tuy nhiên import chỉ chạy một lần. Nếu vẫn muốn làm việc trên GitLab mà GitHub luôn có code mới nhất, hãy dùng push mirror của GitLab theo các bước dưới đây.
1. Tạo GitHub Personal Access Token
- Vào
GitHub Settings>Developer settings>Personal access tokens>Fine-grained tokens: github.com/settings/personal-access-tokens - Bấm
Generate new token. - Đặt tên cho token và chọn quyền:
Expiration: nên đặt thời hạn nhất định.Repository access: tốt nhất chọnOnly select repositories, chỉ cấp quyền cho repository thực sự cần. Nếu setup cho nhiều repository cùng owner và không muốn sửa lại về sau, có thể chọnAll repositories.Permissions: mụcContentschọnAccess: Read and write. Nếu repository có file trong.github/workflows/, thêmWorkflows: Read and write, nếu không GitHub sẽ từ chối push các file đó.
- Copy token vừa tạo. Token chỉ hiển thị một lần.
1.1. Nếu repository thuộc Organization
- Vào
Settingscủa organization, mụcPersonal access tokens, tabFine-grained tokens. VD:https://github.com/organizations/ORG/settings/personal-access-tokens - Ở
Fine-grained personal access tokenschọnAllow access via fine-grained personal access tokens. - Ở
Require approval of fine-grained personal access tokens:Require administrator approval: PAT tạo ra cần được admin duyệt.Do not require administrator approval: không cần duyệt.
- Bấm
Saveở mỗi mục, sau đó quay lại tạo PAT ở bước 1.
2. Thiết lập mirror trên GitLab
- Vào project trên GitLab, chọn
Settings>Repository. - Mở rộng phần
Mirroring repositories. VD: gitlab.com/tiennm99/demo-gitlab-mirror/-/settings/repository - Bấm
Add new(dấu+) và điền:Git repository URL:https://github.com/GROUP/PROJECT.git, thayGROUPvàPROJECTbằng tên trên GitHub. VD:https://github.com/tiennm99/demo-gitlab-mirror.gitUsername: username của GitHub account.Password: personal access token đã tạo ở bước 1.
- Bấm
Mirror repository.
3. Kiểm tra trạng thái
Sau khi thiết lập, mirror được liệt kê với URL dạng:
https://*****:*****@github.com/<your_github_group>/<your_github_project>.git
Để kiểm tra mirror hoạt động:
- Push một commit mới lên GitLab.
- Đợi khoảng 5 phút (hoặc 1 phút nếu bật
Only mirror protected branches). - Kiểm tra trên GitHub xem commit đã được sync chưa.
- Muốn sync ngay thì bấm
Update now(nút retry).
Lưu ý
- Mirror tự động sync các commit mới từ GitLab sang GitHub. Đừng commit thẳng lên GitHub, nếu không branch sẽ bị diverge.
- Branch đã merge vào default branch và bị xoá ở GitLab sẽ bị xoá ở mirror trong lần push tiếp theo.
- Branch còn thay đổi chưa merge sẽ được giữ lại.
- Nếu branch bị diverge, phần
Mirroring repositoriessẽ hiển thị lỗi. - Khi token hết hạn, mirror sẽ báo lỗi xác thực; tạo token mới rồi cập nhật lại password của mirror.