把本地的倉庫推送到遠程倉庫 gitlab上
實現gitlab和本地之間進行推送倉庫,首先要做ssh認證
配置 SSHKEY
前面我們已經在 GitLab 創建了倉庫,並且授權用戶可以使用倉庫,我們所有的操作都是在 WEB 頁面進行,下在我們介紹如何使用客戶端來連接我們的倉庫。
我們的倉庫是私有的,只有授權的用戶才可以訪問到該倉庫,那么只要將客戶端的用戶與我們 GitLab 的用戶綁定,客戶端即可訪問到 GitLab 上的倉庫,
我們建議使用 SSH 方式實現客戶端與 Gitlab 用戶的綁定,具體配置如下:
在客戶端生成 ssh 密鑰對(注 windows 客戶端下只能使用 rsa 加密方式):
我們在本地客戶端生成公鑰,把公鑰配到對應的gitlab用戶的ssh key上
[root@ci-node1 ~]# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:dKgV7ndo1haY4XeUseN/yjj7/2AWNH1wC2GpK/fLfuY root@ci-node1 The key's randomart image is: +---[RSA 2048]----+ | . . +*o.| | . + +.oo=.| | = = + *.o| | = . = = o.| | . S = = o | | = = o | | o . + o| | o* =.| | +BOE+| +----[SHA256]-----+
[root@ci-node1 ~]# ll .ssh/ total 8 -rw------- 1 root root 1679 Mar 29 17:55 id_rsa -rw-r--r-- 1 root root 395 Mar 29 17:55 id_rsa.pub
[root@ci-node1 ~]# cat .ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCstYVepXE4/B85nKNU6gaVpPsjGXu6Ql2sKRgRDxsHGmuZVg+q0GD3c0Ml2XkzozswTRuKCcIhZjK/DEGbbgsBEH8q6HHrWCP6vAWb+7wIEvhtJVm6+5zOGA/AorFczEgnm9b2ekPkwAMV7qrOQ1KG9S94U+XRwAtjgNV0MuYx5S0liBr9KBEt8pQ7Fd2TMo95rF3328gGnWLZJWmETjtJaWpLKDgWnQ7BdlK89o6eyYBtdKJfZ5uYM3tgeX8GiRA+daR8fpEruOSpjuvRbyjRStf5ZXPIemL46lXcEDYurnQ4Uucv5F+paqTWnzYHziQOoKd3Jnxgqsh24TZeoEDP root@ci-node1
我們將該用戶與 GitLab 的 root 用戶綁定,復制用戶的公鑰,在 GitLab 主頁面點擊用戶設置
進入到用戶設置頁面,點擊左側菜單欄的 SSH KEYS
進入到 SSH KEYS 管理頁面
此處只能添加公鑰,如果添加私鑰系統將報錯,此外一個公鑰在整個 GitLab 系統中只能添加一次,但是一個 GitLab 用戶可以添加多個公鑰。
配置完了,本地客戶端和gitlab認證完成了
推送本地客戶端倉庫到 GitLab
把git_test倉庫推到遠程倉庫 gitlab 本地已經有git_test倉庫
首先我們要將GitLab上的git_test倉庫配置為ci-node1上git_test倉庫的遠程倉庫,
為他配置遠程倉庫
[root@ci-node1 git_test]# git remote add gitlab git@192.168.31.11:test/git_test.git [root@ci-node1 git_test]# git remote gitlab
其次,使用 git push 命令直接推送本地倉庫的 master 分支到遠程倉庫
[root@ci-node1 git_test]# git push -u gitlab master The authenticity of host '192.168.31.11 (192.168.31.11)' can't be established. ECDSA key fingerprint is SHA256:Eal3aUwcGcxH1ZD9X799taSiGiCMFgAE7rnoY74f25U. ECDSA key fingerprint is MD5:27:41:0f:5a:4c:ba:8f:23:bd:23:44:8d:18:53:e6:30. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.31.11' (ECDSA) to the list of known hosts. Counting objects: 30, done. Compressing objects: 100% (18/18), done. Writing objects: 100% (30/30), 2.50 KiB | 0 bytes/s, done. Total 30 (delta 2), reused 0 (delta 0) To 192.168.31.11:test/git_test.git * [new branch] master -> master Branch master set up to track remote branch master from gitlab.
提示推送功能,我們在 GitLab 上的 git_test 倉庫就可以看到我們推送上來的內容,
認證是正確的,現在實現了本地的倉庫和遠程gitlab的集成,能把倉庫推上去
克隆 GitLab 倉庫到本地客戶端
我們要把gitlab 的倉庫 克隆到另外一台機器上
我們在另外一台機器上 ci-node2 安裝git服務
參考這個帖子 源碼安裝git
https://www.cnblogs.com/mingerlcm/p/11403126.html
安裝好git后 配置git全局配置 郵箱 name
https://www.cnblogs.com/mingerlcm/p/11403274.html
ci-node2客戶端生成ssh 公鑰 做ssh認證
[root@ci-node2 ~]# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:pCb3ozG5sS3ZrnkDUsLTD5RTSule2QxE6oCcmAlzKTU root@ci-node2 The key's randomart image is: +---[RSA 2048]----+ |o.E. .*+ | |oB.+ .=o. | |+.+..+o..= | | +o=oo o | | .==+S | | .+oo. | | .=+o | | oX+. | | *=o. | +----[SHA256]-----+
把公鑰配置到gitlab
[root@ci-node2 ~]# ll .ssh/ total 8 -rw------- 1 root root 1675 Mar 29 23:39 id_rsa -rw-r--r-- 1 root root 395 Mar 29 23:39 id_rsa.pub
首先,我們配置 ci-node2 客戶端與 GitLab 上 dev 用戶綁定,
把ci-node2 客戶端的公鑰配置到 gitlab 上的dev用戶綁定
登錄dev用戶
gitlab要求第一次登錄dev用戶需要重新修改默認密碼
把ci-node2 客戶端的公鑰配置到 gitlab 上的dev用戶綁定
現在認證已經做完了
第二,使用 git clone 命令克隆倉庫到 ci-node2 本地
[root@ci-node2 data]# git clone git@192.168.31.11:test/git_test.git Cloning into 'git_test'... The authenticity of host '192.168.31.11 (192.168.31.11)' can't be established. ECDSA key fingerprint is SHA256:Eal3aUwcGcxH1ZD9X799taSiGiCMFgAE7rnoY74f25U. ECDSA key fingerprint is MD5:27:41:0f:5a:4c:ba:8f:23:bd:23:44:8d:18:53:e6:30. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.31.11' (ECDSA) to the list of known hosts. remote: Counting objects: 30, done. remote: Compressing objects: 100% (18/18), done. remote: Total 30 (delta 2), reused 0 (delta 0) Receiving objects: 100% (30/30), done. Resolving deltas: 100% (2/2), done. Checking connectivity... done.
我們可以看到已經將 GitLab 上的 git_test 倉庫克隆到了 ci-node2 本地,同時為本地倉庫添加了一個指向 GitLab 上 git_test 倉庫的遠程倉庫。
執行git clone時候,會默認會倉庫添加一個默認的遠程倉庫 就是gitlab
[root@ci-node2 data]# ll git_test/ total 4 -rw-r--r-- 1 root root 21 Mar 29 23:55 a -rw-r--r-- 1 root root 0 Mar 29 23:55 master -rw-r--r-- 1 root root 0 Mar 29 23:55 test [root@ci-node2 data]# cd git_test/ [root@ci-node2 git_test]# [root@ci-node2 git_test]# git remote origin
我們 ci-node2 的 git_test 上創建一個 dev 分支,並將 dev 分支,推送到 GitLab 上:
驗證是否默認遠程倉庫 是否是gitlab
建立dev分支
// 建立dev分支 [root@ci-node2 git_test]# git branch dev // 切換到dev分支 [root@ci-node2 git_test]# git checkout dev Switched to branch 'dev'
在dev分支,創建一個文件
[root@ci-node2 git_test]# touch dev
提交commit,需要先配置git全局配置
[root@ci-node2 git_test]# git add . [root@ci-node2 git_test]# git commit -m "commit dev on dev branch" [dev a0e7b8d] commit dev on dev branch 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 dev
把dev分支推到gitlab
[root@ci-node2 git_test]# git status On branch dev nothing to commit, working tree clean [root@ci-node2 git_test]# git push -u origin dev Counting objects: 2, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (2/2), 227 bytes | 0 bytes/s, done. Total 2 (delta 1), reused 0 (delta 0) remote: remote: To create a merge request for dev, visit: remote: http://192.168.31.11/test/git_test/merge_requests/new?merge_request%5Bsource_branch%5D=dev remote: To 192.168.31.11:test/git_test.git * [new branch] dev -> dev Branch dev set up to track remote branch dev from origin.
完成后,我們在 GitLab 上可以看到我們剛推上來的 dev 分支