转载请注明出处:http://blog.csdn.net/heng615975867/article/details/52759779

一.前言

在ubuntu 16.04 service版本上搭建jenkins来管理自动化编译,查阅了很多博客,发现都是ctrl+v的,到处踩坑,不断爬坑,所以出此博客帮助需要的同志少走弯路。

二.先安装JAVA JDK


check jdk:
xulongheng@ubuntu:/usr/lib/jvm$ java -version
openjdk version "1.8.0_91"
OpenJDK Runtime Environment (build 1.8.0_91-8u91-b14-3ubuntu1~16.04.1-b14)
OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode)


三.Jenkins安装


有两张安装方式,

方式一:war包安装


去Jenkins官网(https://jenkins.io/index.html)下载Jenkins.war 包  启动tomcat就可以了。
访问:http://localhost:8080/jenkins

方式二:apt install 安装


1.我们可以手动从这个链接下载稳定的版本, 添加Debian库和关键的系统。 这一步可以自动安装:
<pre name="code" class="java" style="color: rgb(85, 85, 85); line-height: 35px;font-size:18px;">xulongheng@ubuntu<span style="color: rgb(51, 51, 51); font-family: 'microsoft yahei', simhei, arial, sans-serif; font-size: 16px; line-height: 20px;">:~# sudo wget -q -O - http://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -</span>
OK
 
 
2.然后添加Debian pacakage到源清单 /etc/apt/sources.list.d
xulongheng@ubuntu:~# sudo sh -c 'echo deb http://pkg.jenkins-ci.org/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'

3.最后升级获取最新推出稳定版本的Jenkins
xulongheng@ubuntu:~# sudo apt-get update
<pre name="code" class="java" style="color: rgb(51, 51, 51); font-size: 16px; line-height: 20px;">xulongheng@ubuntu:<span style="font-family: 'microsoft yahei', simhei, arial, sans-serif;">~# sudo apt-get install jenkins</span>
 
  
4.服务的启动/停止:
<span style="font-size:18px;">sudo service jenkins start  
sudo service jenkins stop  </span>

5.路径:
访问路径:http://localhost:8080  
安装路径:/var/lib/jenkins  
日志路径:/var/log/jenkins
创建的项目目录当然在jenkins安装目录下的jobs里面。

6.启动Jenkins服务
<span style="font-size:18px;">xulongheng@ubuntu:~# service jenkins start
Job for jenkins.service failed because the control process exited with error code. See "systemctl status jenkins.service" and "journalctl -xe" for details.</span>

7.服务启动失败,按照提示排除错误原因

xulongheng@ubuntu:~# systemctl status jenkins.service
● jenkins.service - LSB: Start Jenkins at boot time
Loaded: loaded (/etc/init.d/jenkins; bad; vendor preset: enabled)
Active: failed (Result: exit-code) since Thu 2016-10-08 15:08:40 IST; 9s ago
Docs: man:systemd-sysv-generator(8)
Process: 25640 ExecStart=/etc/init.d/jenkins start (code=exited, status=7)

Oct 08 15:08:39 systemsguru systemd[1]: Starting LSB: Start Jenkins at boot time...
Oct 08 15:08:39 systemsguru jenkins[25640]: * Starting Jenkins Continuous Integration Server jenkins
Oct 08 15:08:39 systemsguru jenkins[25640]: The selected http port (8080) seems to be in use by another program
Oct 08 15:08:39 systemsguru jenkins[25640]: Please select another port to use for jenkins
Oct 08 15:08:40 systemsguru jenkins[25640]: ...fail!
Oct 08 15:08:40 systemsguru systemd[1]: jenkins.service: Control process exited, code=exited status=7
Oct 08 15:08:40 systemsguru systemd[1]: Failed to start LSB: Start Jenkins at boot time.
Oct 08 15:08:40 systemsguru systemd[1]: jenkins.service: Unit entered failed state.
Oct 08 15:08:40 systemsguru systemd[1]: jenkins.service: Failed with result 'exit-code'.
xulongheng@ubuntu:~# cat /etc/services | grep 8080
http-alt 8080/tcp webcache # WWW caching service
http-alt 8080/udp

8. 从日志看出Jenkins在默认情况下从8080端口启动。在这个系统端口中,8080已经在使用中了。所以在/etc/default/jenkins.修改Jenkins默认端口设置

xulongheng@ubuntu:~# nano /etc/default/jenkins

修改默认端口为  HTTP_PORT=8090


9.我们再来试试启动Jenkins

xulongheng@ubuntu:~$ sudo service jenkins start
[sudo] password for xulongheng: 
xulongheng@ubuntu:~$ sudo service jenkins start
xulongheng@ubuntu:~$ sudo service jenkins status
[0m jenkins.service - LSB: Start Jenkins at boot time
   Loaded: loaded (/etc/init.d/jenkins; bad; vendor preset: enabled)
   Active: active (exited) since Sat 2016-10-08 17:52:51 CST; 1h 41min ago
     Docs: man:systemd-sysv-generator(8)
  Process: 22519 ExecStart=/etc/init.d/jenkins start (code=exited, status=0/SUCCESS)

Oct 08 17:52:50 ubuntu systemd[1]: Starting LSB: Start Jenkins at boot time...
Oct 08 17:52:50 ubuntu jenkins[22519]:  * Starting Jenkins Continuous Integration Server jenkins
Oct 08 17:52:50 ubuntu su[22538]: Successful su for jenkins by root
Oct 08 17:52:50 ubuntu su[22538]: + ??? root:jenkins
Oct 08 17:52:50 ubuntu su[22538]: pam_unix(su:session): session opened for user jenkins by (uid=0)
Oct 08 17:52:51 ubuntu jenkins[22519]:    ...done.
Oct 08 17:52:51 ubuntu systemd[1]: Started LSB: Start Jenkins at boot time.
Oct 08 19:10:32 ubuntu systemd[1]: Started LSB: Start Jenkins at boot time.
Oct 08 19:34:29 ubuntu systemd[1]: Started LSB: Start Jenkins at boot time.
Oct 08 19:34:33 ubuntu systemd[1]: Started LSB: Start Jenkins at boot time.
xulongheng@ubuntu:~$ ps -ef | grep jenkins
jenkins  22539     1  0 17:52 ?        00:00:00 /lib/systemd/systemd --user
jenkins  22541 22539  0 17:52 ?        00:00:00 (sd-pam)
jenkins  22547     1  0 17:52 ?        00:00:00 /usr/bin/daemon --name=jenkins --inherit --env=JENKINS_HOME=/var/lib/jenkins --output=/var/log/jenkins/jenkins.log --pidfile=/var/run/jenkins/jenkins.pid -- /usr/bin/java -Djava.awt.headless=true -jar /usr/share/jenkins/jenkins.war --webroot=/var/cache/jenkins/war --httpPort=8090
jenkins  22548 22547  1 17:52 ?        00:01:05 /usr/bin/java -Djava.awt.headless=true -jar /usr/share/jenkins/jenkins.war --webroot=/var/cache/jenkins/war --httpPort=8090
xulongh+ 30755 30553  0 19:35 pts/0    00:00:00 grep --color=auto jenkins
xulongheng@ubuntu:~$ 
Jenkins service started successfully on port 8090

10.创建Jenkins,在浏览器里面输入:http://192.168.1.16:8090/出现下面界面



这里需要/var/lib/jenkins/secrets/initialAdminPassword的密码解锁Jenkins

root@systemsguru:~# cat /var/lib/jenkins/secrets/initialAdminPassword
然后把密码复制进去。

选择默认的插件



创建用户



Jenkins安装成功


11.Jenkins卸载
sudo apt-get remove –auto-remove jenkins



四.Jenkins配置

关于Jenkins配置与使用请参考文档






Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