安装jenkins
Jenkins介绍 Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能。
Jenkins非常的灵活,可以配合很多平台工作,所以这也是为什么自动化CI CD会使用Jenkins。只要给Jenkins安装上对应的插件,那么Jenkins就会变的很强大。
# 1.安装jenkins(用yum可以解决部分依赖)
[root@jenkins ~]# yum install -y https://mirrors.tuna.tsinghua.edu.cn/jenkins/redhat/jenkins-2.419-1.1.noarch.rpm
# 2.安装jdk11
[root@jenkins ~]# yum install -y java-11-openjdk
# 3.修改插件下载源
[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>
# 5.启动jenkins
[root@jenkins ~]# systemctl start jenkins
检查端口是否起来(8080Java的)
[root@jenkins ~]# netstat -lntup
# 4.浏览器访问
10.0.0.92:8080
访问成功后会告诉密码存放位置
/var/lib/jenkins/secrets/initialAdminPassword
# 5.查看密码
[root@jenkins ~]# cat /var/lib/jenkins/secrets/initialAdminPassword
e49ff74da5e54b8985e14ec79ebed5cd(Jenkins启不来的)
b45fb0054a5346f285b53e68ca153cc1
然后把密码贴进去,即可访问
# 6.获取插件
先上传插件:rz 选择所需插件jenkins_plugins.tar.gz
再获取插件:[root@jenkins ~]# mv jenkins_plugins.tar.gz /var/lib/jenkins/
没装插件前
安装jenkins插件
方式一:页面安装
# 1.进入jenkins目录下
[root@jenkins ~]# cd /var/lib/jenkins/
# 2.去jenkins网页安装插件
# 3.装完中文插件后plugins目录产生了很多相关文件
[root@jenkins jenkins]# ll plugins/
total 9964
drwxr-xr-x 4 jenkins jenkins 56 Aug 27 13:00 bouncycastle-api
-rw-r--r-- 1 jenkins jenkins 8871674 Aug 27 13:00 bouncycastle-api.jpi
drwxr-xr-x 4 jenkins jenkins 56 Aug 27 13:00 instance-identity
-rw-r--r-- 1 jenkins jenkins 18849 Aug 27 13:00 instance-identity.jpi
drwxr-xr-x 4 jenkins jenkins 56 Aug 27 13:00 javax-activation-api
-rw-r--r-- 1 jenkins jenkins 77290 Aug 27 13:00 javax-activation-api.jpi
drwxr-xr-x 4 jenkins jenkins 56 Aug 27 13:00 javax-mail-api
-rw-r--r-- 1 jenkins jenkins 632493 Aug 27 13:00 javax-mail-api.jpi
drwxr-xr-x 4 jenkins jenkins 56 Aug 27 13:01 localization-support
-rw-r--r-- 1 jenkins jenkins 24084 Aug 27 13:01 localization-support.jpi
drwxr-xr-x 5 jenkins jenkins 70 Aug 27 13:01 localization-zh-cn
-rw-r--r-- 1 jenkins jenkins 569475 Aug 27 13:01 localization-zh-cn.jpi
# 4.重启jenkins
[root@jenkins jenkins]# systemctl restart jenkins
# 5.再次访问
汉化后重新登录
方式二:镜像源安装
镜像安装方式一:手动解压
# 1.安装插件
[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]# ll
total 540
-rw-r--r-- 1 root root 551108 Nov 16 2020 localization-zh-cn.hpi
# 2.查看压缩包类型
[root@jenkins plugins]# file localization-zh-cn.hpi
localization-zh-cn.hpi: Zip archive data, at least v1.0 to extract
# 3.解压
先创建一个解压目录:localization-zh-cn
解压到指定目录:
[root@jenkins plugins]# unzip localization-zh-cn.hpi -d localization-zh-cn
# 4.授权
[root@jenkins plugins]# chown -R jenkins.jenkins ./*
# 5.重启jenkins
[root@jenkins plugins]# systemctl restart jenkins
# 浏览器访问,查看是否汉化 (?没成功)??????????????????????????????????????
镜像安装方式二:自动解压
# 1.安装插件
[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]# wget https://mirrors.tuna.tsinghua.edu.cn/jenkins/plugins/localization-support/latest/localization-support.hpi --no-check-certificate
# 2.授权
[root@jenkins plugins]# chown -R jenkins.jenkins ./*
# 3.启动jenkins
[root@jenkins plugins]# systemctl restart jenkins
# 启动后后自动解压开
[root@jenkins plugins]# ll
total 16948
drwxr-xr-x 4 jenkins jenkins 56 Aug 27 13:50 bouncycastle-api
-rw-r--r-- 1 jenkins jenkins 11944104 Aug 27 12:19 bouncycastle-api.jpi
drwxr-xr-x 4 jenkins jenkins 56 Aug 27 13:50 instance-identity
-rw-r--r-- 1 jenkins jenkins 18849 Aug 27 12:19 instance-identity.jpi
drwxr-xr-x 4 jenkins jenkins 56 Aug 27 13:50 javax-activation-api
-rw-r--r-- 1 jenkins jenkins 77290 Aug 27 12:19 javax-activation-api.jpi
drwxr-xr-x 4 jenkins jenkins 56 Aug 27 13:50 javax-mail-api
-rw-r--r-- 1 jenkins jenkins 632493 Aug 27 12:19 javax-mail-api.jpi
drwxr-xr-x 4 jenkins jenkins 56 Aug 27 13:50 localization-support
-rw-r--r-- 1 jenkins jenkins 23848 May 2 2019 localization-support.hpi
drwxr-xr-x 5 jenkins jenkins 70 Aug 27 13:50 localization-zh-cn
-rw-r--r-- 1 jenkins jenkins 551108 Nov 16 2020 localization-zh-cn.hpi
drwxr-xr-x 4 jenkins jenkins 56 Aug 27 13:50 mina-sshd-api-common
-rw-r--r-- 1 jenkins jenkins 834621 Aug 27 12:19 mina-sshd-api-common.jpi
drwxr-xr-x 4 jenkins jenkins 56 Aug 27 13:50 mina-sshd-api-core
-rw-r--r-- 1 jenkins jenkins 851806 Aug 27 12:19 mina-sshd-api-core.jpi
drwxr-xr-x 4 jenkins jenkins 56 Aug 27 13:50 sshd
-rw-r--r-- 1 jenkins jenkins 92375 Aug 27 12:19 sshd.jpi
drwxr-xr-x 4 jenkins jenkins 56 Aug 27 13:50 trilead-api
-rw-r--r-- 1 jenkins jenkins 2314046 Aug 27 12:19 trilead-api.jpi
# 访问一下看是否汉化
方式三:
# 直接使用这个包
plugins.tar.gz
# 解压
[root@jenkins plugins]# tar xf plugins.tar.gz
# 重启
[root@jenkins plugins]# systemctl restart jenkins
# 访问查看
自己更新插件
先删除原来的plugins目录
# 上传大礼包jenkins_plugins.tar.gz
# 解压
[root@jenkins jenkins]# tar xf jenkins_plugins.tar.gz
# 授权
[root@jenkins jenkins]# chown -R jenkins.jenkins plugins/
# 把ruby-runtime和ruby-runtime.jpi移走
[root@jenkins plugins]# mv ruby-runtime* /tmp
# 重启
[root@jenkins plugins]# systemctl restart jenkins
# 访问查看是否汉化
插件
更新插件
blueocean-core-js
blueocean for Blue Ocean
Gitlab logo
Gitlab API
Localization Support
Localization Chinese
Maven integration
在加一个tag的插件,然后要重启
git
gitlab
pipeline
maven
ssh
----------可装可不装----------
blue ocean
sonar
jenkins自由风格
jenkins想要去gitlab中拉代码条件
jenkins想要去gitlab中拉代码:
1)jenkins能识别gitlab的域名
2)jenkins要将公钥放入gitlab中
3)jenkins机器上要有git命令
没有参数的
添加git仓库URL
报错原因:jenkins服务器上没有git命令
[root@jenkins jenkins]# yum install -y git
建立连接
-新版本:
-老版本要加私钥
报错原因:没有将jenkins公钥放入gitlab
# 生成公钥
[root@jenkins ~]# ssh-keygen
# 查看公钥
[root@jenkins ~]# cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDFVWKV8eFDLhI2QrTl/mbSxlnnq9WCqd4RNDSQr4kECdnnI2aXSJ0kVCalxlVNATkOvtAkCLE3ZF9idXI4x7A8klnvJOp2uBINysVc58OF8cTv1Fbx8N/Gv7QGEf1oCCqnFnUd0FHcHX3Ya/PFt8lBrKgVgJCzHI+FGQvY6641i4aYjtxES8wcskAI12r1XQLzx70sh0j7L340buWnzvgHoJVpuRdNIlibhv0k/RsCa5dRbuBQ31HqSRqP6ENyfkJrcx61csAWVHpTLLb4X4hqDYk7ZP1lxHSIo78nuI9GeG7NMeoIiElf9MEMOm93W8LL2hX+BtdjTrSfXd2gV6fl root@jenkins
# 把公钥放入gitlab
报错原因:jenkins放入公钥和私钥都不好使
因为jenkins服务使用jenkins用户启动的,放入的是root密钥对,无法使用
方法一:jenkins使用root启动(企业中不会使用)
[root@jenkins jenkins]# vim /etc/sysconfig/jenkins
JENKINS_USER="root"
推荐使用:#方法二:给jenkins用户创建密钥对
[root@jenkins jenkins]# usermod jenkins -s /bin/bash
[root@jenkins jenkins]# su - jenkins
-bash-4.2$ ssh-keygen
-bash-4.2$ cat .ssh/id_rsa.pub #(把Jenkins用户的公钥加到gitlab里)
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCwrm+iKuwVBe/2C2mVJTy7kwKEnNnMgMR+AR1xD840ou2IQ+ad5hJrMMHuxZj1dVLNqdYsekJ+316s+9g5pCgPuwZTgVSkLHxcL8t51Cr74FR4bK3s2ctMVpzO/QV3AVaitBvcZ7Vmxbx6D6Hicd4XIRjTRFeVKfnbLztDQaJ+Fp72Yy+XfBax7J2EkdDzdWJ+BlUkJtL1O/t2d5Nxbei5THq6vMhwpEguaMmf6f0v748sn9GDT18MbV7THWI1tfxL/Que+ksUOIeOqRweIhfy5QI6VgIIuszdXIdlb8nOyYSGarshUudT/xSxaqp8RJg70cGx4RBwHgpqmITn/LVp jenkins@jenkins
# 查看私钥
-bash-4.2$ cat .ssh/id_rsa #(私钥加到Jenkins里,操作如下)
指定分支
Build Steps
jenkins用户su - jenkins
# jenkins需要和web端免密
-bash-4.2$ ssh-copy-id -i .ssh/id_rsa.pub root@172.16.1.7
# 连接一下看看能不能连上
-bash-4.2$ ssh root@172.16.1.7
Last login: Sun Aug 27 18:07:46 2023 from 172.16.1.92
[root@web01 ~]# logout
cd $WORKSPACE &&\
tar zcf web.tar.gz ./* &&\
scp web.tar.gz root@172.16.1.7:/www/code &&\
ssh root@172.16.1.7 "cd /www/code && tar xf web.tar.gz"
echo "git_commit: $GIT_COMMIT"
echo "job_namr: $JOB_NAME"
echo "workspace: $WORKSPACE"
修改:
code_dir='/www/code'
cd $WORKSPACE &&\
tar zcf web_${GIT_COMMIT}.tar.gz ./* &&\
for ip in 7,8,9;do
scp web_${GIT_COMMIT}.tar.gz root@172.16.1.${ip}:${code_dir} &&\
ssh root@172.16.1.${ip} "cd /www/code && tar xf web_${GIT_COMMIT}.tar.gz"
done
echo "git_commit: $GIT_COMMIT"
echo "job_name: $JOB_NAME"
echo "workspace: $WORKSPACE"
# 修改
code_dir='/www/code'
cd $WORKSPACE &&\
tar zcf web_${GIT_COMMIT}.tar.gz ./* &&\
if [$env == 'test' ];then
echo $env
scp web_${GIT_COMMIT}.tar.gz root@172.16.1.${ip}:${code_dir} &&\
ssh root@172.16.1.${ip} "cd /www/code && tar xf web_${GIT_COMMIT}.tar.gz"
elif [$env == 'dev' ];then
echo $env
else
echo $env
fi
echo "git_commit: $GIT_COMMIT"
echo "job_name: $JOB_NAME"
echo "workspace: $WORKSPACE"
参数化构建
这样选复制的话,General里的一些操作就不需要重复点选,在加一个参数化构建即可
打标签
[root@gitlab code]# git tag -a 'v1.1' -m 4dfad0c4e9c
[root@gitlab code]# git tag -a 'v1.2' -m a703a97b6ca
[root@gitlab code]# git push --tag
Counting objects: 2, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 204 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 0 (delta 0)
To git@10.0.0.91:root/code.git
* [new tag] v1.1 -> v1.1
* [new tag] v1.2 -> v1.2
参数化构建
test 测试环境
dev 开发环境
prod 生成环境
拓展
gitlab的钩子(自动触发代码上线)
gitlab钩子(触发器)功能触发jenkins的webhook gitlab调用jenkins接口(API)
## 实现全自动,开发提交了代码之后,自动将代码部署到测试环境
#1.Jenkins触发器功能:Jenkins通知地址
http://10.0.0.92:8080/project/freestyle-code-webhook
#2.Jenkins获取令牌:Jenkins生成tocken令牌
5e1f9d80b1420e9cde422445a2cc14ba
#3.gitlab开启功能
你的工作-管理中心-设置-网络设置-外发请求-允许钩子和服务访问本地网络
#4.gitlab配置url地址和令牌
找到对应项目-设置-集成(新版本webhooks)
- 链接:Jenkins通知地址
- 令牌:Jenkins对应令牌
#5.开发上传代码,web检查结果
配置完点击测试,看看webhook有没有问题
配置成功后,如果有修改代码的操作,只要提交到代码仓库,就会自动去构建,写执行shell脚本时一定要设置为测试环境
# 修改代码操作
[root@gitlab code]# vim style.css
[root@gitlab code]# git add .
[root@gitlab code]# git commit -m '官网蓝色'
[master 3b3ea08] 官网蓝色
1 file changed, 1 insertion(+), 1 deletion(-)
[root@gitlab code]# git tag -a 'v1.4' -m '官网蓝色'
[root@gitlab code]# git push --all
Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 299 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To git@10.0.0.91:root/code.git
72f51f7..3b3ea08 master -> master
[root@gitlab code]# git push --tag
Counting objects: 1, done.
Writing objects: 100% (1/1), 177 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To git@10.0.0.91:root/code.git
* [new tag] v1.4 -> v1.4