在centos7上安装jenkins

导语:因为工作需要,我们希望自动部署项目,jenkins是一个自动部署的开源软件,下面是在centos7上安装jenkins的全过程。由于jenkins需要openjdk,但服务器上前期安装了oracle的jdk,因此文章开头是先卸载oracle的jdk,然后在安装openjdk。

jdk准备:卸载oracle的jdk

查看JAVA jdk版本

java -version

java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

查看安装的oracle jdk,准备卸载

jenkins需要安装open jdk。

rpm -qa|grep jdk

jdk1.8-1.8.0_181-fcs.x86_64

卸载oracle jdk

rpm -e --nodeps jdk1.8-1.8.0_181-fcs.x86_64

移除Oracle jdk的配置路径文件

vi /etc/profile 

#java environment
#export JAVA_HOME=/usr/java/jdk1.8.0_181-amd64
#export CLASSPATH=.:${JAVA_HOME}/jre/lib/rt.jar:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar
#export PATH=$PATH:${JAVA_HOME}/bin

执行命令让配置生效

source /etc/profile

查看PATH是否生效

echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

如果没有生效,那重启服务器好了。

查看服务器版本

cat /etc/redhat-release

安装openjdk

安装之前先查看一下有无系统自带jdk

rpm -qa |grep java

rpm -qa |grep jdk

rpm -qa |grep gcj

如果有就使用批量卸载命令

rpm -qa | grep java | xargs rpm -e --nodeps 

安装java

安装最新版

yum -y install java

或者安装1.8版本

yum -y install java-1.8.0-openjdk* 

Installed:
  java-1.8.0-openjdk.x86_64 1:1.8.0.222.b10-0.el7_6                                                  java-1.8.0-openjdk-accessibility.x86_64 1:1.8.0.222.b10-0.el7_6
  java-1.8.0-openjdk-accessibility-debug.x86_64 1:1.8.0.222.b10-0.el7_6                              java-1.8.0-openjdk-debug.x86_64 1:1.8.0.222.b10-0.el7_6
  java-1.8.0-openjdk-demo.x86_64 1:1.8.0.222.b10-0.el7_6                                             java-1.8.0-openjdk-demo-debug.x86_64 1:1.8.0.222.b10-0.el7_6
  java-1.8.0-openjdk-devel.x86_64 1:1.8.0.222.b10-0.el7_6                                            java-1.8.0-openjdk-devel-debug.x86_64 1:1.8.0.222.b10-0.el7_6
  java-1.8.0-openjdk-headless.x86_64 1:1.8.0.222.b10-0.el7_6                                         java-1.8.0-openjdk-headless-debug.x86_64 1:1.8.0.222.b10-0.el7_6
  java-1.8.0-openjdk-javadoc.noarch 1:1.8.0.222.b10-0.el7_6                                          java-1.8.0-openjdk-javadoc-debug.noarch 1:1.8.0.222.b10-0.el7_6
  java-1.8.0-openjdk-javadoc-zip.noarch 1:1.8.0.222.b10-0.el7_6                                      java-1.8.0-openjdk-javadoc-zip-debug.noarch 1:1.8.0.222.b10-0.el7_6
  java-1.8.0-openjdk-src.x86_64 1:1.8.0.222.b10-0.el7_6                                              java-1.8.0-openjdk-src-debug.x86_64 1:1.8.0.222.b10-0.el7_6

