案例:实现脚本自动化上传代码
环境
主机名 | 主机IP |
---|---|
gitlab | 192.168.1.20 |
jenkins | 192.168.1.19 |
nginx | 192.168. |
部署gitlab和jenkins环境此处省略!
gitlab服务器
创建新的项目(web)
[root@gitlab ~]# git clone https://gitee.com/kangjie1209/monitor.git //在gitee码云上下载项目代码
正克隆到 'monitor'...
remote: Enumerating objects: 435, done.
remote: Counting objects: 100% (435/435), done.
remote: Compressing objects: 100% (381/381), done.
remote: Total 435 (delta 53), reused 390 (delta 44), pack-reused 0
接收对象中: 100% (435/435), 8.78 MiB | 6.62 MiB/s, done.
处理 delta 中: 100% (53/53), done.
创建本地代码库
[root@gitlab ~]# mkdir /web
[root@gitlab ~]# cd /web
[root@gitlab web]# git init
初始化空的 Git 版本库于 /web/.git/
[root@gitlab web]# cp -rp /root/monitor/* /web/
上传代码到gitlab服务器
[root@gitlab web]# git remote add origin git@192.168.1.20:dev/web.git
[root@gitlab web]# git add .
[root@gitlab web]# git commit -m "commit"
[master(根提交) eebc34b] commit
376 files changed, 65793 insertions(+)
......
[root@gitlab web]# git push -u origin master
Counting objects: 414, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (407/407), done.
Writing objects: 100% (414/414), 4.31 MiB | 0 bytes/s, done.
Total 414 (delta 48), reused 0 (delta 0)
remote: Resolving deltas: 100% (48/48), done.
To git@192.168.1.20:dev/web.git
* [new branch] master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。
jenkins服务器
new item >> 输入项目名(web) >> Freestyle project >> ok >> configure ->> 源代码库 >> git >> gitlab路径及私钥认证 >> save
实现脚本上传网站代码(nginx)
(1)安装nginx
[root@nginx ~]# yum -y install epel-release.noarch
[root@nginx ~]# yum -y install nginx
[root@nginx ~]# systemctl start nginx
[root@nginx ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
(2)在jenkins服务器编写上传nginx的脚本
[root@jenkins ~]# mkdir /scripts
[root@jenkins ~]# vim /scripts/web.sh
#!/bin/sh
CODE_DIR=/var/lib/jenkins/workspace/web/
WEB_DIR=/usr/share/nginx
IP=192.168.1.11
TIME=`date +%F-%H-%M-%S`
cd $CODE_DIR && tar zcf /tmp/web-${TIME}.tar.gz ./*
scp /tmp/web-${TIME}.tar.gz $IP:$WEB_DIR
ssh root@$IP "cd $WEB_DIR && mkdir web-$TIME"
ssh root@$IP "cd $WEB_DIR && tar xf web-${TIME}.tar.gz -C web-$TIME && rm -rf web-${TIME}.tar.gz"
ssh root@$IP "cd $WEB_DIR && rm -rf html && ln -s web-$TIME html"
(3)jenkins传输公钥到nginx,执行脚本,测试查看nginx网站
[root@jenkins ~]# ssh-copy-id root@192.168.1.11
[root@jenkins ~]# sh /scripts/web.sh
web-2020-07-23-14-43-54.tar.gz 100% 4545KB 54.5MB/s 00:00
(4)手动构建,关联脚本,自动上传
项目web >> configure >> build >> Execute shell >> sh /scripts/web.sh
[
jenkins关联gitlab,实现自动上传代码
(1)jenkins服务器
URL: http://192.168.1.19:8080/project/web
令牌:5492f28075b4a49b60d7c5109db4c7dd
(2)gitlab服务器
(3)更新push代码,测试自动构建上传。
gitlab:修改代码或添加新的文件
[root@gitlab web]# vim index.html
<title>移动能效管理平台</title>
改成:
<title>欢迎使用本网站</title>
[root@gitlab web]# git add .
[root@gitlab web]# git commit -m "modify index.html"
[master cfd388a] modify index.html
1 file changed, 2 insertions(+), 2 deletions(-)
[root@gitlab web]# git push -u origin master
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 324 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To git@192.168.1.20:dev/web.git
eebc34b..cfd388a master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。
(4)登录nginx网站,查看更新内容。