Jenkins 使用
文章目录
- Jenkins 使用
- 一、jenkins 任务执行
- 二、 Jenkins 连接gitee
- 三、Jenkins 部署静态网站
一、jenkins 任务执行
jenkins 创建 job
job的名字最好是有意义的
restart_web_backend
restart_web_mysql
[root@jenkins ~]# ls /var/lib/jenkins/
config.xml nodeMonitors.xml
hudson.model.UpdateCenter.xml nodes
hudson.plugins.git.GitTool.xml plugins
identity.key.enc queue.xml.bak
jenkins.install.InstallUtil.lastExecVersion secret.key
jenkins.install.UpgradeWizard.state secret.key.not-so-secret
jenkins.model.JenkinsLocationConfiguration.xml secrets
jenkins.telemetry.Correlator.xml updates
jobs userContent
logs users
[root@jenkins ~]# ls /var/lib/jenkins/jobs/
guan_jenkins_job
[root@jenkins ~]# ls /var/lib/jenkins/jobs/guan_jenkins_job/
builds config.xml
[root@jenkins ~]# ls /var/lib/jenkins/workspace/guan_jenkins_job
guan12319.txt
清理工作空间之后,可以发现workspace目录下没有内容了
[root@jenkins ~]# ls /var/lib/jenkins/workspace/
二、 Jenkins 连接gitee
ssh协议连接方式
获取仓库地址
git@gitee.com:sound-of-birds-chirpingg/test_git.git
目的:用户在jenkins服务器上去Gitee(码云)下载代码,然后把代码下载到jenkins服务器上
常见报错
jenkins 没有安装git
yum install -y git
没有做免密
用ssh-keygen 生成公私要,然后上传公钥到gitee上,进行免密登录
[root@jenkins ~]# cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC6ATb8WL+zTOhYNkwJKr+xfaZ5QbfLBxyaRYtcO977T6ySDv1MDmBXC0ELBOE0x/Pyac3u5IB+NjLsKM63iYzSXrqk0TzjgrCXM85ZdnJ3mqCwaOa0xt2xFYeu0Pxz5o6CO+bffgfoInEIBHn44UaiJLM1qUdyoIxm8vKEaWF5ZZeDvyl8s952du0NgAQeRr70aISqRFTfZM5rYtkJeQ1r/3O0fuv5VbbklJprOid+viXwPIATLnste4TMFeN0dWW752WSfYpxR/XbPWLeZ0rTu3frIhLCs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aZmKNwU69wZdXXuoNA7Oqnt/W/qey91l
DCe0pp2vlsrWARnrRwkq747JDuQZgfJ8FR7VQ0PwnShYddPhgsAAAAwQDanDLFQGQlAEGf
9Xaa0hvpDy8rAgpW02t0j/uwedmrrjKa9l3MHjEb42iO3FSsWSVUnGNjdDAXlubGWpfG6Q
VJDuwHYujppIjq2GCipmrS5E7eriLQyMR3009DEF4AJvWvLyoVs951O9ThC/VKZXfiaMAP
67aDyxQg0cLCeAJHQJW4yc6hgp1uEhru5ViRCOxsag6z1s4q0Y8lPcHbInzCtRCEPC5+8G
C/K1JTRbcKznwGUciQ6IGYqbC0gFE9OdEAAADBANnRZTXzS7Ml17HmxJylS0XRmKpBC64Y
vEay8AUdMLfJ/Z1SKq54tD8T2AFQHRTlANd7jmpWjxebJ6AuzlCGIgFvMaWzG5wE5RuMCj
/yhPgTS6HrRNIzM0BBjQQG2P1sgUPeaWhQcD5NrIzUmL3QA2ZqohEzNSmXNHLWXbHd2VQf
DMbIUyjjdQw3eIADpuLpOx7RnI9JhOE1ns84mKE7OIfAaDssqbsdl2UsiSprq8V4/vjNpr
4u5QnBxUrmz2pDIQAAAAxyb290QGplbmtpbnMBAgMEBQ==
-----END OPENSSH PRIVATE KEY-----
- 在jenkins服务器上配置好git,生成公私钥发给gitlab
- 在jenkins服务器里做好配置,和凭证,写入私钥
Https 协议连接方式
1.获取仓库地址
https://gitee.com/sound-of-birds-chirpingg/test_git.git
2.安装gitee插件,并在安装完成之后重启jenkins服务
添加凭据
此时,gitee仓库更新内容,而jenkins还是之前的内容,没有同步
这个时候,只需要在jenkins 服务器上在构建一次,就完成了代码仓库的同步更新
[root@jenkins guan_jenkins_job]# ls
git.txt guan12319.txt guan.txt hello.txt
[root@jenkins guan_jenkins_job]# ls
git.txt guan12319.txt guan.txt hello.txt README.md
[root@jenkins guan_jenkins_job]#
三、Jenkins 部署静态网站
自动化部署静态站点
1.手动或自动部署一个nginx集群
2.开发提交代码到代码托管平台,gitee
3.运维开发shell脚本,交给jenkins去执行这个脚本,jenkins再去获取代码,推送到nginx集群下
实验准备:使用了2台服务器,一台是运行jenkins服务,另一台运行web服务
1.在web服务器部署一个nginx web 集群
yum install -y nginx
2.修改nginx的配置文件
[root@web ~]# vim /www/server/nginx/conf/nginx.conf
server
{
listen 888; //修改端口
server_name phpmyadmin;
index index.html index.htm index.php;
root /www/server/phpmyadmin;
location ~ /tmp/ {
return 403;
}
创建页面
[root@web ~]# cd /www/server/phpmyadmin
[root@web phpmyadmin]# ls
[root@web phpmyadmin]# echo "hello guan12319" > index.html
[root@web phpmyadmin]# ls
index.html
在浏览器访问页面
[root@web phpmyadmin]# cat index.html
hello guan12319 ----> 你好,世界
在网页文件输入中乱码
解决办法,在网页文件加入如下内容
[root@web phpmyadmin]# cat index.html
<meta charset=utf8>
hello guan12319 ---- 你好,世界
模拟开发推送代码带gitee或者gitlab或者GitHub
- 开发将代码提交到仓库
- 运维通过jenkins从gitee或gitlab拉取代码,然后在jenkins通过使用shell部署到nginx服务器上。
- 在jenkins上创建心的job,然后填入内容,用于执行一个脚本
sh -x /scripts/deploy_nginx.sh
- 在jenkins服务器上部署好脚本
[root@jenkins ~]# mkdir /scripts/
[root@jenkins ~]# touch /scripts/deploy_nginx.sh
[root@jenkins ~]# cat /scripts/deploy_nginx.sh
#1,进入代码目录,打包传输
DATE=$(date +%Y-%m-%d-%H-%M-%S)
#web_server="154.9.228.143 154.9.228.144 154.9.228.145"
web_server="154.9.228.143"
# 定义部署的功能函数
# $WORKSPACE 是 jenkins 特有的变量,取得当前job的工作绝对路径
get_code(){
cd $WORKSPACE && \
tar czf /opt/web-${DATE}.tar.gz ./*
}
# 2.代码发送给web集群组
scp_web_server(){
for hosts in $web_server
do
# jenkins执行如下命令,将自己本地代码压缩文件,scp发送给nginx服务器
scp /opt/web-${DATE}.tar.gz root@$hosts:/opt/
# jenkins服务器,利用ssh命令,远程执行命令
ssh root@$hosts "mkdir -p /nginx_web_html/web-${DATE} && \
tar -zxf /opt/web-${DATE}.tar.gz -C /nginx_web_html/web-${DATE}
rm -rf /nginx_web_html/web && \
ln -s /nginx_web_html/web-${DATE} /nginx_web_html/web"
done
}
# nginx 服务器上的网页根目录是: /nginx_web_html/web
# 3.函数执行
deploy(){
get_code
scp_web_server
}
# 4.执行入口
deploy
#给脚本赋予执行权限
[root@jenkins ~]# chmod 777 /scripts/deploy_nginx.sh
脚本直接执行,如果权限不够,可能是因为jenkins这个服务是jenkins用户运行的,像要操作目录权限不够,所以需要修改jenkins的用户为root
[root@jenkins ~]# grep root /etc/sysconfig/jenkins
JENKINS_USER="root"
[root@jenkins opt]# ssh-copy-id root@154.9.228.143
构建成功之后即可,访问网页了