本文环境:
Windows 10 + CentOS 6.8
为Jenkins创建特殊的用户和组
groupadd build
useradd --create-home --shell /bin/bash --groups build jenkins
为Jenkins用户配置Java
# 编辑用户的.bashrc
# set java env
export JAVA_HOME=/usr/java/jdk1.7.0_79
export PATH=$JAVA_HOME/bin:$PATH
运行Jenkins
# 下载最新稳定版的Jenkins war包,放到/usr/local/src目录下
java -jar /usr/local/src/jenkins.war # 启动Jenkins
停止Jenkins,按Ctrl+C 组合键
Jenkins默认运行在8080,可以手动制定端口,及常用参数。
--httpPort --ajp13Port# 指定端口
java -jar /usr/local/src/jenkins.war --httpPort=8081
--daemon # 以后台任务形式启动jenkins
--logfile # 指定jenkins启动日志路径
mkdir /var/log/jenkins
java -jar /usr/local/src/jenkins.war --logfile=/var/log/jenkins/jenkins.log
调用内嵌的Winstone应用服务器运行Jenkins
调用Winstone应用服务器来重启或关闭Jenkins,需要启动Jenkins时指定controlPort选项,示例:
java -jar /usr/local/src/jenkins.war --controlPort=8001
Linux环境下完整示例:
nohup java -jar /usr/local/src/jenkins.war --controlPort=8001 > /var/log/jenkins/jenkins.log 2>&1 &
重启服务器,示例
export JENKINS_HOME=/home/jenkins/.jenkins
java -cp $JENKINS_HOME/war/winstone.jar winstone.tools.WinstoneControl reload: --host=jenkins --port=8001 # reload 报错
java -cp $JENKINS_HOME/war/winstone.jar winstone.tools.WinstoneControl shutdown --host=jenkins --port=8001 # shutdown正常
创建启动Jenkins的daemon脚本
Linux环境下Jenkins的daemon脚本,提供Jenkins启动、停止、重新启动功能,已在CentOS6.x上测试,根据相关环境变量设置适合你自己系统的环境变量。
本文实验环境:
OS:CentOS release 6.8 (Final)
JDK:jdk-7u79-linux-x64.rpm
基于CentOS安装测试
# 下载tomcat
wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-7/v7.0.73/bin/apache-tomcat-7.0.73.tar.gz -P /usr/local/src
tar zxvf /usr/local/src/apache-tomcat-7.0.73.tar.gz -C /home/jenkins
chown -R jenkins:build /home/jenkins/apache-tomcat-7.0.73
# 由于本文前半部分启动Jenkins使用的是指定用户Jenkins,因此使用Jenkins用户启动的目录改变所属,如果是采用以Tomcat来启动Jenkins,无需使用特定用户,这里看心情定
# 下载JDK,安装省略,已安装
脚本内容如下:
#!/bin/sh
#
# Startup script for the Jenkins Continuous Integration server
# (via Jakarta Tomcat Java Servlets and JSP server)
#
# chkconfig: - 85 15
# description: Jakarta Tomcat Java Servlets and JSP server
# processname: jenkins
# pidfile: /home/jenkins/jenkins-tomcat.pid
# Set Tomcat environment.
JENKINS_USER=jenkins
LOCKFILE=/var/lock/jenkins
export PATH=/usr/local/bin:$PATH
export HOME=/home/jenkins
export JAVA_HOME=/usr/java/jdk1.7.0_79
export JENKINS_BASEDIR=/home/jenkins
export TOMCAT_HOME=$JENKINS_BASEDIR/apache-tomcat-7.0.73
export CATALINA_PID=$JENKINS_BASEDIR/jenkins-tomcat.pid
export CATALINA_OPTS="-DJENKINS_HOME=$JENKINS_BASEDIR/jenkins-home -Xmx512m -Djava.awt.headless=true"
# Source function library.
. /etc/rc.d/init.d/functions
[ -f $TOMCAT_HOME/bin/catalina.sh ] || exit 0
export PATH=$PATH:/usr/bin:/usr/local/bin
# See how we were called.
case "$1" in
start)
# Start daemon.
echo -n "Starting Tomcat: "
su -p -s /bin/sh $JENKINS_USER -c "$TOMCAT_HOME/bin/catalina.sh start"
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch $LOCKFILE
;;
stop)
# Stop daemons.
echo -n "Shutting down Tomcat: "
su -p -s /bin/sh $JENKINS_USER -c "$TOMCAT_HOME/bin/catalina.sh stop"
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f $LOCKFILE
;;
restart)
$0 stop
$0 start
;;
condrestart)
[ -e $LOCKFILE ] && $0 restart
;;
status)
status -p $CATALINA_PID -l $(basename $LOCKFILE) jenkins
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
esac
exit 0
chmod a+x /etc/init.d/jenkins
chkconfig jenkins on
使用ntsysv查看Jenkins服务是否已启动
使用Jenkins Daemon启动Jenkins
/etc/init.d/jenkins start # 启动Tomcat、Jenkins
查看Jenkins是否被正常启动
cd /home/jenkins/apache-tomcat-7.0.73/logs
从图中可以看出Jenkins已经启动成功,从客户端浏览器访问如下:http://192.168.0.56:8080/jenkins
到这里,使用Tomcat+Daemon方式启动Jenkins已经完成,调整Tomcat的工作模式,优化参数来更好的使用Jenkins
查看admin初始密码,并登录Jenkins安装插件并使用
cat /home/jenkins/jenkins-home/secrets/initialAdminPassword
可以通过下面命令查看Jenkins安装插件过程后台输出的日志
tailf /home/jenkins/apache-tomcat-7.0.73/logs/catalina.out
Jenkins插件安装报错
SEVERE: Failed Loading plugin GitHub Branch Source Plugin v1.10.1 (github-branch-source)
SEVERE: Failed Loading plugin GitHub Organization Folder Plugin v1.5 (github-organization-folder)
解决报错:Failed Loading plugin GitHub Organization Folder Plugin v1.5
跳过报错,登录Jenkins后,重新安装插件
这里会提示你创建一个新的用户可以是管理员用户或者普通用户,未截图