Jenkins - SSH认证方式拉取Git代码

1、本地生成密钥

[root@root ~] 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:
bf:1e:4d:b3:0f:fb:8b:71:cd:ef:d3:70:69:a4:46:ff root@node1.chinasoft.com
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| |
| . . |
| S + + .|
| . o =.*.|
| o * oo=|
| o B .E|
| .o o.+o+|
+-----------------+

2 、查看已生成的公钥

[root@root ~]  cat .ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC3vhfbA4NdkP8g1WVyYkw93f
1PZ04nWoyFnoerC9b40jIY5px+tkugdy/RZ3/bp4hMC5yNgV1S25Tm8Rpz
BJMp7pbJz8dO+LCKqUnXq9Eh0QhsZE0xlQN+J4awy9YIGiD2nFg7k/Zz
eAtQRzNryrrPnKaWpXtg7tMGGTWBjeyLAVuqCOMU7euY94G26UmVfjD
LIJkcJqB+8dwvodW3wpmUER32qVPKZLnSYSOCfoNed+P4Eujs5PBCLu
zFvGyiDhY2Pwrk/4S11jWUa7TJItfoPXzeGc/ujaJi/o24dt8VXeFa/Rm4wy
wYLNW3TRjXy1mPpdVlob3701MMQ0bf3qPv root@root.soft.com

3、将公钥加入Git-Project中

进入Project > Repository Settings > Deploy Keys中,加入第二步查看的公钥。Title自行设置,Key中输入公钥内容。

Deploy Keys.png

4、在Jenkins机器上验证能否正常拉取代码

[root@root~]   git clone ssh://git@git.com:9022/tester/appium.git
Cloning into 'appium'...
The authenticity of host '192.168.3.198 (192.168.3.198)' can't be established.
ECDSA key fingerprint is 9d:60:c3:8e:49:72:e8:9c:c0:d6:c8:d2:f1:b6:3a:74.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.3.198' (ECDSA) to the list of known hosts.
remote: Counting objects: 79, done.
remote: Compressing objects: 100% (79/79), done.
remote: Total 79 (delta 17), reused 0 (delta 0)
Receiving objects: 100% (79/79), 1.18 MiB | 0 bytes/s, done.
Resolving deltas: 100% (17/17), done.

5、查看已生成的私钥

root@root ~]   cat .ssh/id_rsa
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAt74X2wODXZD/INVlcmJMPd39T2dOJ1qMhZ6HqwvW+NIyGOac
frZLoHcv0Wd/26eITAucjYFdUtuU5vEacwSTKe6Wyc/HTviwiqlJ16vRIdEIbGRN
MZUDfieGsMvWCBog9pxYO5P2c3gLUEcza8q6z5ymlqV7YO7TBhk1gY3siwFbqgjj
FO3rmPeBtulJlX4wyyCZHCagfvHcL6HVt8KZlBEd9qlTymS50mEjgn6DXnfj+BLo
7OTwQi7sxbxsog4WNj8K5P+EtdY1lGu0ySLX6D183hnP7o2iYv6NuHbfFV3hWv0Z
uMMsGCzVt00Y18tZj6XVZaG9+9NTDENG396j7wIDAQABAoIBAEHMfCR9HJTsMMDk
SmDs3JqnHWhK+UzUe0/6VmEla0VNmI0cQFyMEYcUR3Z41uulEgURf22ZLv9WDPuq
yar4r26rtynsE1avbiEpwHzQVaMDhT2zqYUg9NA/fVdgl3PtT3KgyGQFd5MgIQUN
ileGkOF6GpoGBqnOvJkJfAS5+0RHi8EZ8RcufNExgan5QF42dtKNWOEdgZIp0+WY
jmI73YEpIadQzKxjL6PCOUmFAjfuNllw3It5QLePvYYlQeWKkl8QdROOADzOxgoj
-----END RSA PRIVATE KEY-----

6、配置Jenkins的认证

进入Jenkins>凭据>全局凭据>添加凭据,类型选择SSH Username with private key,Private Key中输入第5步中查看的私钥。

private key.png

7、配置工程中Git - Repositories

进入Jenkins>工程>配置>源码管理>Git,填写Repository URL:ssh://git@git.com:9022/tester/appium.git,Credentials选择第6步创建的新Credentials。完美结束。

Repositories.png


以上,对你有帮助的话,点赞❤️吧~~
欢迎关注我的简书,博客,TesterHome,Github~~~

posted @ 2018-09-20 13:00  小喜_ww  阅读(14762)  评论(0编辑  收藏  举报