参考资料
Jenkins用户手册 https://jenkins.io/zh/doc/
安装 jenkins
https://jenkins.io/zh/download/
https://pkg.jenkins.io/redhat-stable/
- yum安装:
wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo
rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
yum install jenkins
- rpm包安装指定版本:
rpm -ivh jenkins-2.138-1.1.noarch.rpm
2.138版本的中文比较好一些。
- 安装Java
注意如果服务器上已经安装了java,注意下Java版本。参照手册
yum install java-1.8.0-openjdk
[root@zhangxiu-test ~]# java -version
openjdk version "1.8.0_201"
OpenJDK Runtime Environment (build 1.8.0_201-b09)
OpenJDK 64-Bit Server VM (build 25.201-b09, mixed mode)
- 启动与停止
service jenkins start/stop/restart
chkconfig jenkins on
配置
- 通过浏览器访问8080端口(可通过修改配置文件
/etc/sysconfig/jenkins
更改端口)。 - 按照引导输入密码
/var/lib/jenkins/secrets/initialAdminPassword
- 配置角色权限 需要用到插件 Role-based Authorization Strategy
- 配置git代码凭据 需要用到 Git client plugin,配置使用那个git分支需要用到 Git Parameter
- 添加git凭据: 凭据=》系统=》全局凭据=》添加凭据。选择 ssh username with private key
- 常用插件
Git client plugin
Utility plugin for Git support in Jenkins. 集成git
Git Parameter
Adds ability to choose branches, tags or revisions from git repositories configured in project. 可以选择使用git哪个分支
publish over ssh
Deploy to container
Dingding[钉钉]
钉钉Jenkins通知器
Mailer Plugin
This plugin allows you to configure email notifications for build results
邮件发送构建结果
Email Extension
This plugin is a replacement for Jenkins's email publisher. It allows to configure every aspect of email notifications: when an email is sent, who should receive it and what the email says
扩展了发送告警邮件的控制力度。可以定义邮件触发器、邮件内容、收件人
Email Extension Template
Role-based Authorization Strategy
Enables user authorization using a Role-Based strategy. Roles can be defined globally or for particular jobs or nodes selected by regular expressions.
用户角色权限控制
Matrix Authorization Strategy Plugin
Offers matrix-based security authorization strategies (global and per-project). 为每个项目设置用户权限
Backup plugin
Backup or restore your Hudson configuration files
jenkins服务器迁移
新服务器安装相同版本的jenkins,然后同步主目录下的部分文件到新的服务器。
rsync -avrl --progress /var/lib/jenkins/ root@192.168.1.2:/var/lib/jenkins/ --include-from=/root/include.txt --exclude=/*
include.txt
内容如下
config.xml
credentials-configuration.xml
credentials.xml
jobs/
plugins/
secrets/
users/
workspace/
jenkins版本号可在“系统管理=》关于Jenkins”查看
主目录位置 可在“系统管理=》系统设置”中查看。默认为/var/lib/jenkins/
同步完毕之后注意修改一下jenkins主目录的所有者,不然会启动失败。
常见问题
-
停用权限控制插件(Role-based Authorization Strategy)后系统不能运行
cd /var/lib/jenkins/
#备份配置文件
cp config.xml config.xml.bak
vim config.xml
#useSecurity 改为false
# 去掉关于权限控制的 authorizationStrategy 和 securityRealm
service jenkins restart
# 启动服务后 开启权限控制插件,重启服务
cp config.xml.bak config.xml
service jenkins restart
- 构建失败 git Host key verification failed.
在jenkins服务器,切换用户su -s /bin/bash jenkins
执行ssh-keygen -t rsa
生成密钥,把公钥上传到gitlab
执行git ls-remote -h git@code.aliyun.com:dxk/easy.git
生成host key