部署背景:
jenkins: CentOS 7.4C IP:172.16.3.74
gitlab-11.5.3: CentOS 7.4D IP:172.16.4.74
此上部署都是根据我之前的博客配置完成的;
jenkins有三种构建方式:
触发式构建:用于开发环境部署,开发人员可以push代码或者合并代码到gitlab项目的master分支上,jenkins就部署代码到对应服务器;
参数化构建:用于测试环境或者预上线环境部署,只需要在jenkins的界面传入对应参数即可执行部署;
定时构建:在参数化基础上添加,可以每天定时打包;
触发式构建部署过程:
1.首先,我们需要在jenkins主机上生成root用户的公钥和jenkins用户的公钥;
因为jenkins用户默认没有bash;所以我们需要给jenkins设定一个bash,用以输入命令;
[root@master testProject]# grep jenkins /etc/passwd jenkins:x:982:976:Jenkins Automation Server:/var/lib/jenkins:/bin/false [root@master ~]#usermod -s /bin/bash jenkins [root@master testProject]# grep jenkins /etc/passwd jenkins:x:982:976:Jenkins Automation Server:/var/lib/jenkins:/bin/bash
2.生成公钥:
[root@master ~]# ssh-keygen -t rsa [root@master ~]# cat .ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCq4LhWeHNqq34B7j2uL4Ss4EJWyZkWeNHrgsN/2IkaVPatLEJliDHGnPVnKQEejUmApjRofTzJ6eHryzr/7NYNYVoYY08gpVYer2Tct0EWp7V6IA+8OUrMYZYGXLVEBaj/SsTlp23sPZKYUfshGW4wFMBsTGtSNspsSl2MyBnNbO/VDNZUtkx18UwBocBdOLM0ogaT1Taa+/XOtTYs5tP1LWfZA/Wcbwih+9mZFZD55wsyUgIGEgQd9M88lhbHKzjTU9bVUPBOLwQVdcnsd5o/KduNidwY3+Pd+75hDXwE6o4xh5M0+pd6M+KyDMQLN8ZD51P1+91IDvp2W9uEQDCf root@
[root@master ~]# su - jenkins -bash-4.2$ ssh-keygen -t rsa -bash-4.2$ cat .ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6j4gWxtlwzuwEDBOHmuchiZgNRAdNTLHj+70TJY+lqTKM4YkYHvdD6ZXtmTafrHW5onMzzzdNr3xnsvuGVdxc7hzJ5KZ27bWAo4Ka2IhH2+KXaKxooqcVOsrHdzEOsoOM9fLgLzZgn674NRZky4lZTNxFb9Iu8nu3EgjIa2Juio/DM+IPmipy4u+qHWoJ2RJqVuNh+qiSrpXXnwYLEB28MoHbCZa2otHoIITAekYvTJfIyqfl5NT3IXG4+AaWhQ0sTsEw3qpB4Tgi3FTtVuVXvp0QrvCCN6UKS1/nIcaNU9KFak4bGjv7V7uE2MexWOjacLWuxByuF4/hLca08a0F jenkins@
3.把生成的公钥文件(.ssh/id_rsa.pub内容)复制到gitlab服务器上:
注意:我们需要在jenkins用户登录中对root用户进行免密认证,否则在我们进行触发式构建的时候,会执行不成功,出现 Host key verification failed错误,会让我们功亏一篑,因为jenkins默认是jenkins用户执行我们设置的命令,而我们脚本一旦有ssh root@HOST_IP的时候,那么jenkins就需要登录为root用户执行后边的命令,我们就让jenkins免密登录root即可解决;
[root@master ~]#登出 Connection to 172.16.3.74 closed. -bash-4.2$ ssh-copy-id -i .ssh/id_rsa.pub root@172.16.3.74 /bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".ssh/id_rsa.pub" /bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /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@172.16.3.74's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@172.16.3.74'" and check to make sure that only the key(s) you wanted were added. -bash-4.2$
4.找到我们创建的gitlab库,并下载至jenkins主机上;
下载过程;
[root@master ~]# git config --global ljy [root@master ~]# git config --global ljy@ [root@master ~]# git clone git@172.16.4.74:/root/testProject
5.选择git Repository的URL;
6.创建触发脚本并写入触发shell;
[root@master ~]#cd testProject/ [root@master testProject]#cat jenkins_trigger.sh #!/bin/bash # echo "test successful" [root@master testProject]#chmod +x jenkins_trigger.sh
7(可选).我们可以设置一个API token;
输入名称,会输出一个token,我们需要把token复制下来;
注意:如果我们是gitlab-10.6以后的版本,我们会无法创建,出现一个requests to the local network are not allowed的红色报警信息,这时我们需要允许gitlab访问本地网络,默认不允许访问本地网络;
8.出现一个HTTP 201的返回值即证明成功;
返回值为SUCCESS即为成功;
创建一个参数脚本;
[root@master ~]#cd testProject/ [root@master testProject]#vim jenkins_paramter.sh [root@master testProject]#cat jenkins_paramter.sh #!/bin/bash # git_pro(){ echo "pull $1 code" } git_pro $1 [root@master testProject]# [root@master testProject]#chmod +x jenkins_paramter.sh [root@master testProject]#
定时构建;
1.定时构建依据在参数化基础之上,选择一个临近的时间;
2.测试;
至此:三种构建方式已经部署完毕;作为公司常用工具,这个技能还是很必要的;