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

1、生成本地密钥(没有的,用ssh-keygen生成)

[[email protected] ~]# 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:yt60CeJSGyUwcDtrddVS+xOnAr3WhvgERR4srsHzdAw [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|... .== |
| .o. .E+oo |
| oo....o=+ . . |
| +..= o+o= + |
| o o *S.* * |
| . o....+ o . |
| ..o+ . . |
| ...o + o |
| .. . + |
+----[SHA256]-----+

私钥如下

[[email protected] ~]# cat /root/.ssh/id_rsa
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAr6hcLM5HtOwvL4z28sYBpYJhgZpaGAbnlCOr5d+28nn93TB8
26H2lobJiWY6ahqbpTYb3A5JlJ1CAXeYtvSZRCP+Gy7W2YJZEeq1fpfTUKyP+sVT
qKjqQVRXlfnaZmcuzWtk08Nwq2LqHn007QJ4/pQKo70/+81wwRPf+A0WzmeCco3y
QFw/TCfM8WUIPjcEDnJBKrRug8/0KNmu+foEPk2S3kZs2VN8VmLEnUDCD10Olysl
cBshbpDMZ9rUob1mxVMVPQ6G/dMGkEyNbbXGcUvvhausTz/VyKrKS0NUCTcwYp69
8vm3YDmewCQCO2SL5Qc0EXRUL9IdsYf7ndKgawIDAQABAoIBABNDYFBdksmJeSc6
yz4WbsnKwYyn86dc4nqBavF7liGDNU7TaW3glh0tzN4vgspt/pGgnG+GdZVBpaJp
jirKKiCp3cz3VwJlZAUt+xdV6QuY0PVMPH9rfr0JVc2Rp9zBBgXq+BStAycYYtpT
GsG/Gqu4am5Ia8LeZbYWyuH19YFO8+0iuFLLZ+cY5qtPkBhdyNfn5cEVwLZkbiGA
boQSo7gYJ2vn2szI3YGJBo96IxXCidrSEj8r9U9u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c2EAAAADAQABAAABAQCvqFwszke07C8vjPbyxgGlgmGBmloYBueUI6vl37byef3dMHzbofaWhsmJZjpqGpulNhvcDkmUnUIBd5i29JlEI/4bLtbZglkR6rV+l9NQrI/6xVOoqOpBVFeV+dpmZy7Na2TTw3CrYuoefTTtAnj+lAqjvT/7zXDBE9/4DRbOZ4JyjfJAXD9MJ8zxZQg+NwQOckEqtG6Dz/Qo2a75+gQ+TZLeRmzZU3xWYsSdQMIPXQ6XKyVwGyFukMxn2tShvWbFUxU9Dob90waQTI1ttcZxS++Fq6xPP9XIqspLQ1QJNzBinr3y+bdgOZ7AJAI7ZIvlBzQRdFQv0h2xh/ud0qBr [email protected]

2、将公钥加入Git-Project中

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

3、去jenkins主机上查看是否额能拉取代码,我的是jmp主机

下面是部署前,提示要输入密码

[[email protected] ~]# git clone [email protected]:wangtao/hello2.git
Cloning into 'hello2'...
[email protected]'s password: 
Permission denied, please try again.
[email protected]'s password:

下面是部署后,一切OK了

[[email protected] ~]# git clone [email protected]:wangtao/hello2.git
Cloning into 'hello2'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.

4、配置Jenkins的认证

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

5、配置工程中Git – Repositories

进入Jenkins>工程>配置>源码管理>Git,填写Repository URL:[email protected]:wangtao/hello2.git,Credentials选择第4步创建的新Credentials。配置到此结束

此条目发表在jenkins分类目录,贴了标签。将固定链接加入收藏夹。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注