Jenkins是一个开源软件项目,是基于Java开发的一种持续集成的工具,用于监控持续重复的工作, 旨在提供一个开放易用的平台,使软件的持续集成变成可能,是持续集成的核心,可以与其他软件进行协作,例如,gitlab推送给测试环境代码,可以通过Jenkins省略步骤,实现自动推送,再例如Jenkins可以通过tag传递参数实现发布和代码回滚操作,再例如Jenkins还可以推送代码给SonarQube进行代码质量检测,等等流程,都需要Jenkins的参与。
目录
Jenkins安装部署
Jenkins修改密码
Jenkins插件安装
Jenkins创建freestyle-job项目
一、创建freestyle-job项目
二、Gitlab上传代码
三、Jenkins端配置从git获取代码
四、手动构建获取代码
五、拉取代码到jenkins
六、配置构建步骤
七、配置自动触发构建
八、测试jenkins触发
九、配置jenkins返回构建状态到gitlab
Jenkins安装部署
关闭selinux和防火墙,安装JDK运行环境和Jenkins服务,下载链接放在文末
[root@Jenkins ~]# rpm -ivh jdk-8u181-linux-x64.rpm
[root@Jenkins ~]# rpm -ivh jenkins-2.176.1-1.1.noarch.rpm
[root@Jenkins ~]# cat /etc/sysconfig/jenkins|grep JENKINS_USER
JENKINS_USER="root"
[root@Jenkins ~]# systemctl start jenkins
[root@Jenkins ~]# systemctl enable jenkins
jenkins.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig jenkins on
[root@Jenkins ~]# /sbin/chkconfig jenkins on #设置开机自启动
访问 http://10.0.0.201:8080
[root@Jenkins ~]# cat /var/lib/jenkins/secrets/initialAdminPassword
c8f3b50a4ed747dea3a5ccc9d1c17f15
Jenkins修改密码
我们修改密码为固定值,不能每次都去获取,太麻烦了
再次登录使用设置的密码即可
Jenkins插件安装
此处我们跳过安装插件,直接上传插件至目录,插件下载链接放在文末
#jobs为每次构建后构建的结果目录,plugins为插件目录
[root@Jenkins ~]# cd /var/lib/jenkins/plugins/
[root@Jenkins plugins]# tar xf jenkins_plugins.tar.gz
[root@Jenkins plugins]# rm -rf jenkins_plugins.tar.gz
[root@Jenkins plugins]# mv plugins/* .
[root@Jenkins plugins]# rm -rf plugins/
[root@Jenkins plugins]# systemctl restart jenkins
/usr/lib/jenkins/:jenkins安装目录,WAR包会放在这里
/etc/sysconfig/jenkins:jenkins配置文件,“端口”,“JENKINS_HOME”等都可以在这里配置
/var/lib/jenkins/:默认的JENKINS_HOME
/var/log/jenkins/jenkins.log:Jenkins日志文件
在系统管理,插件管理,可以看到安装的插件
Jenkins创建freestyle-job项目
一、创建freestyle-job项目
下拉确定
丢弃旧的构建
执行一条shell命令、查看运行的当前路径,构建后的产物储存在/var/lib/jenkins/workspace/
二、Gitlab上传代码
Jenkins远程仓库上传小霸王游戏的源码,下载链接放到文末
[root@Gitlab ~]# mkdir git_xiaobawang
[root@Gitlab ~]# cd git_xiaobawang
[root@Gitlab git_xiaobawang]# rz -E
rz waiting to receive.
[root@Gitlab git_xiaobawang]# unzip 小霸王_FC怀旧游戏机-HTML源码.zip
[root@Gitlab git_xiaobawang]# rm -rf 小霸王_FC怀旧游戏机-HTML源码.zip
[root@Gitlab git_xiaobawang]# mv 小霸王\ FC怀旧游戏机-HTML源码/* .
[root@Gitlab git_xiaobawang]# rm -rf 小霸王\ FC怀旧游戏机-HTML源码/
[root@Gitlab git_xiaobawang]# ls
bgm.mp3 images js shuoming.html
css index.html roms
[root@Gitlab git_xiaobawang]# git init
Initialized empty Git repository in /root/git_xiaobawang/.git/
[root@Gitlab git_xiaobawang]# git add .
[root@Gitlab git_xiaobawang]# git commit -m "create xiaobawang"
[root@Gitlab git_xiaobawang]# git remote add origin git@10.0.0.200:root/xiaobawang.git
[root@Gitlab git_xiaobawang]# git push -u origin masterCounting objects: 99, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (97/97), done.
Writing objects: 100% (99/99), 7.49 MiB | 3.54 MiB/s, done.
Total 99 (delta 3), reused 0 (delta 0)
To git@10.0.0.200:root/xiaobawang.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
三、Jenkins端配置从git获取代码
在源码管理,输入git的url,报红色,提示我们需要认证,所以我们坐下jenkins服务器到git的免密钥
[root@Jenkins plugins]# ssh-keygen
[root@Jenkins plugins]# cat ~/.ssh/id_rsa.pub #复制到gitlab上
[root@Jenkins plugins]# ssh -T git@10.0.0.200 #测试是否能连接,输入一次yes
The authenticity of host '10.0.0.200 (10.0.0.200)' can't be established.
ECDSA key fingerprint is SHA256:zQvI/tCFYssR7l6cr90EtaIA93FXJp8FmUhGtkZshlA.
ECDSA key fingerprint is MD5:0b:a1:ee:d2:75:92:1a:62:05:63:5e:d1:e8:42:13:84.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.200' (ECDSA) to the list of known hosts.
Welcome to GitLab, Administrator!
点击保存再次打开配置的源码管理,发现没有问题
四、手动构建获取代码
执行完成后点击可以看控制台输出,我们的构建后执行的shell也会在此处显示
五、拉取代码到jenkins
写一个脚本把从git仓库里获取的代码上传到web服务器站点目录下
可以写到jenkins固定位置,也可以写到gitlab,推送的时候一起推送过去,注意在gitlab写,需要提交至远程仓库
web端先配置好nginx,创建好代码目录,方便脚本推送
[root@Web01 ~]# mkdir -p /code/xiaobawang #web端需要先创建好
[root@Web01 code]# cat /etc/nginx/conf.d/xiaobawang.conf
server {
listen 80;
server_name game.koten.com;
root /code/xiaobawang/game;
index index.php index.html index.htm;
location ~\.php$ {
root /code/xiaobawang/game;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@Web01 code]# systemctl restart nginnx
Failed to restart nginnx.service: Unit not found.
[root@Web01 code]# systemctl restart nginx
#windows修改本地hosts
10.0.0.7 game.koten.com
可以在此处查看环境变量,将环境变量与脚本结合
脚本操作
[root@Jenkins plugins]# ssh-copy-id -i ~/.ssh/id_rsa.pub 10.0.0.7 #此脚本需要推送内容,所以需要先做免密钥
[root@Gitlab git_xiaobawang]# cat git_to_web.sh
#!/bin/sh
DATE=$(date +%Y-%m-%d-%H-%M-%S)
CODE_DIR="/var/lib/jenkins/workspace/freestyle-job"
WEB_DIR="/code/xiaobawang"
get_code_tar(){
cd $CODE_DIR && tar zcf /opt/xiaobawang_${BUILD_ID}.tar.gz ./*
}
scp_code_web(){
scp /opt/xiaobawang_${BUILD_ID}.tar.gz 10.0.0.7:$WEB_DIR
}
code_tarxf(){
ssh 10.0.0.7 "cd $WEB_DIR &&mkdir xiaobawang_${BUILD_ID} && tar xf xiaobawang_${BUILD_ID}.tar.gz -C xiaobawang_${BUILD_ID}"
}
ln_html(){
ssh 10.0.0.7 "cd $WEB_DIR && rm -rf game && ln -s xiaobawang_${BUILD_ID} game"
}
main(){
get_code_tar;
scp_code_web;
code_tarxf;
ln_html;
}
main
[root@Gitlab git_xiaobawang]# git add .
[root@Gitlab git_xiaobawang]# git commit -m "create git_to_web.sh"
[master 71cb483] create git_to_web.sh
1 file changed, 29 insertions(+)
create mode 100644 git_to_web.sh
[root@Gitlab git_xiaobawang]# git push -u origin master
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 567 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To git@10.0.0.200:root/xiaobawang.git
9ff9bc1..71cb483 master -> master
Branch master set up to track remote branch master from origin.
六、配置构建步骤
使用jenkins调用部署脚本测试(此处写脚本全路径名称)
sh /var/lib/jenkins/workspace/freestyle-job/git_to_web.sh
立即构建
查看推送文件
[root@Web01 xiaobawang]# ll
total 7884
lrwxrwxrwx 1 root root 13 May 23 14:31 game -> xiaobawang_10
drwxr-xr-x 6 root root 128 May 23 14:31 xiaobawang_10
-rw-r--r-- 1 root root 8072532 May 23 14:31 xiaobawang_10.tar.gz
windows访问game.koten.com,访问正常
七、配置自动触发构建
需要设置安全令牌Secret token,勾选,生成令牌
生成令牌,复制到gitlab
在设置、集成的位置,输入jenkins地址和安全令牌
增加事件进行测试,显示200,没有问题
八、测试jenkins触发
克隆代码到master上更改代码进行推送测试是否自动触发,实现自动推送,方便开发在测试环境进行测试
#如果本地仓库与远程仓库不一致需要先clone
[root@Gitlab git_xiaobawang]# git clone git@10.0.0.200:root/xiaobawang.git
[root@Gitlab git_xiaobawang]# cat index.html|grep 超级魂斗罗 #超级魂斗罗v1.1改为超级魂斗罗v1.2
['超级魂斗罗v1.2', 'roms/cjhdl.nes'],
[root@Gitlab git_xiaobawang]# git add .
[root@Gitlab git_xiaobawang]# git commit -m "modify index.html"
[master b2ccba6] create git_to_web.sh
1 file changed, 2 insertions(+), 2 deletions(-)
[root@Gitlab git_xiaobawang]# git push -u origin masterCounting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 296 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To git@10.0.0.200:root/xiaobawang.git
b45a728..b2ccba6 master -> master
Branch master set up to track remote branch master from origin.
自动拉取的jenkins列表里显示字
九、配置jenkins返回构建状态到gitlab
系统管理-系统设置选项下,name和url直接天,认证方式需要去添加
先进入gitlab点击用户设置找到访问令牌Access Tokens
复制到记事本里防止丢失
添加好后test测试,没有问题就保存选中
设置项目中的构建后操作。然后进行构建测试返回结果
提交远程仓库测试,也可以直接在jenkins里点立即构建进行测试
[root@Gitlab git_xiaobawang]# cat index.html |grep 1.3
['超级魂斗罗v1.3', 'roms/cjhdl.nes'],
[root@Gitlab git_xiaobawang]# git add .
[root@Gitlab git_xiaobawang]# git commit -m "v1.3"
[master 5419cba] v1.3
1 file changed, 1 insertion(+), 1 deletion(-)
[root@Gitlab git_xiaobawang]# git push -u origin masterCounting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 279 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To git@10.0.0.200:root/xiaobawang.git
97f9a0d..5419cba master -> master
Branch master set up to track remote branch master from origin.
得到反馈
JDK下载链接:https://pan.baidu.com/s/1sGftvjyOtGI4ji4zRMFi1Q?pwd=nlyo
Jenkins安装包下载链接:https://pan.baidu.com/s/1k9uv3FE8BwOd3eNjik4O4w?pwd=owom
Jenkins插件下载链接:https://pan.baidu.com/s/1MoMyeNVykLakPsuQy3xg9w?pwd=dib1
小霸王游戏项目下载链接:https://pan.baidu.com/s/1eDZoMkwq87gnmQ5Q5OyKJA?pwd=3vhb
我是koten,10年运维经验,持续分享运维干货,感谢大家的阅读和关注!