Jenkins是用java编写的开源持续集成工具,目前被国内外各公司广泛使用。本章教大家如何在linux服务器上安装Jenkins。
需要先安装jdk
1.查看jdk版本,输入java -version,如果不是jdk1.8以上,需要安装jdk1.8。
2.新建/usr/java文件夹,下载jdk-8u131-linux-x64.gz,放在该目录下。
3.解压jdk,命令输入 tar -zxvf jdk-8u131-linux-x64.gz。
4.配置环境变量,使用vi /etc/profile编辑profile文件,在/etc/profile底部加入下面内容:
export JAVA_HOME=/usr/java/jdk1.8.0_131
export CLASSPATH=.:$JAVA_HOME/lib:$CLASSPATH
export PATH=$PATH:$JAVA_HOME/bin
保存并退出编辑。
使用source /etc/profile让profile文件生效
三、yum安装Jenkins
这里推荐使用yum方式安装Jenkins,最简单方便。
yum的repos中默认是没有Jenkins的,需要先将Jenkins存储库添加到yum repos。
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安装Jenkins
yum install jenkins
随后就可以看到系统开始自动下载安装。
出现提示是否下载的时候输入y
并按回车。
耐心等待安装完成。
现在Jenkins已经安装好了,不过启动之前最好稍微修改一下配置。默认情况是Jenkins是使用Jenkins
用户启动的,但这个用户目前系统并没有赋予权限,这里我们将启动用户修改为root
;另外Jenkins默认端口是8080,这个跟tomcat的默认端口冲突,我们也修改一下默认端口。
输入命令进入Jenkins配置文件
## 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: ""
## ServiceRestart: jenkins
#
# Java executable to run Jenkins
# When left empty, we'll try to find the suitable Java.
#
JENKINS_JAVA_CMD=""
## 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="jenkins"
JENKINS_USER="root" #修改启动用户为root
## 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: integer(0:65535)
## Default: 8080
## ServiceRestart: jenkins
#
# Port Jenkins is listening on.
# Set to -1 to disable
#
#JENKINS_PORT="8080"
JENKINS_PORT="10000" #修改端口为10000
## 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(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
#
# Pass arbitrary arguments to Jenkins.
# Full option list: java -jar jenkins.war --help
#
JENKINS_ARGS=""
修改完配置后,保存退出。
好了,现在配置文件也修改好了,可以启动Jenkins了。
输入启动命令以启动Jenkins服务。
service jenkins start
注意: 启动如果报:
Starting jenkins (via systemctl): Job for jenkins.service failed because the control process exited with error code. See “systemctl status jenkins.service” and “journalctl -xe” for details.
按提示输入:systemctl status jenkins.service
● jenkins.service - LSB: Jenkins Automation Server
Loaded: loaded (/etc/rc.d/init.d/jenkins; bad; vendor preset: disabled)
Active: failed (Result: exit-code) since Sat 2019-02-16 15:07:46 CST; 39s ago
Docs: man:systemd-sysv-generator(8)
Process: 16856 ExecStart=/etc/rc.d/init.d/jenkins start (code=exited, status=1/FAILURE)
Feb 16 15:07:46 localhost.localdomain systemd[1]: Starting LSB: Jenkins Automation Server...
Feb 16 15:07:46 localhost.localdomain runuser[16861]: pam_unix(runuser:session): session opened for user r...=0)
Feb 16 15:07:46 localhost.localdomain jenkins[16856]: Starting Jenkins bash: /usr/bin/java: No such file o...ory
Feb 16 15:07:46 localhost.localdomain jenkins[16856]: [FAILED]
Feb 16 15:07:46 localhost.localdomain systemd[1]: jenkins.service: control process exited, code=exited status=1
Feb 16 15:07:46 localhost.localdomain systemd[1]: Failed to start LSB: Jenkins Automation Server.
Feb 16 15:07:46 localhost.localdomain systemd[1]: Unit jenkins.service entered failed state.
Feb 16 15:07:46 localhost.localdomain systemd[1]: jenkins.service failed.
Hint: Some lines were ellipsized, use -l to show in full.
解决办法
意思就是没有找到java文件,需要自己把java文件地址复制过去
修改/etc/init.d/jenkins文件
# Search usable Java as /usr/bin/java might not point to minimal version required by Jenkins.
# see http://www.nabble.com/guinea-pigs-wanted-----Hudson-RPM-for-RedHat-Linux-td25673707.html
candidates="
/etc/alternatives/java
/usr/lib/jvm/java-1.8.0/bin/java
/usr/lib/jvm/jre-1.8.0/bin/java
/usr/lib/jvm/java-1.7.0/bin/java
/usr/lib/jvm/jre-1.7.0/bin/java
/usr/bin/java
"
然后再执行 启动命令
出现OK
表示Jenkins启动成功。
在浏览器输入ip:10000
进入Jenkins登录页面。
进入登录页面后,Jenkins提示我们需要输入超级管理员密码进行解锁。根据提示,我们可以在/var/lib/jenkins/secrets/initialAdminPassword
文件里找到密码。
输入命令以找到密码。
tail /var/lib/jenkins/secrets/initialAdminPassword
找到密码后,复制密码,粘贴到Jenkins解锁页面,点击Continue
继续初始化配置。短暂的等待后,进入插件安装页面
这里我们点击的Install suggested plugins
,安装默认插件,当然你也可以点击另一个按钮安装指定的插件
点击后,页面进入了插件下载安装页面
安装插件,有时候会报类似的错误:An error occurred during installation: No such plugin: cloudbees-folder
上面的错误显示是,安装插件cloudbees-folder失败,是因为下载的Jenkins.war里没有cloudbees-folder插件,
需要在网上下载:http://ftp.icm.edu.pl/packages/jenkins/plugins/cloudbees-folder/
下载cloudbees-folder.hpi放在 /var/lib/jenkins/plugins 即可
耐心等待全部安装完成。安装完成后,页面自动进入了管理员账户注册页面
输入完信息点击Save and Finish
。
至此,Jenkins安装并初始化完成了