欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

快速搭建开发环境 - Jenkins

程序员文章站 2022-06-09 08:54:32
...

快速搭建开发环境 - Jenkins

官网

一、简介

Jenkins是一款开源 CI&CD 软件,用于自动化各种任务,包括构建、测试和部署软件。
Jenkins 支持各种运行方式,可通过系统包、Docker 或者通过一个独立的 Java 程序。

二、安装

适用于单个/本地机器上的Jenkins新安装。

Jenkins通常作为一个独立的应用程序在其自己的流程中运行, 内置Java servlet 容器/应用程序服务器(Jetty)。
Jenkins也可以运行在不同的Java servlet容器((如Apache TomcatGlassFish))中作为servlet运行。

系统要求

最低推荐配置:

  • 256MB可用内存
  • 1GB可用磁盘空间(作为一个Docker容器运行jenkins的话推荐10GB)

为小团队推荐的硬件配置:

  • 1GB+可用内存
  • 50 GB+ 可用磁盘空间

软件配置:

  • Java 8—无论是Java运行时环境(JRE)还是Java开发工具包(JDK)都可以。

Linux 平台安装

官网下载地址

选择相应操作系统 Download Jenkins 2.235.5 LTS for:

  • Docker
  • Ubuntu/Debian
  • CentOS/Fedora/Red Hat
  • Windows
  • openSUSE
  • FreeBSD
  • Gentoo
  • macOS
  • OpenBSD

操作步骤

step 1 查看操作系统版本

#查看操作系统版本
cat /etc/redhat-release 
CentOS Linux release 7.7.1908 (Core)

step 2 获取jenkins yum源的配置文件

#从官网获取jenkins yum源的配置文件
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源配置文件

ll /etc/yum.repos.d/ | grep jenkins
-rw-r--r--. 1 root root   85 Nov 29  2016 jenkins.repo # 这里可以看到 jenkins 配置文件已经下载到本地

step 3 使用yum安装jenkins

#使用yum安装jenkins (可能较慢,网络差异)
sudo yum upgrade
sudo yum install jenkins java-1.8.0-openjdk-devel
sudo systemctl daemon-reload

setp 4 启动

#启动 jenkins
sudo systemctl start jenkins
#查看 jenkins运行状态
sudo systemctl status jenkins

step 5 初始化jenkins

浏览器访问 http://yoursip:8080

默认日志文件路径 /var/log/jenkins/jenkins.log

[aaa@qq.com ~]# tail -f -n 500 /var/log/jenkins/jenkins.log
......
......
root of factory hierarchy
2020-09-01 09:39:05.498+0000 [id=27]    INFO    jenkins.install.SetupWizard#init: 

*************************************************************
*************************************************************
*************************************************************

Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:

ebde3d6742ac422aafd9dc74189f6233

This may also be found at: /var/lib/jenkins/secrets/initialAdminPassword

*************************************************************
*************************************************************
*************************************************************

注:启动服务遇到的错误

1、Starting Jenkins bash: /usr/bin/java: No such file or directory

[aaa@qq.com ~]# systemctl start jenkins
Job for jenkins.service failed because the control process exited with error code. See "systemctl status jenkins.service" and "journalctl -xe" for 
details.
[aaa@qq.com ~]# 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 Tue 2020-09-01 05:30:45 EDT; 16s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 3692 ExecStart=/etc/rc.d/init.d/jenkins start (code=exited, status=1/FAILURE)

