DevOps-Jenkins实现代码clone与简单job部署

代码克隆方式

通过shell命令clone

通过jenkins clone(非交互式代码克隆)

git基于ssh密钥实现自动代码clone,不需要输入账号密码,要求公钥和私钥固定,最大限制为clone的代码不能push。

http 是交互式clone代码,不会用于jenkins的代码clone,但是clone后做代码修改后可以重新push至gitlab。

1. jenkins服务器生成密钥

]# ssh-keygen

2. 添加公钥到gitlab服务器

 

 测试可以不使用用户名密码后直接获取代码

在jenkins服务器创建代码目录

[root@ecs-60c0 ~]# mkdir /data/git/linux39 -p
[root@ecs-60c0 ~]# mkdir /data/git/magedu -p

jenkins项目使用shell命令clone代码示例

构建方式选择shell

echo $USER ; cd /data/git/linux39 ; rm -rf web1 ; git clone git@159.xx.xx.xxx:linux39/web1.git

拷贝jenkins公钥给web服务器

web1
]# chown -R www:www /opt/apps/ 
]# chown -R www:www /data/tomcat
web2 
]#
chown -R www:www /opt/apps/
]#
chown -R www:www /data/tomcat
jenkins ]# ssh-copy-id www@192.168.0.79 ]# ssh-copy-id www@192.168.0.142

拷贝代码

