Cách sử dụng Jenkins để deploy nhiều repo trên cùng một tài khoản GitHub. Chuyên mục Devops 2025-03-17 0 Lượt xem 0 Lượt thích 0 Bình luận
Có một số cách để bạn có thể sử dụng Jenkins để deploy nhiều repo trên cùng một tài khoản GitHub mà không bị giới hạn bởi Deploy Keys. Dưới đây là các giải pháp:
Cách 1: Dùng Personal Access Token (PAT) thay vì SSH Deploy Keys
Thay vì sử dụng SSH Deploy Keys (vốn chỉ có thể được dùng cho một repo duy nhất), bạn có thể sử dụng Personal Access Token (PAT) để kết nối Jenkins với nhiều repo trên GitHub.
Bước 1: Tạo Personal Access Token (PAT) trên GitHub
- Vào GitHub → Settings → Developer settings → Personal access tokens → Fine-grained tokens.
- Nhấn Generate new token và chọn các quyền:
- repo (full control với các private repo)
- admin:repo_hook (quản lý webhook nếu cần)
- Lưu lại token này vì nó sẽ chỉ hiển thị một lần.
Bước 2: Cấu hình Jenkins với PAT
- Vào Jenkins → Manage Jenkins → Manage Credentials.
- Chọn Global credentials → Add Credentials.
- Chọn:
- Kind: Username with password
- Username: your-github-username
- Password: your-personal-access-token
- ID: github-pat
- Khi config Jenkins pipeline, thay vì dùng SSH URL (git@github.com:...), bạn dùng HTTPS:
https://github.com/your-org-or-user/repo-name.git
- Khi clone repo trong Jenkinsfile hoặc job, bạn có thể thêm:
checkout([$class: 'GitSCM', branches: [[name: '*/main']], userRemoteConfigs: [[ url: 'https://github.com/your-org-or-user/repo-name.git', credentialsId: 'github-pat' ]] ])
Ưu điểm:
✔ Không bị giới hạn 1 key/repo.
✔ Dễ dàng quản lý nhiều repo chỉ với 1 token.
Một số lưu ý trong trường hợp đã cấu hình nhưng vẫn ko connect được hãy sử dụng 1 số cmd sau:
GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_your_ssh_key" git ls-remote -h git@github.com:your_github/project_cms.git HEAD
ssh -T git@github.com -i ~/.ssh/id_rsa_your_ssh_key -vvv
Cách 2: Dùng SSH Agent Plugin với Nhiều SSH Key
Nếu bạn muốn tiếp tục sử dụng SSH thay vì Personal Access Token, bạn có thể tạo nhiều SSH key trên Jenkins server.
Bước 1: Tạo nhiều SSH Key
- SSH vào server Jenkins:
ssh jenkins@your-server
- Tạo nhiều SSH keys khác nhau cho từng repo:
ssh-keygen -t rsa -b 4096 -C "jenkins-repo1" -f ~/.ssh/id_rsa_repo1 ssh-keygen -t rsa -b 4096 -C "jenkins-repo2" -f ~/.ssh/id_rsa_repo2
- Copy từng public key (.pub) lên GitHub Deploy Keys của từng repo.
Bước 2: Cấu hình Jenkins để sử dụng nhiều SSH Key
- Mở file ~/.ssh/config và thêm:
Host github-repo1 HostName github.com User git IdentityFile ~/.ssh/id_rsa_repo1 IdentitiesOnly yes Host github-repo2 HostName github.com User git IdentityFile ~/.ssh/id_rsa_repo2 IdentitiesOnly yes
- Trong Jenkinsfile hoặc job, khi clone repo, bạn sử dụng alias host:
checkout([$class: 'GitSCM', branches: [[name: '*/main']], userRemoteConfigs: [[ url: 'git@github-repo1:your-org-or-user/repo1.git' ]] ])
Ưu điểm:
✔ Vẫn dùng SSH nhưng không bị giới hạn 1 key/repo.
✔ Bảo mật tốt hơn PAT.
❌ Phải tạo nhiều key và quản lý file SSH config.
Cách 3: Dùng Deploy Key với Read-Only + GitHub Actions để Trigger Jenkins
Nếu bạn không muốn dùng Personal Access Token và không muốn cấu hình nhiều SSH key, bạn có thể:
- Dùng Deploy Key Read-Only cho Jenkins.
- Dùng GitHub Actions để trigger Jenkins build khi có thay đổi.
Cách này phù hợp nếu Jenkins chỉ cần clone code mà không cần push.
Kết luận:
✔ Dùng Personal Access Token là cách dễ nhất, nhanh nhất.
✔ Dùng nhiều SSH key với SSH Agent Plugin nếu muốn bảo mật tốt hơn.
✔ Dùng Deploy Key Read-Only + GitHub Actions nếu chỉ cần pull code từ GitHub.
Bình luận (0)