安装
安装&运行
直接使用Java进行启动:
这样可以自定义Jenkins的启动用户、工作和文件存放目录。
groupadd -g 498 jenkins
useradd -g jenkins -u 498 -s /bin/false -d /data/jenkins -c "Continuous Integration Server" -M jenkins
mkdir -p /data/jenkins/{app,logs,.m2}
cp -p jenkins.war /data/jenkins/app/
chown -R jenkins:jenkins /data/jenkins
su - jenkins -s /bin/bash -c "cd /data/jenkins/app ; java -server -Xms128m -Xmx1024m -jar jenkins.war -DJENKINS_HOME=/data/jenkins --httpPort=8080 --logfile=/data/jenkins/logs/jenkins.$(date +%Y-%m-%d).log > /dev/null" &
tailf /data/jenkins/logs/jenkins.log
mvn用户配置文件
建立配置文件,设置mvn仓库使用本地nexus私服。
vim /data/jenkins/.m2/settings.xml
<?xml version="1.0" encoding="UTF-8"?>
<settings>
<profiles>
<profile>
<id>nexus</id>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
</properties>
<repositories>
<repository>
<id>local-nexus</id>
<url>http://127.0.0.1:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>local-nexus</id>
<url>http://127.0.0.1:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
由于我们不直接在全局的settings.xml中插入<repositories>元素,这里添加了一个profile并使用<activeProfile>元素自动将这个profile激活。这里的local-nexus仓库指向了Nexus中默认的“maven-public”仓库组,即所有该仓库组包含的仓库都能供我们使用。此外,通过<releases>和<snapshots>元素激活了Maven对于仓库所有类型构件下载的支持,当然你也可以调节该配置,比如说禁止Maven从Nexus下载snapshot构件。
并且设置指定了maven构建所使用JAVA编译器 -source 和 -target 的版本为1.7。
使用该配置,Maven就会从Nexus服务器下载构件了。
初始化
第一次启动时,日志中会提示程序需要初始化操作,并会生成一段随机密码,用于进行安装操作:
其会在执行用户的宿主目录下创建.jenkins隐藏目录,里面存放相关配置信息。
访问其监听地址进行操作:http://ipaddr:8080/
输入上面生成的密码后继续。
进入自定义Jenkins页面,第一个选项是安装建议插件(使用最多的),第二个选项是自己选择需要安装的插件。根据需要选择即可,然后其会联网进行插件下载安装:
完成之后 ,会提示创建初始管理员账号:
创建好后即完成安装:
Tips:初始化完成之后,用于初始化的密码文件会自动删除。
设置为服务
init.d方式:
vim /etc/init.d/jenkins
#!/bin/bash
# Jenkins Automation Server.
# chkconfig: 2345 80 30
# description: Jenkins is a Open Source Automation Server.
# processname: java
### BEGIN INIT INFO
# Provides: jenkins
# Required-Start: $local_fs $network $remote_fs
# Required-Stop: $local_fs $network $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop jenkins
# Description: Jenkins is a Automation Server.
### END INIT INFO
# Defined Color
Red='\033[31m\033[1m'
Green='\033[32m\033[1m'
Null='\033[0m'
#----Script Config Begin----
SrN="Jenkins"
runuser=jenkins
basedir=/data/jenkins
bindir=/usr/local/jdk1.8.0_162/bin/
execute=${bindir}/java
args="-server -Xms128m -Xmx1024m -jar jenkins.war -DJENKINS_HOME=${basedir} --httpPort=8080 --logfile=${basedir}/logs/jenkins.$(date +%Y-%m-%d).log"
pidpath="/tmp/hsperfdata_jenkins/"
#----Script Config End------
if [ ! -x "${execute}" ];then
echo "Can't execute ${execute} from dir ${bindir}"
exit 1
fi
# Load Env
source /etc/profile
chkpid () {
cd ${pidpath}
if [ -f "$(ls)" ];then
pid=$(ls)
pidfile="${pidpath}/${pid}"
if [ ! -z "${pid}" ];then
pidtest=`ps -p ${pid} -o args | grep "${args}" | tail -1`
if [ -z "$pidtest" ];then
rm -f "${pidfile}"
echo -e "${Red}Removed stale pid file: ${pidfile}${Null}"
fi
fi
fi
}
Start () {
chkpid
[ -f "${pidfile}" ] && echo -e ${SrN} Already ${Green}Running${Null}\(PID: ${pid}\) && exit 1
echo -n "Starting ${SrN}: "
su - ${runuser} -s /bin/bash -c "cd ${basedir}/app ; ${execute} ${args} > /dev/null" &
RETVAL=$?
[ $RETVAL -eq 0 ] && echo -e ${Green}Success${Null} || echo -e ${Red}Failure${Null}
return $RETVAL
}
Stop() {
chkpid
if [ -f "${pidfile}" ];then
echo -n "Stoping ${SrN}: "
kill ${pid}
RETVAL=$?
[ $RETVAL -eq 0 ] && rm -f $pidfile
[ $RETVAL -eq 0 ] && echo -e ${Green}Success${Null} || echo -e ${Red}Failure${Null}
return $RETVAL
else
echo -e "${Red}${SrN} is not running.${Null}"
fi
}
Status() {
chkpid
if [ -f "${pidfile}" ];then
echo -e ${SrN} ${Green}RUNNING${Null} \(PID: ${pid}\)
else
echo -e ${SrN} ${Red}DEAD${Null}
fi
}
case "$1" in
'start' )
Start
;;
'stop' )
Stop
;;
'status')
Status
;;
'restart' )
Stop
Start
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
exit 2
;;
esac
exit $?
chmod +x /etc/init.d/jenkins chkconfig --add /etc/init.d/jenkins service jenkins start service jenkins status