Dependency Installed:
  GConf2.x86_64 0:3.2.6-8.el7                        atk.x86_64 0:2.28.1-1.el7                   cairo.x86_64 0:1.15.12-3.el7                   copy-jdk-configs.noarch 0:3.3-10.el7_5
  dejavu-fonts-common.noarch 0:2.33-6.el7            dejavu-sans-fonts.noarch 0:2.33-6.el7       fontconfig.x86_64 0:2.13.0-4.3.el7             fontpackages-filesystem.noarch 0:1.44-8.el7
  fribidi.x86_64 0:1.0.2-1.el7                       gdk-pixbuf2.x86_64 0:2.36.12-3.el7          giflib.x86_64 0:4.1.6-9.el7                    graphite2.x86_64 0:1.3.10-1.el7_3
  gtk-update-icon-cache.x86_64 0:3.22.30-3.el7       gtk2.x86_64 0:2.24.31-1.el7                 harfbuzz.x86_64 0:1.7.5-2.el7                  hicolor-icon-theme.noarch 0:0.12-7.el7
  jasper-libs.x86_64 0:1.900.1-33.el7                java-atk-wrapper.x86_64 0:0.30.4-5.el7      javapackages-tools.noarch 0:3.4.1-11.el7       jbigkit-libs.x86_64 0:2.0-11.el7
  libICE.x86_64 0:1.0.9-9.el7                        libSM.x86_64 0:1.2.2-2.el7                  libX11.x86_64 0:1.6.5-2.el7                    libX11-common.noarch 0:1.6.5-2.el7
  libXau.x86_64 0:1.0.8-2.1.el7                      libXcomposite.x86_64 0:0.4.4-4.1.el7        libXcursor.x86_64 0:1.1.15-1.el7               libXdamage.x86_64 0:1.1.4-4.1.el7
  libXext.x86_64 0:1.3.3-3.el7                       libXfixes.x86_64 0:5.0.3-1.el7              libXft.x86_64 0:2.3.2-2.el7                    libXi.x86_64 0:1.7.9-1.el7
  libXinerama.x86_64 0:1.1.3-2.1.el7                 libXrandr.x86_64 0:1.5.1-2.el7              libXrender.x86_64 0:0.9.10-1.el7               libXtst.x86_64 0:1.2.3-1.el7
  libXv.x86_64 0:1.0.11-1.el7                        libXxf86dga.x86_64 0:1.1.4-2.1.el7          libXxf86misc.x86_64 0:1.0.3-7.1.el7            libXxf86vm.x86_64 0:1.1.4-1.el7
  libdmx.x86_64 0:1.1.3-3.el7                        libfontenc.x86_64 0:1.1.3-3.el7             libglvnd.x86_64 1:1.0.1-0.8.git5baa1e5.el7     libglvnd-egl.x86_64 1:1.0.1-0.8.git5baa1e5.el7
  libglvnd-glx.x86_64 1:1.0.1-0.8.git5baa1e5.el7     libjpeg-turbo.x86_64 0:1.2.90-6.el7         libpng.x86_64 2:1.5.13-7.el7_2                 libthai.x86_64 0:0.1.14-9.el7
  libtiff.x86_64 0:4.0.3-27.el7_3                    libwayland-client.x86_64 0:1.15.0-1.el7     libwayland-server.x86_64 0:1.15.0-1.el7        libxcb.x86_64 0:1.13-1.el7
  libxshmfence.x86_64 0:1.2-1.el7                    libxslt.x86_64 0:1.1.28-5.el7               lksctp-tools.x86_64 0:1.0.17-2.el7             mesa-libEGL.x86_64 0:18.0.5-4.el7_6
  mesa-libGL.x86_64 0:18.0.5-4.el7_6                 mesa-libgbm.x86_64 0:18.0.5-4.el7_6         mesa-libglapi.x86_64 0:18.0.5-4.el7_6          nss-pem.x86_64 0:1.0.3-5.el7_6.1
  pango.x86_64 0:1.42.4-2.el7_6                      pcsc-lite-libs.x86_64 0:1.8.8-8.el7         pixman.x86_64 0:0.34.0-1.el7                   python-javapackages.noarch 0:3.4.1-11.el7
  python-lxml.x86_64 0:3.2.1-4.el7                   ttmkfdir.x86_64 0:3.0.9-42.el7              tzdata-java.noarch 0:2019b-1.el7               xorg-x11-font-utils.x86_64 1:7.5-21.el7
  xorg-x11-fonts-Type1.noarch 0:7.5-9.el7            xorg-x11-utils.x86_64 0:7.5-23.el7

Dependency Updated:
  freetype.x86_64 0:2.8-12.el7_6.1     glib2.x86_64 0:2.56.1-4.el7_6               libdrm.x86_64 0:2.4.91-3.el7           nspr.x86_64 0:4.19.0-1.el7_5         nss.x86_64 0:3.36.0-7.1.el7_6
  nss-softokn.x86_64 0:3.36.0-5.el7_5  nss-softokn-freebl.x86_64 0:3.36.0-5.el7_5  nss-sysinit.x86_64 0:3.36.0-7.1.el7_6  nss-tools.x86_64 0:3.36.0-7.1.el7_6  nss-util.x86_64 0:3.36.0-1.1.el7_6

Complete!

查看版本

java -version
 
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)

查看jdk安装情况

cd /usr/lib/jvm

ll
 
