Jenkins 安装与部署详细教程
一、概述
Jenkins 的前身是 Hudson 是一个可扩展的持续集成引擎。Jenkins 是一款开源 CI&CD 软件,用于自动化各种任务,包括构建、测试和部署软件。Jenkins 支持各种运行方式,可通过系统包、Docker 或者通过一个独立的 Java 程序。
二、Jenkins安装前环境准备
1、操作系统CentOS7.X,并进行Java环境的安装,建议安装1.8版本以及更高版本。如果系统没有自带git,同时安装Git。
1)安装Java1.8版本,下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 可以先下载好后,上传至Linux服务器。
[root@localhost data]# ls jdk-8u181-linux-x64.tar.gz [root@localhost data]# tar -zxvf jdk-8u181-linux-x64.tar.gz [root@localhost data]# ls jdk1.8.0_181 jdk-8u181-linux-x64.tar.gz [root@localhost data]# vim /etc/profile .................................... export JAVA_HOME=/data/jdk1.8.0_181/ export CLASSPATH=.:${JAVA_HOME}/jre/lib/rt.jar:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar export PATH=$PATH:${JAVA_HOME}/bin .................................... [root@localhost data]# source /etc/profile //使环境变量生效。 [root@localhost data]# 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)
2)安装Git插件
[root@localhost data]# yum install git 已加载插件:fastestmirror, langpacks Determining fastest mirrors * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com base | 3.6 kB 00:00 extras | 2.9 kB 00:00 updates | 2.9 kB 00:00 (1/2): extras/7/x86_64/primary_db | 165 kB 00:00 (2/2): updates/7/x86_64/primary_db | 7.6 MB 00:01 正在解决依赖关系 --> 正在检查事务 ---> 软件包 git.x86_64.0.1.8.3.1-20.el7 将被 升级 --> 正在处理依赖关系 git = 1.8.3.1-20.el7,它被软件包 perl-Git-1.8.3.1-20.el7.noarch 需要 ---> 软件包 git.x86_64.0.1.8.3.1-21.el7_7 将被 更新 --> 正在检查事务 ---> 软件包 perl-Git.noarch.0.1.8.3.1-20.el7 将被 升级 ---> 软件包 perl-Git.noarch.0.1.8.3.1-21.el7_7 将被 更新 --> 解决依赖关系完成 依赖关系解决 ================================================================================
三、Jenkins安装
1、第一种安装方法
[root@localhost data]# wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo --2020-04-21 09:01:53-- https://pkg.jenkins.io/redhat-stable/jenkins.repo 正在解析主机 pkg.jenkins.io (pkg.jenkins.io)... 52.202.51.185 正在连接 pkg.jenkins.io (pkg.jenkins.io)|52.202.51.185|:443... 已连接。 已发出 HTTP 请求,正在等待回应... 200 OK 长度:85 正在保存至: “/etc/yum.repos.d/jenkins.repo” 100%[=========================================================================>] 85 --.-K/s 用时 0s 2020-04-21 09:01:54 (19.3 MB/s) - 已保存 “/etc/yum.repos.d/jenkins.repo” [85/85]) [root@localhost data]# rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key [root@localhost data]# yum install jenkins 已加载插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com jenkins | 2.9 kB 00:00:00 jenkins/primary_db | 32 kB 00:00:01 正在解决依赖关系 --> 正在检查事务 ---> 软件包 jenkins.noarch.0.2.222.1-1.1 将被 安装 --> 解决依赖关系完成 依赖关系解决 =================================================================================================================== Package 架构 版本 源 大小 =================================================================================================================== 正在安装: jenkins noarch 2.222.1-1.1 jenkins 63 M 事务概要 =================================================================================================================== 安装 1 软件包 总下载量:63 M 安装大小:63 M Is this ok [y/d/N]: y Downloading packages: ^Cnkins-2.222.1-1.1.noarch.rpm 7% [== ] 21 kB/s | 4.7 MB 00:48:13 ETA
2、第二种安装方法。在Jenkins网站将稳定版本的RPM包下载后,上传至Linux服务器。
[root@localhost data]# rpm -ivh jenkins-2.222.1-1.1.noarch.rpm 准备中... ################################# [100%] 正在升级/安装... 1:jenkins-2.222.1-1.1 ################################# [100%]
1)注意:上述两种方法安装Jenkins后,需要配置环境变量,否则在启动时会出现报错。
[root@localhost data]# vim /etc/sysconfig/jenkins ................... JENKINS_PORT="8080" //修改监听端口8080 JENKINS_USER="root" //为了不因为权限出现各种问题,这里面直接修改Jenkins为root权限 并修改文件权限。 .................... [root@localhost data]# chown -R root:root /var/lib/jenkins/ [root@localhost data]# chown -R root:root /var/cache/jenkins/ [root@localhost data]# chown -R root:root /var/log/jenkins/
2)现在启动Jenkins会出现报错信息:
[root@localhost data]# service jenkins restart Restarting jenkins (via systemctl): Job for jenkins.service failed because the control process exited with error code. See "systemctl status jenkins.service" and "journalctl -xe" for details. [FAILED] pathmunge /usr/sbin after [root@localhost data]# 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 二 2020-04-21 09:13:09 CST; 10s ago Docs: man:systemd-sysv-generator(8) Process: 3096 ExecStart=/etc/rc.d/init.d/jenkins start (code=exited, status=1/FAILURE) 4月 21 09:13:09 localhost.localdomain systemd[1]: Starting LSB: Jenkins Automation Server... 4月 21 09:13:09 localhost.localdomain runuser[3101]: pam_unix(runuser:session): session opened for user roo...d=0) 4月 21 09:13:09 localhost.localdomain jenkins[3096]: Starting Jenkins bash: /usr/bin/java: No such file or ...tory 4月 21 09:13:09 localhost.localdomain runuser[3101]: pam_unix(runuser:session): session closed for user root 4月 21 09:13:09 localhost.localdomain jenkins[3096]: [FAILED] 4月 21 09:13:09 localhost.localdomain systemd[1]: jenkins.service: control process exited, code=exited status=1 4月 21 09:13:09 localhost.localdomain systemd[1]: Failed to start LSB: Jenkins Automation Server. 4月 21 09:13:09 localhost.localdomain systemd[1]: Unit jenkins.service entered failed state. 4月 21 09:13:09 localhost.localdomain systemd[1]: jenkins.service failed. test -e "$JENKINS_CONFIG" || { echo "$JENKINS_CONFIG not existing"; Hint: Some lines were ellipsized, use -l to show in full.
3)问题主要原因是Starting Jenkins bash: /usr/bin/java: No such file or ...tory,Java环境变量。
[root@localhost data]# which java /data/jdk1.8.0_181/bin/java [root@localhost data]# vim /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 /data/jdk2.8.0_181/bin/java //将Java路径添加此处 "..................................... [root@localhost data]# source /etc/profile [root@localhost data]# source /etc/profile [root@localhost data]# systemctl restart jenkins.service Warning: jenkins.service changed on disk. Run 'systemctl daemon-reload' to reload units. [root@localhost data]# systemctl status jenkins.service ● jenkins.service - LSB: Jenkins Automation Server Loaded: loaded (/etc/rc.d/init.d/jenkins; bad; vendor preset: disabled) Active: active (running) since 二 2020-04-21 09:19:33 CST; 4s ago Docs: man:systemd-sysv-generator(8) Process: 3479 ExecStart=/etc/rc.d/init.d/jenkins start (code=exited, status=0/SUCCESS) CGroup: /system.slice/jenkins.service └─3503 /data/jdk1.8.0_181/bin/java -Dcom.sun.akuma.Daemon=daemonized -Djava.awt.headless=true -DJENKI... 4月 21 09:19:32 localhost.localdomain systemd[1]: Starting LSB: Jenkins Automation Server... 4月 21 09:19:33 localhost.localdomain runuser[3484]: pam_unix(runuser:session): session opened for user roo...d=0) 4月 21 09:19:33 localhost.localdomain runuser[3484]: pam_unix(runuser:session): session closed for user root 4月 21 09:19:33 localhost.localdomain jenkins[3479]: Starting Jenkins [ OK ] 4月 21 09:19:33 localhost.localdomain systemd[1]: Started LSB: Jenkins Automation Server. Warning: jenkins.service changed on disk. Run 'systemctl daemon-reload' to reload units. Hint: Some lines were ellipsized, use -l to show in full.
4)安装完成后,设置开机自动启动,需要知悉Jenkins相关默认路径
[root@localhost ~]# ll /usr/lib/jenkins/ //Jenkins war包 总用量 64736 -rw-r--r-- 1 root root 66288019 3月 25 22:13 jenkins.war [root@localhost ~]# vim /etc/sysconfig/jenkins //配置文件:端口、权限 Jenkins HOME路径等 [root@localhost ~]# ls /var/lib/jenkins/ //Jenkins HOME路径 com.cloudbees.hudson.plugins.folder.config.AbstractFolderConfiguration.xml logs config.xml nodeMonitors.xml hudson.model.UpdateCenter.xml nodes hudson.plugins.git.GitTool.xml plugins identity.key.enc queue.xml.bak jenkins.install.InstallUtil.lastExecVersion secret.key jenkins.install.UpgradeWizard.state secret.key.not-so-secret jenkins.model.JenkinsLocationConfiguration.xml secrets jenkins.security.apitoken.ApiTokenPropertyConfiguration.xml updates jenkins.security.QueueItemAuthenticatorConfiguration.xml userContent jenkins.security.UpdateSiteWarningsConfiguration.xml users jenkins.telemetry.Correlator.xml workflow-libs jobs [root@localhost ~]# ll /var/log/jenkins/jenkins.log //Jenkins日志文件 -rw-r--r-- 1 root root 56355 4月 21 13:53 /var/log/jenkins/jenkins.log
3、第三种安装方法
第三种安装方法是安装Tomcat,下载Jenkins war包,放入Tomcat内进行启动加载。本次不进行安装测试了。
四、Jenkins基本配置
1、本地使用http://IP:8080 访问Jenkins,会进入初始部署,选择推荐的插件安装。
执行下述命令查看初始密码。
[root@localhost lib]# cat /var/lib/jenkins/secrets/initialAdminPassword cedc48233e8e4a958b77f11c59c42d65
2、安装推荐插件
登录Jenkins进行更深次的学习吧。
-----------------------------------------------------------书山有路勤为径,学海无涯苦作舟-------------------------------------------------------------