Sep 01 05:30:45 localhost.localdomain systemd[1]: Starting LSB: Jenkins Automation Server...
Sep 01 05:30:45 localhost.localdomain runuser[3697]: pam_unix(runuser:session): session opened for user jenkins by (uid=0)
Sep 01 05:30:45 localhost.localdomain jenkins[3692]: Starting Jenkins bash: /usr/bin/java: No such file or directory
Sep 01 05:30:45 localhost.localdomain systemd[1]: jenkins.service: control process exited, code=exited status=1
Sep 01 05:30:45 localhost.localdomain jenkins[3692]: [FAILED]
Sep 01 05:30:45 localhost.localdomain systemd[1]: Failed to start LSB: Jenkins Automation Server.
Sep 01 05:30:45 localhost.localdomain systemd[1]: Unit jenkins.service entered failed state.
Sep 01 05:30:45 localhost.localdomain systemd[1]: jenkins.service failed.
[aaa@qq.com ~]# java -version
java version "1.8.0_192"
Java(TM) SE Runtime Environment (build 1.8.0_192-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.192-b12, mixed mode)
[aaa@qq.com ~]# 

原因:

jenkins默认jdk配置路径和自定义安装jdk路径不一致

解决:

修改jenkins启动脚本,添加本地jdk环境路径 /etc/init.d/jenkins

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/lib/jvm/java-11.0/bin/java
/usr/lib/jvm/jre-11.0/bin/java
/usr/lib/jvm/java-11-openjdk-amd64
/usr/bin/java
/usr/local/java/bin/java #本地jdk路径
"

Docker 安装

操作步骤

step 1 拉取镜像

#拉取镜像
[aaa@qq.com vagrant]# docker pull jenkinsci/blueocean
Using default tag: latest
Trying to pull repository docker.io/jenkinsci/blueocean ...
latest: Pulling from docker.io/jenkinsci/blueocean
......
#查看镜像
[aaa@qq.com ~]# docker images
REPOSITORY                    TAG       IMAGE ID            CREATED             SIZE
docker.io/jenkins/jenkins    latest     ca03aad4393c        18 hours ago        648 MB

step 2 启动

#启动jenkins
docker run -d -p 80:8080 -p 50000:50000 -v jenkins:/var/jenkins_home -v /etc/localtime:/etc/localtime --name jenkins docker.io/jenkins/jenkins

官方推荐启动方式

docker run \
  -u root \
  --rm \
  -d \
  -p 8080:8080 \
  -p 50000:50000 \
  -v jenkins-data:/var/jenkins_home \
  -v /var/run/docker.sock:/var/run/docker.sock \
  jenkinsci/blueocean

注:在镜像文档里,我们知道Jenkins访问的端口号是8080,另外还需要暴露一个tcp的端口号50000。我们使用如下命令启动Jenkins镜像。

jenkins正确打开方式 ( 踩坑无数 o(╥﹏╥)o)

docker run --restart=always \
-u root \
-d \
-v /home/jenkins-data:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /usr/local/maven:/usr/local/maven \
-p 8080:8080 \
-p 50000:50000 \
--net="host" \
--name jenkins \
jenkinsci/blueocean

--restart=always 在容器停止运行的时候,重启容器。用于开机自启和容器意外关闭时的自启动
-v /home/jenkins-data:/var/jenkins_home 将宿主机的 /home/jenkins-data 目录挂载到 /var/jenkins_home。这样做是为了把Jenkins文件存放到宿主机上,就算容器被删除了,新建一个新的容器内容也不会丢失。
-v /var/run/docker.sock:/var/run/docker.sock jenkins容器与Docker守护进程通信, 如果jenkins容器需要实例化其他Docker容器,则该守护进程是必需的。
-v /opt/software/apache-maven-3.6.3:/usr/local/maven 挂在Maven目录
--net="host" 容器使用宿主机的网络。jenkins默认是8080端口,用这条参数,不会修改端口,如果要改端口,请使用 -p
-p 80:8080 将容器的8080端口映射到宿主机的80端口
--name jenkins 给容器命名
-d 容器后台运行

解锁jenkins

[aaa@qq.com vagrant]# docker logs 6a618fe7c3ef

日志中查看解锁密码

