使用yum安装Jenkins
一、安装之前
1.安装Jenkins所依赖的jdk
yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
二、安装
2.安装方法1
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
yum install -y jenkins
#安装方法2
wget https://pkg.jenkins.io/redhat/jenkins-2.156-1.1.noarch.rpm
rpm -ivh jenkins-2.156-1.1.noarch.rpm
三、创建文件与授权
mkdir -p /var/lib/jenkins
mkdir -p /var/cache/jenkins
mkdir -p /var/log/jenkins
chown -R root:root /var/lib/jenkins
chown -R root:root /var/cache/jenkins
chown -R root:root /var/log/jenkins
四、设置调整防火墙
FirewallD是Centos 8上的默认防火墙解决方案
在安装过程中,Nginx使用预定义的规则创建防火墙服务文件,以允许访问HTTP(80)和 HTTPS(443) 端口
3.使用以下命令永久打开必要的端口
systemctl start firewalld
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --zone=public --add-port=7001/tcp --permanent
firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --zone=public --add-port=8888/tcp --permanent
firewall-cmd --zone=public --add-port=27017/tcp --permanent
sudo firewall-cmd --reload
systemctl restart firewalld
五、设置jenkins配置
输入命令 vim /etc/sysconfig/jenkins 进入编辑模式 ggdG 全删 ggyG 全部复制 ggvG ggVG 高亮显示
## Path: Development/Jenkins
## Description: Jenkins Automation Server
## Type: string
## Default: "/var/lib/jenkins"
## ServiceRestart: jenkins
#
# Directory where Jenkins store its configuration and working
# files (checkouts, build reports, artifacts, ...).
#
JENKINS_HOME="/var/lib/jenkins"
## Type: string
## Default: "jenkins"
## ServiceRestart: jenkins
## 修改用户
# Unix user account that runs the Jenkins daemon
# Be careful when you change this, as you need to update
# permissions of $JENKINS_HOME and /var/log/jenkins.
#
JENKINS_USER="root"
## Type: integer(0:65535)
## Default: 8080
## ServiceRestart: jenkins
## 监听端口
# Port Jenkins is listening on.
# Set to -1 to disable
#
JENKINS_PORT="8888"
## Type: string
## Default: ""
## ServiceRestart: jenkins
#
# Java executable to run Jenkins
# When left empty, we'll try to find the suitable Java.
#
JENKINS_JAVA_CMD=""
## Type: string
## Default: "false"
## ServiceRestart: jenkins
#
# Whether to skip potentially long-running chown at the
# $JENKINS_HOME location. Do not enable this, "true", unless
# you know what you're doing. See JENKINS-23273.
#
#JENKINS_INSTALL_SKIP_CHOWN="false"
## Type: string
## Default: "-Djava.awt.headless=true"
## ServiceRestart: jenkins
#
# Options to pass to java when running Jenkins.
#
JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true"
## Type: string
## Default: ""
## ServiceRestart: jenkins
#
# IP address Jenkins listens on for HTTP requests.
# Default is all interfaces (0.0.0.0).
#
JENKINS_LISTEN_ADDRESS=""
## Type: integer(0:65535)
## Default: ""
## ServiceRestart: jenkins
#
# HTTPS port Jenkins is listening on.
# Default is disabled.
#
JENKINS_HTTPS_PORT=""
## Type: string
## Default: ""
## ServiceRestart: jenkins
#
# Path to the keystore in JKS format (as created by the JDK 'keytool').
# Default is disabled.
#
JENKINS_HTTPS_KEYSTORE=""
## Type: string
## Default: ""
## ServiceRestart: jenkins
#
# Password to access the keystore defined in JENKINS_HTTPS_KEYSTORE.
# Default is disabled.
#
JENKINS_HTTPS_KEYSTORE_PASSWORD=""
## Type: string
## Default: ""
## ServiceRestart: jenkins
#
# IP address Jenkins listens on for HTTPS requests.
# Default is disabled.
#
JENKINS_HTTPS_LISTEN_ADDRESS=""
## Type: integer(0:65535)
## Default: ""
## ServiceRestart: jenkins
#
# HTTP2 port Jenkins is listening on.
# Default is disabled.
#
# Notice: HTTP2 support may require additional configuration, see Winstone
# documentation for more information.
#
JENKINS_HTTP2_PORT=""
## Type: string
## Default: ""
## ServiceRestart: jenkins
#
# IP address Jenkins listens on for HTTP2 requests.
# Default is disabled.
#
# Notice: HTTP2 support may require additional configuration, see Winstone
# documentation for more information.
#
JENKINS_HTTP2_LISTEN_ADDRESS=""
## Type: integer(1:9)
## Default: 5
## ServiceRestart: jenkins
#
# Debug level for logs -- the higher the value, the more verbose.
# 5 is INFO.
#
JENKINS_DEBUG_LEVEL="5"
## Type: yesno
## Default: no
## ServiceRestart: jenkins
#
# Whether to enable access logging or not.
#
JENKINS_ENABLE_ACCESS_LOG="no"
## Type: integer
## Default: 100
## ServiceRestart: jenkins
#
# Maximum number of HTTP worker threads.
#
JENKINS_HANDLER_MAX="100"
## Type: integer
## Default: 20
## ServiceRestart: jenkins
#
# Maximum number of idle HTTP worker threads.
#
JENKINS_HANDLER_IDLE="20"
## Type: string
## Default: ""
## ServiceRestart: jenkins
#
# Folder for additional jar files to add to the Jetty class loader.
# See Winstone documentation for more information.
# Default is disabled.
#
JENKINS_EXTRA_LIB_FOLDER=""
## Type: string
## Default: ""
## ServiceRestart: jenkins
#
# Pass arbitrary arguments to Jenkins.
# Full option list: java -jar jenkins.war --help
#
JENKINS_ARGS=""
六、启动
6.设置启动与开机启动
sudo systemctl start jenkins && sudo systemctl enable jenkins
#或 sudo service jenkins start && chkconfig jenkins on
#启动
sudo systemctl start jenkins
#或 service jenkins start
#重启
sudo systemctl restart jenkins
#或 service jenkins restart
#开机启动
sudo systemctl enable jenkins
#或 sudo service jenkins star
#关闭开机启动
sudo systemctl disable jenkins
#或 sudo chkconfig jenkins off
快速部署Jenkins
yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
yum install -y jenkins
mkdir -p /var/lib/jenkins
mkdir -p /var/cache/jenkins
mkdir -p /var/log/jenkins
chown -R root:root /var/lib/jenkins
chown -R root:root /var/cache/jenkins
chown -R root:root /var/log/jenkins
systemctl start firewalld
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --zone=public --add-port=7001/tcp --permanent
firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --zone=public --add-port=8888/tcp --permanent
firewall-cmd --zone=public --add-port=27017/tcp --permanent
sudo firewall-cmd --reload
systemctl restart firewalld
查看
#执行命令查看密码
cat /var/lib/jenkins/secrets/initialAdminPassword
#检查是否已安装JDK,以下命令二选一,中括号选一即可
yum list installed | grep [java][jdk]
rpm -qa | grep [java][jdk][gcj]
#查看centos系统版本
cat /etc/redhat-release
#查看部署的库主目录
ls /var/lib/jenkins/
修改密码
1. 查找jenkins的路径
#方法1:
[root@iZedbi0f3jvy9lZ ~]# whereis jenkins
jenkins: /usr/lib/jenkins
#方法2:
[root@iZedbi0f3jvy9lZ ~]# find / -name config.xml
/var/lib/jenkins/jobs/gitee test/config.xml
/var/lib/jenkins/users/kim_1794366939214431569/config.xml
/var/lib/jenkins/config.xml
2. 修改目录
#方法1:
修改config.xml文件
找到关键词 useSecurity, 把后面的值 true 改为false
可以无账号和密码登录Jenkins
#方法2:
打开该配置文件找到<passwordHash>节点
将#jbcrypt:后面的内容替换成
$2a$10$d6oDwJRKYlWQMOKhW/Mi0eNq4xs/utxvfHLeWBUeeXbQLN9vpNhuK
即可
上面那串如何生成的呢?
Jenkins采用Java加密工具jBCrypt,所以我们在生成密码的时候需要添加相关的jar包。
<dependency>
<groupId>org.mindrot</groupId>
<artifactId>jbcrypt</artifactId>
<version>0.4</version>
</dependency>
public class Test {
public static void main(String[] args) {
// 加密
String admin = BCrypt.hashpw("admin", BCrypt.gensalt());
System.out.println("加密:" + admin);
// 解密
if (BCrypt.checkpw("admin", admin)) {
System.out.println("is match");
} else {
System.out.println("is not match");
}
}
}
3. 重启 Jenkins
/etc/init.d/jenkins restart
或
sudo systemctl restart jenkins
插件
Git Parameter
Extended Choice Parameter
#覆盖内容
echo "abc==master,develop,test" > /opt/jenkins.properties
#追加内容
echo "abc=master,develop,test" >> /opt/jenkins.properties
#cat 用户保存下的路径/jenkins.property > /opt/jenkins.property
错误解决
错误信息为Starting Jenkins bash: /usr/bin/java: No such file or directory是java环境配置的问题
yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
#或者如下命令,安装jdk1.8.0的所有文件
#yum install -y java-1.8.0-openjdk*
#ln -s /usr/local/java/jdk1.8.0_171/bin/java /usr/bin/java