【超级全面】jenkins通过SSH凭证方式拉取Gitee代码(含错误解决方法)
jenkins通过SSH凭证方式拉取Gitee代码
使用 SSH 密钥方式拉取 Gitee 上的代码,我们只需要在服务器上使用 root 用户生成公钥和私钥,将公钥配置到 Gitee 中,再将私钥配置到 Jenkins 中。这样 Jenkins 就可以免密从 Gitee 上拉取代码了。
一、生成密钥
1、生成密钥之前检查一下是否已经生成密钥,存在密钥则进行删除:
删除存在的密钥是防止跟着此流程走出一些未知的错
root@bpvank-virtual-machine:~# cd /root/.ssh
root@bpvank-virtual-machine:~/.ssh# ls
id_rsa id_rsa.pub
root@bpvank-virtual-machine:~/.ssh# rm id_rsa
root@bpvank-virtual-machine:~/.ssh# rm id_rsa.pub
root@bpvank-virtual-machine:~/.ssh# ls
2、在/root/.ssh目录下使用命令ssh-keygen -t rsa
生成公钥和私钥,输入命令后一直按回车即可
root@bpvank-virtual-machine:~/.ssh# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:DrGdLSo+dVV9Q0doMHWw6FINU9rdH3psE0iol6FqVvw root@bpvank-virtual-machine
The key's randomart image is:
+---[RSA 2048]----+
| **==+|
| ooO==+|
| . . o.*o+++|
| + B.= o +|
| o S.= .. =.|
| .B.. E o .|
| ..+.. |
| ... |
| .. |
+----[SHA256]-----+
3、生成的公钥和私钥文件如下:
root@bpvank-virtual-machine:~/.ssh# ls
id_rsa id_rsa.pub
二、添加公钥到你的Gitee帐户
1、查看生成的公钥
root@bpvank-virtual-machine:~/.ssh# cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDbXXDH792Z1Z382EQingiZWHjXWuds++v16HIT1uzA7dmpR41dGgmg9trGlJ/sbk8DPU83aAZZSy5kp9TaLUnFfxGOQU3NNMe78kRt2eOTaAnzoqhA8ZwyuXIgQVu7234N5HyLhmv0umzH/N+2Sdg0H9orBm+55BcrVzbsKSzPatZrGRlgNyOc2O/CLak0nn4/ivAglBgZJeODwEaTQAEOcdfdQpTvsJO+EdjOlRcfWWa4ylQl3zRBzSLklzfONo4y4AnCvDNuCTLzbSWnnG9Ti0v0b0aYwvqliFqGp67/EpQyjKfctoPgkGXMm5rfbhgo9Nmhi0++6Wi2kzLGu4Fh root@bpvank-virtual-machine
2、登录 Gitee,通过点击头像中的设置选项,进入后台,配置刚才生成的公钥文件里面的内容。
三、添加私钥到Jenkins的SSH凭证中并创建项目使用SSH凭证
1、查看生成的私钥
root@bpvank-virtual-machine:~/.ssh# cat id_rsa
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEA211wx+/dmdWd/NhEIp4ImVh411rnbPvr9ehyE9bswO3ZqUeN
XRoJoPbaxpSf7G5PAz1PN2gGWUsuZKfU2i1JxX8RjkFNzTTHu/JEbdnjk2gJ86Ko
......
LyKnjUsIBjC4uk98sJ29KwKBgDR8bjDXBwo3BusyZnWBGr33iZ0AC8GBMF1gV0ce
E8zAcEmOapDMQ12ap65b3lg4Rxj8gxKo8W4KBrbRwFaQ1iukNx4qg/m5MDd4IwnQ
0J2wXpZyN+at4ez5QJFn6a2JiKIVHmnNNuMFiB82jkrvi84B7HsH
-----END RSA PRIVATE KEY-----
2、在 Jenkins 中新建 SSH 凭证,我们刚才使用 root 用户来生成密钥,所以用户名填 root,ID可以不填保存后会自动生成,私钥填我们生成的私钥文件中的内容。
注意:复制私钥时头尾也需要复制添加进去!!!
-----BEGIN RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----
3、这样我们就可以在 Jenkins 中的项目配置 SSH 凭证了,注意这时需要使用项目的 SSH 地址了,如下所示:
①创建项目
②配置git信息,然后点击下方的应用/保存
③可能会报如下错误,报错了也点击下方的应用/保存
三、解决错误并在本地git仓库中创建一个与远程git仓库的关联
①进入git仓库目录
# 1、查看git仓库目录
root@bpvank-virtual-machine:~/.ssh# find ~ -name ".git" -type d
/root/.jenkins/workspace/java-project-01/.git
/root/.jenkins/workspace/java-project-02/.git
# 2、进入刚刚创建项目的git仓库目录
root@bpvank-virtual-machine:~/.ssh# cd /root/.jenkins/workspace/java-project-02/.git
# 3、ls查看
root@bpvank-virtual-machine:~/.jenkins/workspace/java-project-02/.git# ls
branches config description FETCH_HEAD HEAD hooks info objects refs
②使用git remote -v
查看你当前的remote url
root@bpvank-virtual-machine:~/.jenkins/workspace/java-project-02/.git# git remote -v
origin https://github.com/test1/test1.git (fetch)
origin https://github.com/test1/test1.git (push)
③使用git remote set-url
来修改你的remote url,不用https协议,改用git 协议
git remote set-url origin [email protected]:bpvank/jenkins-testing.git
④再次使用git remote -v
查看remote url
root@bpvank-virtual-machine:~/.jenkins/workspace/java-project-02/.git# git remote -v
origin [email protected]:bpvank/jenkins-testing.git (fetch)
origin [email protected]:bpvank/jenkins-testing.git (push)
⑤再运行命令/usr/bin/git ls-remote -h git地址
root@bpvank-virtual-machine:~/.jenkins/workspace/java-project-02/.git# /usr/bin/git ls-remote -h [email protected]:bpvank/jenkins-testing.git
The authenticity of host 'gitee.com (212.64.63.215)' can't be established.
ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrQp+KkGYoFgbVr17bmjey0Wc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitee.com,212.64.63.215' (ECDSA) to the list of known hosts.
3b671ae7df7898d9ad4315bd1354a5f00ab8c158 refs/heads/master
解释:这个命令通常用于在本地git仓库中创建一个与远程git仓库的关联,或者更新本地git仓库中远程引用的最新状态信息。
⑥成功解决,在项目中点击配置查看,会发现刚才的错误解决了
四、构建项目
①我们构建项目时,可以在控制台输出中看到下载的git源代码的位置和使用了刚才配置的 SSH 凭证。
②最后在Linux中,进入下载的git源代码的位置,查看文件是否拉取成功!
root@bpvank-virtual-machine:~# cd /root/.jenkins/workspace/java-project-02
root@bpvank-virtual-machine:~/.jenkins/workspace/java-project-02# ls
pom.xml README.md springmvctest.iml src