2020-04-22 00:37:56.156+0000 [id=26]    INFO    jenkins.install.SetupWizard#init:
*************************************************************
*************************************************************
*************************************************************
Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:
9609e6ab67854880ba629ddd78b50b9c
This may also be found at: /var/jenkins_home/secrets/initialAdminPassword
*************************************************************
*************************************************************
*************************************************************

默认账户/密码 admin 9609e6ab67854880ba629ddd78b50b9c

进入容器内部获取密码

[aaa@qq.com ~]# docker exec jenkins tail /var/jenkins_home/secrets/initialAdminPassword
1bb8cfb0289f4df4bb0ce658941fb02e
[aaa@qq.com ~]#

三、jenkins初始化步骤

step 1

浏览器访问 http://localhost:8080/login?from=%2F
快速搭建开发环境 - Jenkins

step 2

快速搭建开发环境 - Jenkins

step 3 (花费时间较长)

快速搭建开发环境 - Jenkins
快速搭建开发环境 - Jenkins

step 4 创建用户

快速搭建开发环境 - Jenkins

step 5 实例配置

快速搭建开发环境 - Jenkins

step 6 安装成功

快速搭建开发环境 - Jenkins
快速搭建开发环境 - Jenkins


附:完整操作记录

#从官网获取jenkins yum源的配置文件
[aaa@qq.com ~]# sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
--2020-09-01 04:31:40--  https://pkg.jenkins.io/redhat-stable/jenkins.repo
Resolving pkg.jenkins.io (pkg.jenkins.io)... 151.101.194.133, 151.101.2.133, 151.101.66.133, ...
Connecting to pkg.jenkins.io (pkg.jenkins.io)|151.101.194.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 85
Saving to: ‘/etc/yum.repos.d/jenkins.repo’

100%[=========================================================================================================>] 85          --.-K/s   in 0s      

2020-09-01 04:31:42 (8.64 MB/s) - ‘/etc/yum.repos.d/jenkins.repo’ saved [85/85]

#查看下载的yum源文件
[aaa@qq.com /]# ll /etc/yum.repos.d/ | grep jenkins
-rw-r--r--. 1 root root   85 Nov 29  2016 jenkins.repo

#导入秘钥
[aaa@qq.com /]# sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key

#使用yum安装jenkins 
[aaa@qq.com /]# yum install jenkins
base                                                                                                                        | 3.6 kB  00:00:00     
epel/x86_64/metalink                                                                                                        | 2.7 kB  00:00:00     
epel                                                                                                                        | 4.7 kB  00:00:00     
extras                                                                                                                      | 2.9 kB  00:00:00     
jenkins                                                                                                                     | 2.9 kB  00:00:00     
updates                                                                                                                     | 2.9 kB  00:00:00     
(1/4): updates/7/x86_64/primary_db                                                                                          | 4.5 MB  00:00:01     
(2/4): epel/x86_64/updateinfo                                                                                               | 1.0 MB  00:00:04     
(3/4): jenkins/primary_db                                                                                                   |  34 kB  00:00:04     
(4/4): epel/x86_64/primary_db                                                                                               | 6.9 MB  00:31:25     
Resolving Dependencies
--> Running transaction check
---> Package jenkins.noarch 0:2.235.5-1.1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===================================================================================================================================================
 Package                           Arch                             Version                                Repository                         Size
===================================================================================================================================================
Installing:
 jenkins                           noarch                           2.235.5-1.1                            jenkins                            63 M

Transaction Summary
===================================================================================================================================================
Install  1 Package

Total download size: 63 M
Installed size: 63 M
Is this ok [y/d/N]: y
Is this ok [y/d/N]: y
Downloading packages:
jenkins-2.235.5-1.1.noarch.rpm                                                                                              |  63 MB  00:00:16     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : jenkins-2.235.5-1.1.noarch                                                                                                      1/1 
  Verifying  : jenkins-2.235.5-1.1.noarch                                                                                                      1/1 

Installed:
  jenkins.noarch 0:2.235.5-1.1                                                                                                                     

