jenkins 参数构建

整体思路

jenkins 参数构建_第1张图片

依赖环境及工具

  • Git
  • Centos7及以上
  • Gitlab
  • Jenkins
  • shell
  • ansible

创建一个jenkins项目

jenkins 参数构建_第2张图片
jenkins 参数构建_第3张图片
jenkins 参数构建_第4张图片
jenkins 参数构建_第5张图片
jenkins 参数构建_第6张图片
应用保存,测试构建
jenkins 参数构建_第7张图片

jenkins 参数构建_第8张图片
在gitlab创建新项目,编写index.html
jenkins 参数构建_第9张图片

[root@jenkins-node1 .ssh]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:+ri/kjkgpJHVMn97xQRp/h6AfgRzqzUfvp2Gid6y8xM root@jenkins-node1
The key's randomart image is:
+---[RSA 2048]----+
|   .    .o       |
|  + . o + .      |
| o +   B +       |
|o . . o B +      |
| +   o =SB .     |
|. . . +.o E      |
|   . ..= o B .   |
|      =o+.* +    |
|      o***oo     |
+----[SHA256]-----+
[root@jenkins-node1 .ssh]# 
[root@jenkins-node1 .ssh]# ls
authorized_keys  id_rsa  id_rsa.pub  known_hosts
[root@jenkins-node1 .ssh]# ls
authorized_keys  id_rsa  id_rsa.pub  known_hosts
[root@jenkins-node1 .ssh]# cat id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCpJFKXoRIr8JjLP0kvF0ScH0/HJ3hBb+bXY+ERvFtL7MZQkHUJkfaRQaHOQaRve2uAzg3FAnErAfRmUAJ7NmLvq6TwAles69ESrdRNy0zCAqgy8d/0d+Onp1BVGEf84L3MZNFleaGv5TuXymWE1pKkUVo8wG+OycfU5kTXIM/RJ/wO/2sUNKZjVcy66CPHcioeni6M2hmER3jMPkb5h1d3X+5m8epotgpnOr+Gs+5UczFKS01XN70uxyiktpAVqR0Kd4pRnL3hyhl8PT6SeIRI9txne7mDkb8rV7Kzy8/TobxYG4jLPr61iR8Uc0c62V0gLf8jpgdgCDgxl8Om0BWp root@jenkins-node1

jenkins 参数构建_第10张图片

[root@jenkins-node1 nginx]# rm -rf html/
[root@jenkins-node1 nginx]# ls
[root@jenkins-node1 nginx]#  git clone http://192.168.20.155/zx/test.git
正克隆到 'test'...
Username for 'http://192.168.20.155': xiaoguan
Password for 'http://[email protected]': 
remote: Enumerating objects: 9, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 9 (delta 0), reused 0 (delta 0), pack-reused 3
Unpacking objects: 100% (9/9), done.
[root@jenkins-node1 nginx]# ls
test
[root@jenkins-node1 nginx]# mv test/ html

浏览器访问
jenkins 参数构建_第11张图片

[root@jenkins ~]# yum -y install ansible
[root@jenkins ~]# vim /opt/hosts
[nginx]
192.168.20.156
[tomcat]

[root@jenkins ~]# ansible -i /opt/hosts  all -m  ping -o
192.168.20.156 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
[root@jenkins opt]# vim nginx-test.sh
#!/bin/bash
service_name=$1
statu=$2
gitlab_cade=$3
gitlab_id=$4

cmd="ansible -i /opt/hosts nginx"

if [ "$service_name" = "nginx" ];then
 if [ "$gitlab_cade" = "pull" ];then
  $cmd -m shell -a "chdir=/usr/share/nginx/html git pull [email protected]:zx/test.git"
 elif [ "$gitlab_cade" = "reset1" ];then
  $cmd -m shell -a "chdir=/usr/share/nginx/html git reset --hard HEAD^"
 elif [ "$gitlab_cade" = "reset2" ];then
  $cmd -m shell -a "chdir=/usr/share/nginx/html git reset --hard HEAD^^"
 elif [ "$gitlab_cade" = "reset_id" ];then
  $cmd -m shell -a "chdir=/usr/share/nginx/html git reset --hard $gitlab_id"
fi
 if [ "$statu" != "none" ];then
  $cmd -m shell -a "systemctl $statu nginx"
fi
fi

测试脚本是否成功

[root@jenkins opt]# sh nginx-test.sh nginx none pull none
192.168.20.156 | CHANGED | rc=0 >>
更新 cd2dd06..e9aea5f
Fast-forward
 index.html | 1 +
 1 file changed, 1 insertion(+)来自 192.168.20.155:zx/test
 * branch            HEAD       -> FETCH_HEAD

修改配置

jenkins 参数构建_第12张图片
修改index.html
jenkins 参数构建_第13张图片
jenkins 参数构建_第14张图片
jenkins 参数构建_第15张图片
jenkins 参数构建_第16张图片

回滚

jenkins 参数构建_第17张图片
jenkins 参数构建_第18张图片

你可能感兴趣的:(jenkins,servlet,java)