Jenkins介绍
Jenkins是一个开源的、可扩展的持续集成、交付、部署(软件/代码的编译、打包、部署)的基于web界面的平台。
起源于Hudson(Hudson是商用的),主要用于持续、自动的构建/测试软件项目、监控外部任务的运行(这个比较抽象,暂且写上,不做解释)。
允许持续集成和持续交付项目,无论用的是什么平台,可以处理任何类型的构建或持续集成。
它可以在代码上传仓库(如github,gitee,gitlab)后,在jenkins(一个网站界面)中通过获取代码仓库中最新代码,进行自动化部署,而省去手动打包、上传服务器、部署这一系列步骤,非常方便。
好处:
- 降低风险,由于持续集成不断去构建,编译和测试,可以很早发现问题
- 减少重复性的工作
- 持续部署,提供可部署单元包
- 持续交付可供使用的版本
Jenkins安装部署
#下载jdk11
[root@jenkins ~]# yum install -y java-11-openjdk
#下载jenkins
[root@jenkins ~]# yum install -y https://mirrors.tuna.tsinghua.edu.cn/jenkins/redhat/jenkins-2.419-1.1.noarch.rpm
#修改插件下载源
[root@jenkins ~]# vim /var/lib/jenkins/hudson.model.UpdateCenter.xml
<?xml version='1.1' encoding='UTF-8'?>
<sites>
<site>
<id>default</id>
<url>https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json</url> #//修改为国内源
</site>
</sites>
#启动服务
[root@jenkins ~]# systemctl start jenkins.service
#浏览器访问
10.0.0.92:8080
#查看密码
[root@jenkins ~]# cat /var/lib/jenkins/secrets/initialAdminPassword
2bf57af45e8e4a33850ec6aec98d7458
插件安装
方法一:页面安装
方法二:镜像源安装
[root@jenkins plugins]# wget
https://mirrors.tuna.tsinghua.edu.cn/jenkins/plugins/localization-zh-cn/latest/localization-zh-cn.hpi --no-check-certificate
# 方法一:手动解压
[root@jenkins plugins]# unzip localization-zh-cn.hpi -d localization-zh-cn
# 方法二:自动解压 //修改插件包属主和属组为jenkins
[root@jenkins plugins]# systemctl restart jenkins
常用插件
- git
- gitlab
- pipeline
- maven
- ssh
- blue ocean
- sonar
Jenkins任务自由风格
普通构建
#优化插件
tar xf plugins.tar.gz
#重启服务
systemctl restart jenkins.service
报错一:
原因:jenkins服务端没有git命令
yum install -y git
报错二:
原因:gitlab没有jenkins公钥导致连接失败
添加jenkins公钥至gitlab
#切换用户至jenkins //因为是使用jenkins用户启动的服务,所以添加公钥的时候要注意是哪个用户的公钥
#修改登录权限
[root@jenkins ~]# usermod -s /bin/bash jenkins
#切换用户
[root@jenkins ~]# su - jenkins
Last login: Fri Aug 25 16:27:08 CST 2023 on pts/0
-bash-4.2$
#生成密钥
-bash-4.2$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/var/lib/jenkins/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /var/lib/jenkins/.ssh/id_rsa.
Your public key has been saved in /var/lib/jenkins/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Z62M+bDS5xo20umGnHlni5M+6mtRQK5jF5B5RLKJfPY jenkins@jenkins
The key's randomart image is:
+---[RSA 2048]----+
| o*+ |
| . .o*o |
| o =.o. |
| o o .. . |
| + E.S o . |
| . o.. B . |
| ..*X.o |
| B+BB+ |
| o+B*O+. |
+----[SHA256]-----+
#查看公钥
-bash-4.2$ cat .ssh/id_rsa.pub
#添加了公钥之后无报错的情况,可跳过添加私钥操作
即使添加了公钥和私钥为什么还是报错呢??
#执行报错成给出的命令
git ls-remote -h git@gitlab.driverzeng.com:root/code
#不难看出,出现了交互式的操作,因为jenkins连接gitlab无法自行完成交互式的操作,所以一直会卡住,导致连接失败
#创建站点目录
[root@jenkins ~]# mkdir /code
[root@jenkins ~]# ll /code/
total 0
任务构建测试
#推送失败原因。客户端没有jenkis公钥导致scp推送失败 //那台机器需要拉却代码就推送公钥给那台机器
#推送公钥
[root@jenkins ~]# su - jenkins
-bash-4.2$ ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.7
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/var/lib/jenkins/.ssh/id_rsa.pub"
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@172.16.1.7's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@172.16.1.7'"
and check to make sure that only the key(s) you wanted were added.
客户端查看:
参数构建
保存,构建测试
更新以下插件即可
#web网页访问
重新选择一个版本
多参数任务构建
#多参数构建的时候,shell执行可以选择脚本的方式执行
#编写脚本
[root@jenkins scripts]# vim /var/lib/jenkins/scripts/gitlab_copy.sh
#!/bin/bash
#-----------------------------变量定义-------------------
#站点目录
dir=code
#代码工作路径
WORKSPACE=/var/lib/jenkins/workspace/freestyle-code-参数化构建
#测试:user ip
test_user=root
test_ip=172.16.1.7
#开发:user ip
dev_user=root
dev_ip=172.16.1.8
#生产:user ip
pord_user=root
pord_ip=172.16.1.9
host_name=$(ps -ef | grep [j]enkins | awk 'NR==1{print $1}')
#-----------------------------函数定义---------------------
pull(){
#进入workspacemul
cd $WORKSPACE
#打包代码
tar zcf ${gittag}.tar.gz ./*
#推送至下拉机器
scp ${gittag}.tar.gz $1:/${dir}
#远端解压
ssh $1 "cd /${dir} && tar xf ${gittag}.tar.gz"
}
#公钥推送
set_up_ssh(){
su - $host_name
if [ -f .ssh/id_dsa.pub ];then
ssh-copy-id -i ~/.ssh/id_dsa.pub -o StrictHostKeyChecking=no $1
fi
}
#-----------------------------代码运行----------------------
if [[ $env == test ]]; then
set_up_ssh ${test_user}@${test_ip}
pull ${test_user}@${test_ip}
elif [[ $env == dev ]]; then
set_up_ssh ${dev_user}@${dev_ip}
pull ${dev_user}@${dev_ip}
elif [[ $env == pord ]]; then
set_up_ssh ${pord_user}@${pord_ip}
pull ${pord_user}@${pord_ip}
else
exit
fi
//可运行版
#!/bin/bash
#-----------------------------变量定义-------------------
#站点目录
dir=code
#代码工作路径
WORKSPACE=/var/lib/jenkins/workspace/freestyle-code-参数化构建
#测试:user ip
test_user=root
test_ip=172.16.1.7
#开发:user ip
dev_user=root
dev_ip=172.16.1.8
#生产:user ip
pord_user=root
pord_ip=172.16.1.9
#-----------------------------函数定义---------------------
pull(){
#进入workspacemul
cd $WORKSPACE
#打包代码
tar zcf ${gittag}.tar.gz ./*
#推送至下拉机器
scp ${gittag}.tar.gz $1:/${dir}
#远端解压
ssh $1 "cd /${dir} && tar xf ${gittag}.tar.gz"
}
#-----------------------------代码运行----------------------
if [[ $env == test ]]; then
pull ${test_user}@${test_ip}
elif [[ $env == dev ]]; then
pull ${dev_user}@${dev_ip}
elif [[ $env == pord ]]; then
pull ${pord_user}@${pord_ip}
else
exit
fi
Jenkins批量删除构建历史
def jobName = "Item_name"
def maxNumber = 100
Jenkins.instance.getItemByFullName(jobName).builds.findAll {
it.number <= maxNumber
}.each {
it.delete()
}
# 注:jobName 为需要删除构建历史的项目名称