Jenkins自动发布代码实战篇
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.Jenkins服务器配置秘钥对并上传到Gitlab中
1>.在Jenkins后端生成秘钥对
[root@jenkins ~]# ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa Generating public/private rsa key pair. Created directory '/root/.ssh'. 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: e1:75:1f:cf:18:02:5e:b7:16:99:14:75:56:73:ac:ad root@jenkins.yinzhengjie.org.cn The key's randomart image is: +--[ RSA 2048]----+ | . ..=BB| | . o .o+=| | . o o =o | | . o . +.*.| | S o.o| | E | | | | | | | +-----------------+ [root@jenkins ~]#
2>.查看服务端的公钥和私钥
[root@jenkins ~]# cat /root/.ssh/id_rsa.pub #查看公钥 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCwr6GMG0OYSWyLcraC4FGquTm9C+26a2/SdTysXOTiC6GpqYE9L57NgCSJSYWFalj+s/O6+LqHLY/ORi+LMmh4DtYSD4rUYo5NRd68B3lVs2JU8FqfCK/bJR+Sy/SxDAtKfrP/8gPM+4saB9JXUaljavhwIsyqMJxLkbwmDsdYmf4MHjMoHA4k8qECYKfvL7mep3Cglh0U4dQTubVvjmN/f6oKX7l7yVe+DoSImPyYae16+8AOe0v8+hiL5zB8eBBuCJZfXT/ZQbp5pAWxNCiiHsLI9VQhjsmsLS+bpgEvnCDqbVuXhrYXaLrQDm2EG2YwU1YSES6gJrvs5OkMbwXr root@jenkins.yinzhengjie.org.cn [root@jenkins ~]# [root@jenkins ~]# [root@jenkins ~]# cat /root/.ssh/id_rsa #查看私钥 -----BEGIN RSA PRIVATE KEY----- MIIEpAIBAAKCAQEAsK+hjBtDmElsi3K2guBRqrk5vQvtumtv0nU8rFzk4guhqamB PS+ezYAkiUmFhWpY/rPzuvi6hy2PzkYvizJoeA7WEg+K1GKOTUXevAd5VbNiVPBa nwiv2yUfksv0sQwLSn6z//I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BNVgyo33 PXuPOlSsQoEWChSVgJjq/QKBgQCA1zBXS+wNqyqEm/Ptd4O2y6qX6+nim5v3bMXa 5lv2WVl45RrbCa4dcmbv2jLUK0auFv7Pxzzs8OWtW6lT3R0LDojLqWKIH9VEL74q C6S5R3gUOFGnTNPnaqj2Gybph3ZFTH7aC4bGCe/C/5ZlmAM34jOZv+cWVilZaLl/ JMQq5wKBgQC41olS3GTRuPdNoZCI9rWaR30XafVv3EV7XmAnytNJGJv/VY00a1fk fXWIUTG/AYQ932qBZQ/PL9eobtuvrZxRj7Xt8p6O6ERRk4mqRERHF+E6yEFdzKef rXfkvpqaZ63aTLjeolEiVO/Vud6ZEcc6UkpBnajgN1e5vapXARL9pA== -----END RSA PRIVATE KEY----- [root@jenkins ~]#
3>.将Jenkins服务器端的公钥上传到GitLab中
二.Jenkins关联GitLab的WebUI界面配置
1>.创建新任务
2>.编辑任务名称
3>.源码管理选择git,并将git的地址填写为GitLab的地址
4>.将Jenkins服务器端的私钥上传到Jenkins的webUI中
5>.关联GitLab
三.小试牛刀-使用Jenkins执行shell命令
1>.在Jenkins服务端创建测试目录
[root@jenkins ~]# mkdir -p /home/yinzhengjie/data/jenkins/www [root@jenkins ~]# [root@jenkins ~]# chown -R jenkins.jenkins /home/yinzhengjie/data/jenkins/www/ [root@jenkins ~]# [root@jenkins ~]# ll -d /home/yinzhengjie/data/jenkins/www/ drwxr-xr-x 2 jenkins jenkins 6 Sep 9 08:04 /home/yinzhengjie/data/jenkins/www/ [root@jenkins ~]# [root@jenkins ~]# chmod +x /home/yinzhengjie/ -R #这个执行权限必须得加,因为Jenkins默认是没有访问yinzhengjie用户家目录的权限哟! [root@jenkins ~]# [root@jenkins ~]# ll /home/yinzhengjie/data/jenkins/www/ total 0 [root@jenkins ~]#
2>.构建选择-Execute shell
3>.自定义需要执行的shell命令(温馨提示:Jenkins默认的工作目录是:/var/lib/jenkins/workspace/,如果之前没有执行任务,workspace默认是不存在的,当执行第一个Jenkins任务时自动创建)
4>.运行任务
5>.Jenkins任务执行成功后查看服务器端测试目录是否有数据
6>.成功执行任务后,我们会发现在服务器端会生成“/var/lib/jenkins/workspace/”目录
四.Jenkins执行任务的具体步骤
1>.点击项目名称
2>.查看编译的历史
3>.查看控制台输出
4>.