Centos7搭建jenkins环境二:github + jenkins持续集成

背景

接上篇Centos7搭建jenkins环境一,这里接着搭建jenkins + github完成自动化环境搭建任务。

1、登录jenkins

因作者换了一台设备,这里采用新ip地址http://192.168.1.25:8080进行登录,用户名和密码同上一篇

login jenkins.png

2、创建一个新任务
create mission.png
3、输入任务名称,选择自由风格
create mission.png
4、general选项,选择github项目,使用自定义工作空间

github项目随便选择一个 https://github.com/YangshanL/gitskills.git/
自定义工作空间,需要提前创建该目录 /opt/jenkins_data

[root@localhost ~]# mkdir /opt/jenkins_data
[root@localhost ~]# cd /opt/jenkins_data
[root@localhost jenkins_data]#
general 1.png
5、源码管理

源码管理这里填入github项目地址 https://github.com/YangshanL/gitskills.git/ ,提示如下错误:

Failed to connect to repository : Error performing command: git ls-remote -h https://github.com/YangshanL/gitskills.git HEAD
源码管理—没有凭证.png

就是服务器连接github连不上,原因有二:
1、因为没有凭证,所以要添加凭证
2、压根就没装git
只有这2点同时满足,红色报错就会消失

6、添加凭证

添加凭证前,先生成秘钥:

[root@localhost jenkins_data]# ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
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:Ca/OzbuBT0pOStfWjo+DBM2m8e8X0CvCwkR9qm74hhk [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|     .           |
|    . . .        |
|   . o.o .       |
|    + =o...      |
|   o O  S. .     |
|  E = =+..o      |
|   *.o*+=...     |
|  +.+O.Bo=.      |
|   +o =.X*o      |
+----[SHA256]-----+
[root@localhost jenkins_data]#

生成公钥和秘钥

[root@localhost jenkins_data]# cd /root/.ssh
[root@localhost .ssh]# ll
total 8
-rw-------. 1 root root 1679 Jun 11 05:29 id_rsa
-rw-r--r--. 1 root root  395 Jun 11 05:29 id_rsa.pub
[root@localhost .ssh]#

这里可以看到,公钥权限可能过高不安全,将公钥权限修改为600

[root@localhost .ssh]# chmod 600 id_rsa.pub
[root@localhost .ssh]# ll
total 8
-rw-------. 1 root root 1679 Jun 11 05:29 id_rsa
-rw-------. 1 root root  395 Jun 11 05:29 id_rsa.pub
[root@localhost .ssh]# cat id_rsa
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAlz42qtEUXkISUTQHD5vKFEupkyYDoc95dIXqu7xRi199j5C0
jQStWg8HLGbdqdb2pu3fZVEQe4B6ckexqnJBI8BZidOlGF7pHf4bdGNY2oASchQ4
GPOIaV637XiPBX4M41JsT5lmSsAGjZpPRavqmvymNNK/Y+opAAhnl6wnUdvU7/XW
Gav/NEFmSiTJ1V3zh0PcfjL6VFh04Bl3nEcg74lHEDo2Jn8uOkPRMk5nbk+7WgOD
oXEckBCe1JRBpff34KdS6D8RJOlJSk1vK1tU6k7hqk+YzFlgpkc546Aw4ZB1Tp8d
XpaoKdQs7ZZl5RLijP2v+YGm3IV0L4NeCGdnEwIDAQABAoIBAEVqMW7+3QlR69RH
O8JnLz1XaLH3rY0Ti1v2psCZcxezsQ42TRe4ANQnt5QidjQgTD5bSL/Yj+cPFaPj
0eA35jvBaZb/g3OHBBlW6kTl3/8Q7uZa71BMXdx2s5YTUhkkPEgNKVbzQeam8ZsG
KsIAYIHX2XS/EUaLT1wLf3g2dCl2NkmgUMQht1ztBLHNQCaW14JxJytP8CxuxjeK
4MQhgb9IASRpiHF1HaQB5gB8jOxAVXTYcBUQpiEsLiAa+eTqQXyPzdIkfUY93rVo
QrHE5ZBfTIg8ZjbfVFZP5DRfrJEQrPLgo43ZzuyirSfBvtpqtY4Cu8SRlrTw+pqC
Pcs/o0kCgYEAxi8WZerX+/NaENB0vtLG4BFn8NK2udKYcocFF54GkgXmdtPtpc7V
TZb5caBEzApHJq7xf5W64F0Mh/+efVCSqWC1hAJOT481PrQvPWxyyUwlc1Sw7y7c
AUt15Vl3iC/80QgyH9Q/oZNEdESst+qHdcWDDhk0y3bojQ7Uv/KOrLUCgYEAw11z
nCne5wjL4YHOu9n1hVe1LqNqhQQ/mXCNSXkjmLxtl01tAjomxkDBR4G+MDBaX4h1
FWMAjQVAB3ETMpxKWWDX2uqERb6hjlmhjzzqgjGmFS+/K6Jcoz/aaDBrszLHadwl
FiH+MvxfWNcV3PUk19CgNb4KbUuOHV9DpCQU6acCgYEAhOVQhLltaiNukvXmQ4v+
FoU1iRUTuRoSZkJKVOMADu1r9OT6aISBHs2NqfdKCvUdb0BC60ZRkWB0JcJw2iGs
p24ValdNgiZLEygqwWq2smugdW0mVM28dBXREiHjeOyNAPMZAaUHomapsbnXhGD9
wyjWUB7w+ztaiDSYz7psmD0CgYEAv/UwxT524DdKbLdo9DcDARPe2IaFAVHdU9y9
VnYKveZXIBVZiJiuxJmXz7TxtNOt2LRxpqPyRJ3l/eni3RWoEA4Rye4oLYdTolT7
Y4yNN4OlFqglhLAxqc3xwfHhwN+iBKfkYMzhwiMrZFViDAtrOgqpgGTMRfu5Fa8m
/T7etX0CgYAQj+IsfglL3Fo86j9WiYty77+CPQ5ufs7oZViXGbbDagNpGRVKlVDX
XOd1GMVmp0NpCnIti6cVEzWXj1LAus5lRFJkYg4c37i4K4/q3XfvJTUNndNLDgwG
0fYh4ehu5bL+HGMVslp7C5t1cg66u3A9mDeP3AHdhS1j/K6Ab1GzDQ==
-----END RSA PRIVATE KEY-----
[root@localhost .ssh]#

回到jenkins配置界面,【类型】选择ssh username with private key,【username】填生成秘钥的[email protected],【private key】选择【enter directly】并填写秘钥全部内容,包括首尾注释

添加凭证.png

如图示点击【添加】按钮,选择jenkins(选择后依然显示红色)


image.png
7、安装git

我们在服务器上运行git命令,发现没有该命令

[root@localhost .ssh]# git
-bash: git: command not found
[root@localhost .ssh]#

通过yum安装

yum install git -y

这里安装后,再到页面去看就不会显示红色了


源码管理—正常色.png
8、构建触发器

这里我们选择最简单的定时构建,【日程表】中的数值代表每15分钟构建一次,点击旁边的?号,有相应说明。


构建触发器.png
9、构建

点击【增加构建步骤】-【执行shell】,输入框中输入以下内容
sh /opt/jenkins_data/hello_jenkins.sh,即每次构建我们这里都运行一次hello_jenkins.sh脚本


构建.png
10、立即构建

【保存】配置内容后,回到首页,点击【立即构建】,不久,久看到下方【build history】中新增一条构建记录,红色代表失败


立即构建
11、查看构建失败

点击构建任务的小三角-【控制台输出】,查看构建失败原因


查看构建失败
构建失败原因.png

可以看到我们这里是使用git clone代码失败,回到服务器验证如下:

[root@localhost jenkins_data]# ls
[root@localhost jenkins_data]#
[root@localhost jenkins_data]# pwd
/opt/jenkins_data
[root@localhost jenkins_data]# git clone https://github.com/YangshanL/gitskills.git
Cloning into 'gitskills'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 22 (delta 0), reused 1 (delta 0), pack-reused 19
Unpacking objects: 100% (22/22), done.
[root@localhost jenkins_data]# ls
gitskills
[root@localhost jenkins_data]#

可以看到,我们以root身份登录可以将代码clone下来,为什么还会报错?
通过查看/etc/passwd我们知道,jenkins这里自己单独创了一个名为jenkins的用户,但是/home目录下没有相应家目录,说明jenkins目录不能单独登录,但是相关进程却是用的jenkins用户权限来运行

[root@localhost home]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
libstoragemgmt:x:998:997:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
jenkins:x:996:994:Jenkins Automation Server:/var/lib/jenkins:/bin/false
[root@localhost home]# ls
[root@localhost home]#

前面我们自己定义了工作空间,查看工作空间的文件权限可知,jenkins用户并没有读写权限,所以出现相应问题,这里我们修改文件夹/opt/jenkins_data的用户与用户组权限为jenkins即可

[root@localhost jenkins_data]# pwd
/opt/jenkins_data
[root@localhost jenkins_data]# ll -d
drwxr-xr-x. 3 root root 23 Jun 11 06:09 .
[root@localhost jenkins_data]#

修改权限

[root@localhost jenkins_data]# chown jenkins:jenkins ./
[root@localhost jenkins_data]# ll -d
drwxr-xr-x. 3 jenkins jenkins 23 Jun 11 06:09 .
[root@localhost jenkins_data]#

删除试验代码

[root@localhost jenkins_data]# ls
gitskills
[root@localhost jenkins_data]# rm -rf gitskills/
[root@localhost jenkins_data]#
12、再次构建

回到jenkins任务页,点击【立即构建】,一会后,终于在第4次看到蓝色的图标了


再次构建成功
13、查看构建效果

先看下整个代码的内容
我这里使用shell脚本调用Python脚本,Python的结果输出在script.log中,无论是shell还是Python都打印了一句hello jenkins

[root@localhost jenkins_data]# ls
hello_jenkins.py  hello_jenkins.sh  README.md  script.log
[root@localhost jenkins_data]# cat hello_jenkins.sh
#!/usr/bin/bash

echo "hello jenkins from bash shell!"
python hello_jenkins.py
[root@localhost jenkins_data]# cat hello_jenkins.py
#!/usr/bin/python
import time

print('hello jenkins from python!')
file = r'/opt/jenkins_data/script.log'
with open(file,'a') as fi:
    fi.write(str(time.time()))
[root@localhost jenkins_data]# cat script.log
1560248433.86
[root@localhost jenkins_data]#

控制台输出结果,在输出最后可以看到,分别来自shell和Python脚本的一句hello jenkins都打印出来了,说明2个脚本都成功运行了。


构建成功结果输出.png

说明

如果是真正的CI过程,这两个脚本就应该是对应的卸载、安装脚本,脚本能pass,说明我们github + jenkins 整个搭建就没问题了。后续我们可以设置为只要github上的脚本一有变动,或者时间一到,jenkins就会自动去迭代测试产品。所谓CI/CD就是这么个样子。

你可能感兴趣的:(Centos7搭建jenkins环境二:github + jenkins持续集成)