jenkins 参数构建

整体思路

依赖环境及工具

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

创建一个jenkins项目





应用保存,测试构建


在gitlab创建新项目,编写index.html

[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
[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://xiaoguan@192.168.20.155': 
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

浏览器访问

[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 git@192.168.20.155: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

修改配置


修改index.html



回滚


相关推荐
P7进阶路1 小时前
Tomcat异常日志中文乱码怎么解决
java·tomcat·firefox
小丁爱养花1 小时前
Spring MVC:HTTP 请求的参数传递2.0
java·后端·spring
CodeClimb2 小时前
【华为OD-E卷 - 第k个排列 100分(python、java、c++、js、c)】
java·javascript·c++·python·华为od
等一场春雨2 小时前
Java设计模式 九 桥接模式 (Bridge Pattern)
java·设计模式·桥接模式
带刺的坐椅2 小时前
[Java] Solon 框架的三大核心组件之一插件扩展体系
java·ioc·solon·plugin·aop·handler
不惑_3 小时前
深度学习 · 手撕 DeepLearning4J ,用Java实现手写数字识别 (附UI效果展示)
java·深度学习·ui
费曼乐园3 小时前
Kafka中bin目录下面kafka-run-class.sh脚本中的JAVA_HOME
java·kafka
feilieren3 小时前
SpringBoot 搭建 SSE
java·spring boot·spring
阿岳3163 小时前
Java导出通过Word模板导出docx文件并通过QQ邮箱发送
java·开发语言
Amor风信子4 小时前
华为OD机试真题---战场索敌
java·开发语言·算法·华为od·华为