Complete!
[aaa@qq.com /]# 
[aaa@qq.com /]# 
[aaa@qq.com ~]# systemctl daemon-reload

#启动jenkins
[aaa@qq.com ~]# systemctl start jenkins
Job for jenkins.service failed because the control process exited with error code. See "systemctl status jenkins.service" and "journalctl -xe" for 
details.

#查看jenkins运行状态
[aaa@qq.com ~]# 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 Tue 2020-09-01 05:30:45 EDT; 16s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 3692 ExecStart=/etc/rc.d/init.d/jenkins start (code=exited, status=1/FAILURE)

Sep 01 05:30:45 localhost.localdomain systemd[1]: Starting LSB: Jenkins Automation Server...
Sep 01 05:30:45 localhost.localdomain runuser[3697]: pam_unix(runuser:session): session opened for user jenkins by (uid=0)
Sep 01 05:30:45 localhost.localdomain jenkins[3692]: Starting Jenkins bash: /usr/bin/java: No such file or directory
Sep 01 05:30:45 localhost.localdomain systemd[1]: jenkins.service: control process exited, code=exited status=1
Sep 01 05:30:45 localhost.localdomain jenkins[3692]: [FAILED]
Sep 01 05:30:45 localhost.localdomain systemd[1]: Failed to start LSB: Jenkins Automation Server.
Sep 01 05:30:45 localhost.localdomain systemd[1]: Unit jenkins.service entered failed state.
Sep 01 05:30:45 localhost.localdomain systemd[1]: jenkins.service failed.

#查看本地java环境
[aaa@qq.com ~]# java -version
java version "1.8.0_192"
Java(TM) SE Runtime Environment (build 1.8.0_192-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.192-b12, mixed mode)

#再次启动jenkins
[aaa@qq.com ~]# systemctl start jenkins

#查看jenkins运行状态
[aaa@qq.com ~]# systemctl status jenkins
● jenkins.service - LSB: Jenkins Automation Server
   Loaded: loaded (/etc/rc.d/init.d/jenkins; bad; vendor preset: disabled)
   Active: active (running) since Tue 2020-09-01 05:38:49 EDT; 25s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 3787 ExecStart=/etc/rc.d/init.d/jenkins start (code=exited, status=0/SUCCESS)
    Tasks: 38
   Memory: 343.1M
   CGroup: /system.slice/jenkins.service
           └─3806 /usr/local/java/bin/java -Dcom.sun.akuma.Daemon=daemonized -Djava.awt.headless=true -DJENKINS_HOME=/var/lib/jenkins -jar /usr/...

Sep 01 05:38:47 localhost.localdomain systemd[1]: Starting LSB: Jenkins Automation Server...
Sep 01 05:38:47 localhost.localdomain runuser[3792]: pam_unix(runuser:session): session opened for user jenkins by (uid=0)
Sep 01 05:38:49 localhost.localdomain systemd[1]: Started LSB: Jenkins Automation Server.
Sep 01 05:38:49 localhost.localdomain jenkins[3787]: Starting Jenkins [  OK  ]
[aaa@qq.com ~]# 
[aaa@qq.com ~]# /var/lib/jenkins/secrets/initialAdminPassword
-bash: /var/lib/jenkins/secrets/initialAdminPassword: Permission denied
[aaa@qq.com ~]#

