jenkins环境搭建

jenkins环境搭建

1.环境说明

key value
环境 centos7
jdk版本 11
git 2.43.0
maven 3.9.6
jenkins最新版本 http://mirrors.jenkins-ci.org/war/latest/

2.环境准备

1.jdk安装

安装方式请参考博主之前的文章:https://blog.csdn.net/weixin_44702984/article/details/128906562

centos7安装jdk

2.安装Git

yum安装git方式

  1. 执行命令:yum install -y git ;
  2. 验证是否安装成功:git --version

1.通过官网下载git:https://mirrors.edge.kernel.org/pub/software/scm/git/

2.下载的git上传到/opt目录下,进行解压

shell 复制代码
tar -zvxf git-2.43.0.tar.gz

3.安装源代码编译环境

shell 复制代码
yum install -y curl-devel expat-devel openssl-devel zlib-devel gcc-c++ 
yum install -y perl-ExtUtils-MakeMaker automake autoconf libtool make

3.配置git安装目录

shell 复制代码
cd git-2.43.0
./configure --prefix=/opt/git

4.执行git安装命令

shell 复制代码
make install

5.添加环境变量

shell 复制代码
vi /etc/profile
export GIT_HOME=/opt/git
export PATH=${GIT_HOME}/bin:$PATH 

6.环境变量生效

shell 复制代码
source /etc/profile

3.安装sshpass

shell 复制代码
 yum install -y sshpass

4.安装Maven

.1.maven下载页面:https://maven.apache.org/download.cgi

2.将下载的压缩包apache-maven-3.9.6-bin.tar.gz上传到/opt目录下,进行解压

shell 复制代码
apache-maven-3.9.6-bin.tar.gz

3.重名名

shell 复制代码
 mv apache-maven-3.9.6 maven-3.9.6

4.添加环境变量

shell 复制代码
vi /etc/profile
export MAVEN_HOME=/opt/maven-3.9.6
export PATH=${MAVEN_HOME}/bin:$PATH

5.环境变量生效

shell 复制代码
source /etc/profile

6.修改maven基本配置

shell 复制代码
vi /opt/maven-3.9.6/conf/settings.xml

maven下的 conf/settings.xml 找到和标签,在其中添加如下内容(镜像加速):

xml 复制代码
<!-- 配置镜像加速 -->
<mirror>
    <id>alimaven</id>
    <name>alimaven</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    <mirrorOf>central</mirrorOf>
</mirror>

配置本地仓库地址

shell 复制代码
<localRepository>/opt/maven-3.9.6/repository</localRepository>

3.安装Jenkins(war包方式安装)

1.安装

1.创建jenkins安装目录进入该目录后下载jenkins

shell 复制代码
mkdir /opt/jenkins && cd /opt/jenkins/

2.下载Jenkins(并且上传)

3.添加启动脚本

在Jenkins安装目录/opt/jenkins下创建jenkins启动脚本

shell 复制代码
vi jenkins.sh

脚本内容如下

sh 复制代码
#!/bin/bash
args=$1
#注意修改jenkinswar包的目录
jenkins_war_path="/opt/jenkins"
#jenkins开放端口
jenkins_http_port="8888"
#java安装路径
java_home="/usr/local/jdk-11.0.18"
function isRuning(){
        local jenkinsPID=`ps -ef|grep jenkins.war|grep -v grep|awk '{print $2}'`
        if [ -z ${jenkinsPID} ];then
                echo "0"
        else
                echo ${jenkinsPID}
        fi
}

#停止jenkins
function stop(){
        local runFlag=$(isRuning)
        if [ ${runFlag} -eq "0" ];then
                echo "Jenkins is already stoped."
        else
                `kill -9 ${runFlag}`
                echo "Stop jenkins success."
        fi
}