total 8
lrwxrwxrwx 1 root root   26 Sep  8 16:09 java -> /etc/alternatives/java_sdk
lrwxrwxrwx 1 root root   32 Sep  8 16:09 java-1.8.0 -> /etc/alternatives/java_sdk_1.8.0
lrwxrwxrwx 1 root root   40 Sep  8 16:09 java-1.8.0-openjdk -> /etc/alternatives/java_sdk_1.8.0_openjdk
drwxr-xr-x 9 root root 4096 Sep  8 16:09 java-1.8.0-openjdk-1.8.0.222.b10-0.el7_6.x86_64
drwxr-xr-x 9 root root 4096 Sep  8 16:09 java-1.8.0-openjdk-1.8.0.222.b10-0.el7_6.x86_64-debug
lrwxrwxrwx 1 root root   34 Sep  8 16:09 java-openjdk -> /etc/alternatives/java_sdk_openjdk
lrwxrwxrwx 1 root root   21 Sep  8 16:08 jre -> /etc/alternatives/jre
lrwxrwxrwx 1 root root   27 Sep  8 16:08 jre-1.8.0 -> /etc/alternatives/jre_1.8.0
lrwxrwxrwx 1 root root   35 Sep  8 16:08 jre-1.8.0-openjdk -> /etc/alternatives/jre_1.8.0_openjdk
lrwxrwxrwx 1 root root   51 Sep  8 16:08 jre-1.8.0-openjdk-1.8.0.222.b10-0.el7_6.x86_64 -> java-1.8.0-openjdk-1.8.0.222.b10-0.el7_6.x86_64/jre
lrwxrwxrwx 1 root root   57 Sep  8 16:08 jre-1.8.0-openjdk-1.8.0.222.b10-0.el7_6.x86_64-debug -> java-1.8.0-openjdk-1.8.0.222.b10-0.el7_6.x86_64-debug/jre
lrwxrwxrwx 1 root root   29 Sep  8 16:08 jre-openjdk -> /etc/alternatives/jre_openjdk

配置jdk运行环境

vi /etc/profile

#set java environment  

export JAVA_HOME=/usr/lib/jvm/java

export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/jre/lib/rt.jar

export PATH=$PATH:$JAVA_HOME/bin

使得配置生效

这里和上面卸载oracle后配置生效使用的不同的命令,也可以使用上面的命令。

. /etc/profile

查看配置路径

echo $JAVA_HOME

/usr/lib/jvm/java
echo $CLASSPATH

.:/usr/lib/jvm/java/jre/lib/rt.jar:/usr/lib/jvm/java/lib/dt.jar:/usr/lib/jvm/java/lib/tools.jar

开始安装jenkins

创建一个目录

cd ~

mkdir jenkins

ll

drwxr-xr-x 2 root root      4096 Sep  8 16:34 jenkins

cd jenkins

安装jenkins前准备

按照官网说的安装前先执行下面的命令,文章地址:https://pkg.jenkins.io/redhat-stable/

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

下载一个安装配置文件

wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
--2019-09-08 16:38:17--  https://pkg.jenkins.io/redhat-stable/jenkins.repo
Resolving pkg.jenkins.io (pkg.jenkins.io)... 52.202.51.185
Connecting to pkg.jenkins.io (pkg.jenkins.io)|52.202.51.185|: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

2019-09-08 16:38:19 (16.4 MB/s) - ‘/etc/yum.repos.d/jenkins.repo’ saved [85/85]

查看一下这个配置文件

cat /etc/yum.repos.d/jenkins.repo

[jenkins]
name=Jenkins-stable
baseurl=http://pkg.jenkins.io/redhat-stable
gpgcheck=1

导入jenkins密钥

rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key

开始安装jenkins

直接使用yum安装

yum install jenkins

Loaded plugins: fastestmirror
jenkins                                                                                                                                                                       | 2.9 kB  00:00:00
jenkins/primary_db                                                                                                                                                            |  29 kB  00:00:01
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package jenkins.noarch 0:2.176.3-1.1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================================================================================================================================================
 Package                                       Arch                                         Version                                              Repository                                     Size
=====================================================================================================================================================================================================
Installing:
 jenkins                                       noarch                                       2.176.3-1.1                                          jenkins                                        74 M

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

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

Installed:
  jenkins.noarch 0:2.176.3-1.1

Complete!

添加jenkins用户

安装时会自动生成jenkins用户,如果没有,新加jenkins用户作为jenkins的系统用户。

useradd jenkins

useradd: user 'jenkins' already exists

修改jenkins的系统配置文件

vi /etc/sysconfig/jenkins

修改用户

## 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的端口

JENKINS_PORT 默认为8080,服务器8080端口已经被其他应用占用的话可以改为其他端口,例如8181;

## Type:        integer(0:65535)
## Default:     8080
## ServiceRestart: jenkins
#
# Port Jenkins is listening on.
# Set to -1 to disable
#
JENKINS_PORT="8080"