[aaa@qq.com ~]# tail -f -n 500 /var/log/jenkins/jenkins.log
Running from: /usr/lib/jenkins/jenkins.war
2020-09-01 09:38:50.155+0000 [id=1]     WARNING winstone.Logger#logInternal: Parameter handlerCountMax is now deprecated
2020-09-01 09:38:50.205+0000 [id=1]     WARNING winstone.Logger#logInternal: Parameter handlerCountMaxIdle is now deprecated
2020-09-01 09:38:50.231+0000 [id=1]     INFO    org.eclipse.jetty.util.log.Log#initialized: Logging initialized @1303ms to org.eclipse.jetty.util.l
og.JavaUtilLog
2020-09-01 09:38:50.321+0000 [id=1]     INFO    winstone.Logger#logInternal: Beginning extraction from war file
2020-09-01 09:38:51.982+0000 [id=1]     WARNING o.e.j.s.handler.ContextHandler#setContextPath: Empty contextPath
2020-09-01 09:38:52.094+0000 [id=1]     INFO    org.eclipse.jetty.server.Server#doStart: jetty-9.4.30.v20200611; built: 2020-06-11T12:34:51.929Z; g
it: 271836e4c1f4612f12b7bb13ef5a92a927634b0d; jvm 1.8.0_192-b12
2020-09-01 09:38:52.834+0000 [id=1]     INFO    o.e.j.w.StandardDescriptorProcessor#visitServlet: NO JSP Support for /, did not find org.eclipse.je
tty.jsp.JettyJspServlet
2020-09-01 09:38:52.950+0000 [id=1]     INFO    o.e.j.s.s.DefaultSessionIdManager#doStart: DefaultSessionIdManager workerName=node0
2020-09-01 09:38:52.951+0000 [id=1]     INFO    o.e.j.s.s.DefaultSessionIdManager#doStart: No SessionScavenger set, using defaults
2020-09-01 09:38:52.952+0000 [id=1]     INFO    o.e.j.server.session.HouseKeeper#startScavenging: node0 Scavenging every 600000ms
2020-09-01 09:38:53.769+0000 [id=1]     INFO    hudson.WebAppMain#contextInitialized: Jenkins home directory: /var/lib/jenkins found at: SystemProp
erties.getProperty("JENKINS_HOME")
2020-09-01 09:38:54.416+0000 [id=1]     INFO    o.e.j.s.handler.ContextHandler#doStart: Started aaa@qq.com{Jenkins v2.235.5,/,file:///var/cache/jenk
ins/war/,AVAILABLE}{/var/cache/jenkins/war}
2020-09-01 09:38:54.502+0000 [id=1]     INFO    o.e.j.server.AbstractConnector#doStart: Started aaa@qq.com{HTTP/1.1, (http/1.1)}{0.0.
0.0:8080}
2020-09-01 09:38:54.503+0000 [id=1]     INFO    org.eclipse.jetty.server.Server#doStart: Started @5575ms
2020-09-01 09:38:54.520+0000 [id=21]    INFO    winstone.Logger#logInternal: Winstone Servlet Engine running: controlPort=disabled
2020-09-01 09:38:57.441+0000 [id=27]    INFO    jenkins.InitReactorRunner$1#onAttained: Started initialization
2020-09-01 09:38:57.535+0000 [id=26]    INFO    jenkins.InitReactorRunner$1#onAttained: Listed all plugins
2020-09-01 09:39:00.297+0000 [id=27]    INFO    jenkins.InitReactorRunner$1#onAttained: Prepared all plugins
2020-09-01 09:39:00.314+0000 [id=27]    INFO    jenkins.InitReactorRunner$1#onAttained: Started all plugins
2020-09-01 09:39:00.333+0000 [id=26]    INFO    jenkins.InitReactorRunner$1#onAttained: Augmented all extensions
2020-09-01 09:39:02.524+0000 [id=26]    INFO    jenkins.InitReactorRunner$1#onAttained: System config loaded
2020-09-01 09:39:02.525+0000 [id=26]    INFO    jenkins.InitReactorRunner$1#onAttained: System config adapted
2020-09-01 09:39:02.525+0000 [id=26]    INFO    jenkins.InitReactorRunner$1#onAttained: Loaded all jobs
2020-09-01 09:39:02.543+0000 [id=26]    INFO    jenkins.InitReactorRunner$1#onAttained: Configuration for all jobs updated
2020-09-01 09:39:03.556+0000 [id=40]    INFO    hudson.model.AsyncPeriodicWork#lambda$doRun$0: Started Download metadata
2020-09-01 09:39:03.564+0000 [id=40]    INFO    hudson.util.Retrier#start: Attempt #1 to do the action check updates server
2020-09-01 09:39:04.635+0000 [id=27]    INFO    o.s.c.s.AbstractApplicationContext#prepareRefresh: Refreshing org.springframework.web.context.suppo
aaa@qq.com: display name [Root WebApplicationContext]; startup date [Tue Sep 01 05:39:04 EDT 2020]; root of context hi
erarchy
2020-09-01 09:39:04.635+0000 [id=27]    INFO    o.s.c.s.AbstractApplicationContext#obtainFreshBeanFactory: Bean factory for application context [or
g.aaa@qq.com23de1542]: org.saaa@qq.com6
b5862fe
2020-09-01 09:39:04.667+0000 [id=27]    INFO    o.s.b.f.s.DefaultListableBeanFactory#preInstantiateSingletons: Pre-instantiating singletons in org.
saaa@qq.com6b5862fe: defining beans [authenticationManager]; root of factory hierarchy
2020-09-01 09:39:05.042+0000 [id=27]    INFO    o.s.c.s.AbstractApplicationContext#prepareRefresh: Refreshing org.springframework.web.context.suppo
aaa@qq.com: display name [Root WebApplicationContext]; startup date [Tue Sep 01 05:39:05 EDT 2020]; root of context hi
erarchy
2020-09-01 09:39:05.042+0000 [id=27]    INFO    o.s.c.s.AbstractApplicationContext#obtainFreshBeanFactory: Bean factory for application context [or
g.aaa@qq.com2c402855]: org.saaa@qq.com2
6dbfc68
2020-09-01 09:39:05.043+0000 [id=27]    INFO    o.s.b.f.s.DefaultListableBeanFactory#preInstantiateSingletons: Pre-instantiating singletons in org.
saaa@qq.com26dbfc68: defining beans [filter,legacy]; root of factory hierarchy
2020-09-01 09:39:05.498+0000 [id=27]    INFO    jenkins.install.SetupWizard#init: 