#启动jenkins
function start(){
        local runFlag=$(isRuning)
        echo "${runFlag}"
        if [ ${runFlag} -eq "0" ];then
                `${java_home}/bin/java -jar ${jenkins_war_path}/jenkins.war --httpPort=${jenkins_http_port} &` > /dev/null
                if [ $? -eq 0 ];then

                        echo "Start jenkins success."
                        exit
                else
                        echo "Start jenkins fail."
                fi
        else
                echo  "Jenkins is running now."
        fi
}

#重启jenkins
function restart(){
        local runFlag=$(isRuning)
        if [ ${runFlag} -eq "0" ];then
                echo "Jenkins is already stoped."
                exit
        else
                stop
                start
                echo "Restart jenkins success."
        fi
}

#根据输入的参数执行不同的动作
#参数不能为空
if [ -z ${args} ];then
        echo "Arg can not be null."
        exit
#参数个数必须为1个
elif [ $# -ne 1 ];then
        echo "Only one arg is required:start|stop|restart"
#参数为start时启动jenkins
elif  [ ${args} = "start" ];then
        start
#参数为stop时停止jenkins
elif [ ${args} = "stop" ];then
        stop
#参数为restart时重启jenkins
elif [ ${args} = "restart" ];then
        restart
else
        echo "One of following args is required: start|stop|restart"
        exit 0
fi

4.jenkins.sh添加执行权限

shell 复制代码
chmod u+x jenkins.sh

5.启动

shell 复制代码
./jenkins.sh start

6.启动报错

7.解决方案

shell 复制代码
./jenkins.sh stop
yum -y install fontconfig.x86_64

8.再次启动

2.镜像加速

1.jenkins默认镜像地址:https://updates.jenkins.io/update-center.json

2.镜像地址列表

常见的jenkins镜像地址有以下地址

镜像名 镜像地址
清华大学 https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json
华为 https://mirrors.huaweicloud.com/jenkins/updates/update-center.json
xmission http://mirror.xmission.com/jenkins/updates/update-center.json

也可参见官方网站提供的可用镜像地址:http://mirrors.jenkins-ci.org/status.html

3.配置加速

修改jenkins配置(插件站点更新,加速联网)

shell 复制代码
vi /root/.jenkins/hudson.model.UpdateCenter.xml

将XML内的url的值替换为:http://mirror.xmission.com/jenkins/updates/update-center.json

xml 复制代码
<?xml version='1.1' encoding='UTF-8'?>
<sites>
  <site>
    <id>default</id>
    <url>http://mirror.xmission.com/jenkins/updates/update-center.json</url>
  </site>
</sites>

4.官网提供的yum方式安装

1.安装密钥

shell 复制代码
 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-2023.key

2.安装

shell 复制代码
  yum install fontconfig java-17-openjdk
  yum install jenkins

5.访问

1.浏览器访问jenkins,端口8888: http://ip:8888/

2.获取管理员密码

shell 复制代码
cat /root/.jenkins/secrets/initialAdminPassword
相关推荐
客观花絮说14 分钟前
DSC+DW实时+异步搭建部署
运维
x66ccff34 分钟前
【linux】4张卡,坏了1张,怎么办?
linux·运维·服务器
周全全36 分钟前
Elasticsearch 检索优化:停用词的应用
大数据·elasticsearch·jenkins
我命由我123452 小时前
GPIO 理解(基本功能、模拟案例)
linux·运维·服务器·c语言·c++·嵌入式硬件·c#
kka杰2 小时前
Linux 进程3
linux·运维·服务器
华纳云IDC服务商2 小时前
网站服务器怎么计算同时在线人数?
运维·服务器
没有名字的小羊2 小时前
Linux基础命令——账户简单管理
linux·运维·服务器·网络·数据库
smile_life_2 小时前
服务器非法关闭后MySQL服务启动失败
运维·服务器·mysql
kka杰2 小时前
Linux 进程2
linux·运维·服务器
大白菜和MySQL2 小时前
tomcat服务搭建部署ujcms网站
java·linux·运维·服务器·tomcat