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



回滚


相关推荐
bug管理者7 分钟前
UI自动化测试中公认最佳的设计模式-POM
jenkins·策略模式
儿时可乖了40 分钟前
使用 Java 操作 SQLite 数据库
java·数据库·sqlite
ruleslol42 分钟前
java基础概念37:正则表达式2-爬虫
java
xmh-sxh-13141 小时前
jdk各个版本介绍
java
天天扭码1 小时前
五天SpringCloud计划——DAY2之单体架构和微服务架构的选择和转换原则
java·spring cloud·微服务·架构
程序猿进阶1 小时前
堆外内存泄露排查经历
java·jvm·后端·面试·性能优化·oom·内存泄露
FIN技术铺1 小时前
Spring Boot框架Starter组件整理
java·spring boot·后端
小曲程序1 小时前
vue3 封装request请求
java·前端·typescript·vue