*************************************************************
*************************************************************
*************************************************************

Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:

ebde3d6742ac422aafd9dc74189f6233

This may also be found at: /var/lib/jenkins/secrets/initialAdminPassword

*************************************************************
*************************************************************
*************************************************************

2020-09-01 09:39:28.478+0000 [id=27]    INFO    jenkins.InitReactorRunner$1#onAttained: Completed initialization
2020-09-01 09:39:28.509+0000 [id=20]    INFO    hudson.WebAppMain$3#run: Jenkins is fully up and running
2020-09-01 09:39:28.879+0000 [id=40]    INFO    h.m.DownloadService$Downloadable#load: Obtained the updated data file for hudson.tasks.Maven.MavenI
nstaller
2020-09-01 09:39:28.880+0000 [id=40]    INFO    hudson.util.Retrier#start: Performed the action check updates server successfully at the attempt #1
2020-09-01 09:39:28.881+0000 [id=40]    INFO    hudson.model.AsyncPeriodicWork#lambda$doRun$0: Finished Download metadata. 25,324 ms
2020-09-01 09:40:57.174+0000 [id=62]    INFO    hudson.model.AsyncPeriodicWork#lambda$doRun$0: Started Periodic background build discarder
2020-09-01 09:40:57.175+0000 [id=62]    INFO    hudson.model.AsyncPeriodicWork#lambda$doRun$0: Finished Periodic background build discarder. 0 ms

参考文章

参考博客 https://www.jianshu.com/p/0391e225e4a6

参考博客 https://www.cnblogs.com/jakaBlog/p/11284451.html

参考博客 https://blog.csdn.net/qq_38252039/article/details/89791247

参考博客 https://www.reinforce.cn/t/658.html

官方文档 https://jenkins.io/zh/doc/book/installing/