git+github+jenkins:
架构图:

说明:jenkins知道github有更新了,就pull进行构建build,编译、自动化测试。然后部署到应用服务器。
maven java的项目构建工具。

在开发者电脑上创建空密码密钥对。

bash
[root@git-developer ~]# git clone git@github.com:changchunhua2017/jenkins.git
Cloning into 'jenkins'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
[root@git-developer ~]#
[root@git-developer ~]#
[root@git-developer ~]#
[root@git-developer ~]# ll
total 4
-rw-------. 1 root root 1518 Sep 2 02:39 anaconda-ks.cfg
drwxr-xr-x 3 root root 35 Sep 20 15:27 jenkins
bash
[root@git-developer jenkins]# git config --global user.name "changchunhua"
[root@git-developer jenkins]# git config --global user.email "chang_chunhua@qq.com"
[root@git-developer jenkins]# git config --global color.ui true
[root@git-developer jenkins]# echo 11111 > 1.php
[root@git-developer jenkins]# ll
total 8
-rw-r--r-- 1 root root 6 Sep 20 15:50 1.php
-rw-r--r-- 1 root root 35 Sep 20 15:27 README.md
[root@git-developer jenkins]# git add *
[root@git-developer jenkins]# git commit -m "Added 1.php"
[main cdc0119] Added 1.php
1 file changed, 1 insertion(+)
create mode 100644 1.php

可以看到1.php文件上传成功:

那开发者这块是没有问题的。
Nginx服务器的准备:
在Nginx服务器上安装nginx,并启动nginx。
bash
[root@nginx ~]# yum install epel-release
[root@nginx ~]# yum install nginx -y
[root@nginx ~]# systemctl start nginx
[root@nginx ~]# systemctl enable nginx
jenkins安装插件:
jenkins图形安装publish over ssh插件:


可以看到Publish Over SSH插件已经安装完成。

确认Git plugin插件已经安装完成。
配置Jenkins服务器通过Publish over ssh免密连接Nginx:
1)第1步: 在jenkins服务器上生成空密码密钥对:
bash
[root@localhost soft]# ssh-keygen
2)查看并复制私钥:
bash
[root@localhost .ssh]# cat id_rsa
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAqbt2nAw4gB3/3HGa06WZGfZZkccGXd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
EklJbX3tiSbL+Iv3yIKcWX298BZmTXl0R6ALyUlCfik7SBZvIIuKLTjdqyIfZE2k
IsArpCRLtgiSJpmK3E66ZIa3816fkYP2jaqXt5xw1BQZAUz8SICZSi0cS+vc0WrY
tChbbgsgHRHtyVL+1pw8fFkCgYEAnGd8XUZ66UahtI1b9hkXoAsc5+MOvQ2N8e68
Bjxux295KqcFI+lua9ViXyTy6eumgTgLumXO9rAK2DPsXVFromMHFC2bd4w4KxV7
8EIlLHCcHIq9DbV3sH/naOavC15OJoYHNf2EFqEt02azWWbvIYs0EHEoo6mPghEm
TFIyOFsCgYBwaSZ5bGqIQ+L+ZgJq1d6FkGzKvS8lrWQGNVds4Ne5iNrMUoeRe6O7
hwsqypAdLhgbF8SgrXxYMOhHBrSMdnaqaAJl5V0H5ydC3rkX/UsGa1pCWsendpVq
TZzTR0pdmNiNYTslQW9ILm9ZkWpaQmwGFFGpTmRqPuYUZHU+WByiGw==
-----END RSA PRIVATE KEY-----
这段私钥,只是代表这台服务器的root账户私人免密,并不能代表jenkins软件免密。
3)在Jenkins中添加私钥:
安装了插件,才有这个Publish over SSH。

还需要添加要连的服务器:

然后点击保存。
还可以做一个测试。

这样,就是配置jenkins软件的私钥。
私钥是解密,公钥是加密。
4)在jenkins服务器上配置对nginx服务器的免密登录。
bash
[root@localhost .ssh]# ssh-copy-id -i 192.168.17.121
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.17.121 (192.168.17.121)' can't be established.
ECDSA key fingerprint is SHA256:4dogA08L5gdfaA69BGekE13kzSuIP2vcRp9rsqYnpn0.
ECDSA key fingerprint is MD5:93:34:b9:61:03:cd:00:92:b9:91:3d:b6:6c:7a:84:95.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.17.121's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.17.121'"
and check to make sure that only the key(s) you wanted were added.
把Jenkins的服务器的公钥给Github:
直接将id_rsa.pub内容配置到Github中。

jenkins连接Github,私钥连接公钥。
为Jenkins服务器添加凭据:
凭据是Jenkins给自己的构建项目授权用的。
我们本项目案例里jenkins是通过ssh免密去找github的项目仓库clone或pull项目代码。
在前面的配置里已经把jenkins服务器的公钥添加到了github仓库上, 在jenkins服务器上root用户使用git clone 项目地址
都可以成功。
但是jenkins服务本身并不允许使用自己的私钥去访问github的公钥, 给个凭据, 让它能做就解决问题了
总结: ==凭据是一种和第三方程序(如github,gitlab等)通讯的权限授权。本案例的凭据就是允许使用ssh免密==。
你可以把这个凭据在jenkins里授予给任何需要ssh免密通讯的构建项目。
在Jenkins里添加凭据:






之前私钥是连接Nginx的私钥,这个私钥配置是连接前面的Github的私钥。
小结:
