官网 jenkins.io
Jenkins是一个开源软件项目,提供超过1000个插件来支持构建、部署、自动化, 满足任何项目的需要,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能。
下载 jenkins:https://pkg.jenkins.io/redhat-stable/
下载JDK环境:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
安装JDK运行环境和jenkins服务上传JDK和jenkins rpm安装包,使用rpm ‐ivh进行安装,安装完JDK运维java测试是否安装成功
1 配置jenkins
[root@jenkins ~]#: vim /etc/sysconfig/jenkins
29 JENKINS_USER="jenkins
改为
29 JENKINS_USER="root"
启动jenkins
[root@jenkins ~]#: systemctl start jenkins
访问10.0.0.21:8080
1.1 正常情况下启动
这里也可以自己安装想要的插件
1.2 非正常情况启动(卡在了初始画面)
拔网线执行以下步骤:
1.3 重新设置密码
2 jenkins 家目录及文件说明
[root@jenkins ~]#: cd /var/lib/jenkins/
-rw-r--r-- 1 root root 1644 Apr 25 20:25 config.xml 配置文件
-rw-r--r-- 1 root root 156 Apr 25 20:14 hudson.model.UpdateCenter.xml jinkens前身就是hudson
-rw------- 1 root root 1712 Apr 25 20:14 identity.key.enc
-rw-r--r-- 1 root root 94 Apr 25 20:14 jenkins.CLI.xml
-rw-r--r-- 1 root root 7 Apr 25 20:25 jenkins.install.InstallUtil.lastExecVersion
-rw-r--r-- 1 root root 7 Apr 25 20:25 jenkins.install.UpgradeWizard.state
-rw-r--r-- 1 root root 171 Apr 25 20:14 jenkins.telemetry.Correlator.xml
drwxr-xr-x 2 root root 6 Apr 25 20:14 jobs
drwxr-xr-x 3 root root 19 Apr 25 20:14 logs
-rw-r--r-- 1 root root 907 Apr 25 20:14 nodeMonitors.xml
drwxr-xr-x 2 root root 6 Apr 25 20:14 nodes
drwxr-xr-x 2 root root 6 Apr 25 20:14 plugins
-rw-r--r-- 1 root root 64 Apr 25 20:14 secret.key
-rw-r--r-- 1 root root 0 Apr 25 20:14 secret.key.not-so-secret
drwx------ 4 root root 265 Apr 25 20:14 secrets
drwxr-xr-x 2 root root 24 Apr 25 20:14 userContent
drwxr-xr-x 3 root root 56 Apr 25 20:14 users 用户的信息
drwxr-xr-x 2 root root 6 Apr 25 20:50 workflow-libs
drwxr-xr-x 3 root root 18 Apr 26 09:23 workspace 构建项目的信息
2.1 忘记密码暴力修改
[root@jenkins admin_2107860204952557741]#: vim /var/lib/jenkins/users/admin_2107860204952557741/config.xml
33 <passwordHash>#jbcrypt:$2a$10$1y6UxV7tbrHAGcCSR67UpeYHckLjukRdmY5iae25mHsj/zcnDDpMe</passwordHash>
把哈希值替换成你知道的密码的哈希值
<passwordHash>#jbcrypt:$2a$10$DdaWzN64JgUtLdvxWIflcuQu2fgrrMSAMabF5TSrGK5nXitqK9ZMS</passwordHash>这个是密码位111111的Hash
解压缩文件包到plugins,在重启服务
[root@jenkins plugins]#: systemctl restart jenkins
3 构建jenkins项目
3.1 构架一个测试的项目
去gitlab管理端把之前的测试项目,拉取到jenkins下
来到jenkins端(需要提前做好免密钥并测试)
在去jenkins的家目录下面看看有没有数据
[root@jenkins jenkins]#: cd workspace/test/
[root@jenkins test]#: cat test.txt
第二次合并
4 代码上线
4.1 从码云上拉取一个真实的环境
码云就相当于国内的gitlab。
https://gitee.com/kangjie1209/monitor在这里拉取代码,
介绍:监控系统完整html页面,适合用来做监控类型.基于Bootstrap的完美后台管理框架,开箱即用
4.2 在准备一台新机器,简单配置nginx
[root@nginx01 html]# vim /etc/nginx/conf.d/test.conf
server{
listen 80;
server_name 10.0.0.7;
location / {
root /html/test;
index index.html;
}
}
[root@nginx01 html]# vim /html/test/index.html
111
[root@nginx01 html]# curl -I 127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Fri, 26 Apr 2019 01:52:35 GMT
Content-Type: text/html; charset=utf-8,gbk
Content-Length: 4
Last-Modified: Fri, 26 Apr 2019 01:49:59 GMT
Connection: keep-alive
ETag: "5cc263c7-4"
Accept-Ranges: bytes
[root@nginx01 html]# mv /html/test/index.html /tmp/
4.3 拉取代码
[root@nginx01 test]# git clone https://gitee.com/kangjie1209/monitor.git
Cloning into 'monitor'...
remote: Enumerating objects: 435, done.
remote: Counting objects: 100% (435/435), done.
remote: Compressing objects: 100% (381/381), done.
remote: Total 435 (delta 54), reused 390 (delta 44)
Receiving objects: 100% (435/435), 8.78 MiB | 574.00 KiB/s, done.
Resolving deltas: 100% (54/54), done.
[root@nginx01 test]# mv monitor/* .
这样就完成里一次代码上线
我们把源码从码云上拉取到gitlab上托管起来,然后jenkins在去gitlab上获取代码,再使用jenkins推送到站点目录下面,完成自动化上线。
4.4
复制刚才的连接,去gitlab上面新建一个项目
4.5 jenkins命令行测试
[root@jenkins tmp]#: git clone git@10.0.0.20:test/monitor.git
Cloning into 'monitor'...
remote: Enumerating objects: 435, done.
remote: Counting objects: 100% (435/435), done.
remote: Compressing objects: 100% (372/372), done.
remote: Total 435 (delta 53), reused 435 (delta 53)
Receiving objects: 100% (435/435), 8.78 MiB | 3.87 MiB/s, done.
Resolving deltas: 100% (53/53), done.
4.5 jenkins构建monitor项目
4.6 自动上线
发送公钥到nginx服务器
[root@jenkins ~]#: ssh-copy-id -i .ssh/id_rsa.pub 10.0.0.7
编写脚本
#!/bin/bash
CODE_DIR="/var/lib/jenkins/workspace/monitor"
TIME=`date +%F-%H-%M-%S`
WEB1=10.0.0.7
CODE_TAR(){
cd $CODE_DIR
tar zcf /opt/web-${TIME}.tar.gz ./*
}
CODE_CP(){
scp /opt/web-${TIME}.tar.gz $WEB1:/html/
}
CODE_XF(){
ssh $WEB1 " cd /html/ && mkdir web-$TIME"
ssh $WEB1 " cd /html/ && tar xf web-${TIME}.tar.gz -C web-$TIME "
}
CODE_LN(){
ssh $WEB1 "cd /html/ && rm -rf web-${TIME}.tar.gz"
ssh $WEB1 "cd /html/ && rm -rf www && ln -s web-$TIME www"
}
main(){
CODE_TAR
CODE_CP
CODE_XF
CODE_LN
}
main
手动测试成功后配置jenkins
5 代码半自动上线
jenkins修改代码推送到gitlab
[root@jenkins monitor]#: vim /tmp/monitor/index.html
10 <title>测试测试</title>
43 <a class="logo pull-left" href="index.html" style="width: 233px">测试测试</a>
[root@jenkins monitor]#: git commit -am 'index'
[root@jenkins monitor]#: git push -u origin master
推送前删除分支保护
6 代码自动上线
Jenkins构建触发器
Gitlab添加钩子信息
报错的解决办法
结果检查
继续修改index.html文件模拟测试,
[root@jenkins monitor]#: vim /tmp/monitor/index.html
43 <a class="logo pull-left" href="index.html" style="width: 233px">脚本自动化上线测试</a>
[root@jenkins monitor]#: git commit -am 'index3'
[root@jenkins monitor]#: git push -u origin master