启动jenkins服务

systemctl start jenkins

jenkins   2453  102  6.7 2274776 68256 ?       Ssl  16:56   0:04 /etc/alternatives/java -Dcom.sun.akuma.Daemon=daemonized -Djava.awt.headless=true -DJENKINS_HOME=/var/lib/jenkins -jar /usr/lib/jenk

停止jenkins服务

systemctl stop jenkins

启用jenkins服务

seytemctl enable jenkins

打开网址完成配置

http://IP:8080

如果页面提示:

Please wait while Jenkins is getting ready to work

等待几分钟后,进入了配置页面。如果一直是这个提示的话可能是你的网络连接不上jenkins的服务器无法下载更新软件,那么需要修改配置。
需要你进入jenkins的工作目录:

cd /var/lib/jenkins/

ll

total 28
-rw-r--r--. 1 jenkins jenkins 1640 Sep 10 02:11 config.xml
-rw-r--r--. 1 jenkins jenkins  156 Sep 10 02:08 hudson.model.UpdateCenter.xml
-rw-------. 1 jenkins jenkins 1712 Sep 10 02:08 identity.key.enc
-rw-r--r--. 1 jenkins jenkins    7 Sep 10 02:08 jenkins.install.UpgradeWizard.state
-rw-r--r--. 1 jenkins jenkins  171 Sep 10 02:08 jenkins.telemetry.Correlator.xml
drwxr-xr-x. 2 jenkins jenkins    6 Sep 10 02:08 jobs
drwxr-xr-x. 3 jenkins jenkins   19 Sep 10 02:08 logs
-rw-r--r--. 1 jenkins jenkins  907 Sep 10 02:08 nodeMonitors.xml
drwxr-xr-x. 2 jenkins jenkins    6 Sep 10 02:08 nodes
drwxr-xr-x. 2 jenkins jenkins    6 Sep 10 02:08 plugins
-rw-r--r--. 1 jenkins jenkins   64 Sep 10 02:08 secret.key
-rw-r--r--. 1 jenkins jenkins    0 Sep 10 02:08 secret.key.not-so-secret
drwx------. 4 jenkins jenkins  265 Sep 10 02:08 secrets
drwxr-xr-x. 2 jenkins jenkins   67 Sep 10 02:12 updates
drwxr-xr-x. 2 jenkins jenkins   24 Sep 10 02:08 userContent
drwxr-xr-x. 3 jenkins jenkins   56 Sep 10 02:08 users

打开 hudson.model.UpdateCenter.xml 把

http://updates.jenkins-ci.org/update-center.json

改成

http://mirror.xmission.com/jenkins/updates/update-center.json

解锁 Jenkins

为了确保管理员安全地安装 Jenkins,密码已写入到日志中(不知道在哪里?)该文件在服务器上:
/var/lib/jenkins/secrets/initialAdminPassword

请从本地复制密码并粘贴到下面。

提示需要管理员密码,在服务器上执行下面的命令:

cat /var/lib/jenkins/secrets/initialAdminPassword

31b0921be8ad4b1ddet740d407a83d9f

将密码粘贴到密码框,点击“继续”。

进入插件安装,先安装社区推荐的插件:

Folders 
OWASP Markup Formatter 
Build Timeout 
Credentials Binding 
Timestamper 
Workspace Cleanup 
Ant 
Gradle 
Pipeline 
GitHub Branch Source 
Pipeline: GitHub Groovy Libraries 
Pipeline: Stage View 
Git 
Subversion 
SSH Slaves 
Matrix Authorization Strategy 
PAM Authentication 
LDAP 
Email Extension 
Mailer 
Localization: Chinese (Simplified)

完成后提示创建第一个用户,然后是一个网址配置。

Jenkins URL:	
http://ip:8080/
Jenkins URL 用于给各种Jenkins资源提供绝对路径链接的根地址。 这意味着对于很多Jenkins特色是需要正确设置的,例如:邮件通知、PR状态更新以及提供给构建步骤的BUILD_URL环境变量。
推荐的默认值显示在尚未保存,如果可能的话这是根据当前请求生成的。 最佳实践是要设置这个值,用户可能会需要用到。这将会避免在分享或者查看链接时的困惑。

到这里,基本上就完成大部分的安装工作了。

结束

jenkins的安装需要注意的问题是jdk,如果服务器上运行了其他的项目需要使用OracleJDK的,那么在这个服务器上安装jenkins是有问题的,jenkins要求OpenJDK。安装社区推荐插件的时候,等待的时间需要很久。

发表评论

邮箱地址不会被公开。 必填项已用*标注