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



回滚


相关推荐
GoodStudyAndDayDayUp2 分钟前
IDEA能够从mapper跳转到xml的插件
xml·java·intellij-idea
装不满的克莱因瓶33 分钟前
【Redis经典面试题六】Redis的持久化机制是怎样的?
java·数据库·redis·持久化·aof·rdb
n北斗40 分钟前
常用类晨考day15
java
骇客野人43 分钟前
【JAVA】JAVA接口公共返回体ResponseData封装
java·开发语言
yuanbenshidiaos2 小时前
c++---------数据类型
java·jvm·c++
向宇it2 小时前
【从零开始入门unity游戏开发之——C#篇25】C#面向对象动态多态——virtual、override 和 base 关键字、抽象类和抽象方法
java·开发语言·unity·c#·游戏引擎
coder_pig2 小时前
📝小记:Ubuntu 部署 Jenkins 打包 Flutter APK
flutter·ubuntu·jenkins
Lojarro2 小时前
【Spring】Spring框架之-AOP
java·mysql·spring
莫名其妙小饼干2 小时前
网上球鞋竞拍系统|Java|SSM|VUE| 前后端分离
java·开发语言·maven·mssql
isolusion2 小时前
Springboot的创建方式
java·spring boot·后端