scp -r  web1/*  www@web1:/data/tomcat/tomcat_webapps/myapp/
scp -r web2/*  www@web1:/data/tomcat/tomcat_webapps/myapp/

jenkins使用以下shell命令构建

echo $USER ; cd /data/git/linux39 ; rm -rf web1 ; git clone git@159.138.5.152:linux39/web1.git
ssh www@web1 "bash /opt/apps/tomcat.sh stop"
ssh www@web2 "bash /opt/apps/tomcat.sh stop"

scp -r web1/*  www@web1:/data/tomcat/tomcat_webapps/myapp/
scp -r web1/*  www@web2:/data/tomcat/tomcat_webapps/myapp/

ssh www@web1 "bash /opt/apps/tomcat.sh start"
ssh www@web2 "bash /opt/apps/tomcat.sh start"

立即构建,打印出控制台输出信息

Started by user jenkinsadmin
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/linux38-web1
[linux38-web1] $ /bin/sh -xe /tmp/jenkins8429408481865685129.sh
+ echo root
root
+ cd /data/git/linux39
+ rm -rf web1
+ git clone git@159.138.5.152:linux39/web1.git
Cloning into 'web1'...
+ ssh www@web1 'bash /opt/apps/tomcat.sh stop'
正在判断服务状态,请稍等3秒钟!
3
2
1
Tomcat 没有运行
1
+ ssh www@web2 'bash /opt/apps/tomcat.sh stop'
正在判断服务状态,请稍等3秒钟!
3
2
1
Tomcat 没有运行
1
+ scp -r web1/app1 web1/index.html www@web1:/data/tomcat/tomcat_webapps/myapp/
+ scp -r web1/app1 web1/index.html www@web2:/data/tomcat/tomcat_webapps/myapp/
+ ssh www@web1 'bash /opt/apps/tomcat.sh start'
正在判断服务状态,请稍等!
请稍等3秒钟
3
2
1
Tomcat没有运行,1秒后启动!
1
Tomcat started.
Tomcat 已经成功启动完成,5秒后判断是否启动成功
5
4
3
2
1
Tomcat 已经成功启动1 个Tomcat进程!,PID为3027
+ ssh www@web2 'bash /opt/apps/tomcat.sh start'
正在判断服务状态,请稍等!
请稍等3秒钟
3
2
1
Tomcat没有运行,1秒后启动!
1
Tomcat started.
Tomcat 已经成功启动完成,5秒后判断是否启动成功
5
4
3
2
1
Tomcat 已经成功启动1 个Tomcat进程!,PID为2625
Finished: SUCCESS

测试访问

[root@gitlab ~]# curl http://web1:8080/myapp/index.html -i
HTTP/1.1 200
Accept-Ranges: bytes
ETag: W/"76-1640529889000"
Last-Modified: Sun, 26 Dec 2021 14:44:49 GMT
Content-Type: text/html
Content-Length: 76
Date: Sun, 26 Dec 2021 14:50:35 GMT

<h1>this is linux39 test page v1</h1>
<h1>this is linux39 test page v2</h1>

[root@gitlab ~]# curl http://web2:8080/myapp/index.html -i
HTTP/1.1 200
Accept-Ranges: bytes
ETag: W/"76-1640529889000"
Last-Modified: Sun, 26 Dec 2021 14:44:49 GMT
Content-Type: text/html
Content-Length: 76
Date: Sun, 26 Dec 2021 14:52:03 GMT

<h1>this is linux39 test page v1</h1>
<h1>this is linux39 test page v2</h1>
代码升级

升级代码页面

[root@ecs-60c0 web1]# cat index.html
<h1>this is linux39 test page v1</h1>
<h1>this is linux39 test page v2</h1>
<h1>this is linux39 test page v3</h1>

上传代码到gitlab

]# git add .
[root@ecs-60c0 web1]# git status
[root@ecs-60c0 web1]# git commit -m "v3"
[root@ecs-60c0 web1]# git push

使用laomao用户重新构建升级

测试访问,可以看到已经升级到了v3

[root@gitlab ~]# curl http://web1:8080/myapp/index.html -i
HTTP/1.1 200
Accept-Ranges: bytes
ETag: W/"115-1640530742000"
Last-Modified: Sun, 26 Dec 2021 14:59:02 GMT
Content-Type: text/html
Content-Length: 115
Date: Sun, 26 Dec 2021 14:59:51 GMT

<h1>this is linux39 test page v1</h1>
<h1>this is linux39 test page v2</h1>
<h1>this is linux39 test page v3</h1>

[root@gitlab ~]# curl http://web2:8080/myapp/index.html -i
HTTP/1.1 200
Accept-Ranges: bytes
ETag: W/"115-1640530742000"
Last-Modified: Sun, 26 Dec 2021 14:59:02 GMT
Content-Type: text/html
Content-Length: 115
Date: Sun, 26 Dec 2021 15:00:27 GMT

<h1>this is linux39 test page v1</h1>
<h1>this is linux39 test page v2</h1>
<h1>this is linux39 test page v3</h1>

 

使用jenkins拉取代码,配置 jenkins到 gitlab非交互拉取代码

右上角-凭据-jenkins—全局凭据—添加凭据

 配置 git 项目地址和选择拉取代码的用户key

 完成代码拷贝与部署

cd /var/lib/jenkins/workspace/linux38-web1

tar -zcvf myapp.tar.gz ./*
scp myapp.tar.gz www@web1:/data/tomcat/tomcat_appdir
scp myapp.tar.gz www@web2:/data/tomcat/tomcat_appdir

ssh www@web1 "bash /opt/apps/tomcat.sh stop"
ssh www@web2 "bash /opt/apps/tomcat.sh stop"

ssh www@web1  "cd /data/tomcat/tomcat_appdir ; rm -rf /data/tomcat/tomcat_webapps/myapp/* ; tar -xvf /data/tomcat/tomcat_appdir/myapp.tar.gz -C  /data/tomcat/tomcat_webapps/myapp"
ssh www@web2  "cd /data/tomcat/tomcat_appdir ; rm -rf /data/tomcat/tomcat_webapps/myapp/* ; tar -xvf /data/tomcat/tomcat_appdir/myapp.tar.gz -C  /data/tomcat/tomcat_webapps/myapp"

ssh www@web1 "bash /opt/apps/tomcat.sh start"
ssh www@web2 "bash /opt/apps/tomcat.sh start"

点击立即构建

Started by user laomao
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/linux38-web1       //jenkins拉取代码自动创建的工作目录
The recommended git tool is: NONE
using credential 1805eae6-669c-4308-8172-da424fea9bec
 > git rev-parse --resolve-git-dir /var/lib/jenkins/workspace/linux38-web1/.git # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url git@159.138.5.152:linux39/web1.git # timeout=10
Fetching upstream changes from git@159.138.5.152:linux39/web1.git
 > git --version # timeout=10
 > git --version # 'git version 1.8.3.1'
using GIT_SSH to set credentials 
 > git fetch --tags --progress git@159.138.5.152:linux39/web1.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
Checking out Revision a33a4c42058587e438cf8767eb037d40528e01af (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f a33a4c42058587e438cf8767eb037d40528e01af # timeout=10
Commit message: "v3"
 > git rev-list --no-walk a33a4c42058587e438cf8767eb037d40528e01af # timeout=10
[linux38-web1] $ /bin/sh -xe /tmp/jenkins8090604836751877080.sh
+ cd /var/lib/jenkins/workspace/linux38-web1
+ tar -zcvf myapp.tar.gz ./app1 ./index.html ./myapp.tar.gz
./app1/
./app1/index.html
./index.html
./myapp.tar.gz
+ scp myapp.tar.gz www@web1:/data/tomcat/tomcat_appdir
+ scp myapp.tar.gz www@web2:/data/tomcat/tomcat_appdir
+ ssh www@web1 'bash /opt/apps/tomcat.sh stop'
正在判断服务状态,请稍等3秒钟!
3
2
1
Tomcat 没有运行
1
+ ssh www@web2 'bash /opt/apps/tomcat.sh stop'
正在判断服务状态,请稍等3秒钟!
3
2
1
Tomcat 没有运行
1
+ ssh www@web1 'cd /data/tomcat/tomcat_appdir ; rm -rf /data/tomcat/tomcat_webapps/myapp/* ; tar -xvf /data/tomcat/tomcat_appdir/myapp.tar.gz -C  /data/tomcat/tomcat_webapps/myapp'
./app1/
./app1/index.html
./index.html
./myapp.tar.gz
+ ssh www@web2 'cd /data/tomcat/tomcat_appdir ; rm -rf /data/tomcat/tomcat_webapps/myapp/* ; tar -xvf /data/tomcat/tomcat_appdir/myapp.tar.gz -C  /data/tomcat/tomcat_webapps/myapp'
./app1/
./app1/index.html
./index.html
./myapp.tar.gz
+ ssh www@web1 'bash /opt/apps/tomcat.sh start'
正在判断服务状态,请稍等!
请稍等3秒钟
3
2
1
Tomcat没有运行,1秒后启动!
1
Tomcat started.
Tomcat 已经成功启动完成,5秒后判断是否启动成功
5
4
3
2
1
Tomcat 已经成功启动1 个Tomcat进程!,PID为3878
+ ssh www@web2 'bash /opt/apps/tomcat.sh start'
正在判断服务状态,请稍等!
请稍等3秒钟
3
2
1
Tomcat没有运行,1秒后启动!
1
Tomcat started.
Tomcat 已经成功启动完成,5秒后判断是否启动成功
5
4
3
2
1
Tomcat 已经成功启动1 个Tomcat进程!,PID为3400
Finished: SUCCESS

 

posted @ 2021-12-26 23:56  不会跳舞的胖子  阅读(377)  评论(0编辑  收藏  举报