jenkins环境搭建
- 1.环境说明
- 2.环境准备
- 1.jdk安装
- 2.安装Git
- 3.安装sshpass
- 4.安装Maven
- 3.安装Jenkins(war包方式安装)
- 1.安装
- 2.镜像加速
- 4.官网提供的yum方式安装
- 5.访问
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方式
- 执行命令:yum install -y git ;
- 验证是否安装成功:git --version
1.通过官网下载git:https://mirrors.edge.kernel.org/pub/software/scm/git/
2.下载的git上传到/opt目录下,进行解压
tar -zvxf git-2.43.0.tar.gz
3.安装源代码编译环境
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安装目录
cd git-2.43.0
./configure --prefix=/opt/git
4.执行git安装命令
make install
5.添加环境变量
vi /etc/profile
export GIT_HOME=/opt/git
export PATH=${GIT_HOME}/bin:$PATH
6.环境变量生效
source /etc/profile
3.安装sshpass
yum install -y sshpass
4.安装Maven
.1.maven下载页面:https://maven.apache.org/download.cgi
2.将下载的压缩包apache-maven-3.9.6-bin.tar.gz上传到/opt目录下,进行解压
apache-maven-3.9.6-bin.tar.gz
3.重名名
mv apache-maven-3.9.6 maven-3.9.6
4.添加环境变量
vi /etc/profile
export MAVEN_HOME=/opt/maven-3.9.6
export PATH=${MAVEN_HOME}/bin:$PATH
5.环境变量生效
source /etc/profile
6.修改maven基本配置
vi /opt/maven-3.9.6/conf/settings.xml
maven下的 conf/settings.xml 找到和标签,在其中添加如下内容(镜像加速):
<!-- 配置镜像加速 -->
<mirror>
<id>alimaven</id>
<name>alimaven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
配置本地仓库地址
<localRepository>/opt/maven-3.9.6/repository</localRepository>
3.安装Jenkins(war包方式安装)
1.安装
1.创建jenkins安装目录进入该目录后下载jenkins
mkdir /opt/jenkins && cd /opt/jenkins/
2.下载Jenkins(并且上传)
-
Jenkins安装包下载页:https://www.jenkins.io/download/
-
各版本war索引页:https://mirrors.jenkins-ci.org/war/
3.添加启动脚本
在Jenkins安装目录/opt/jenkins下创建jenkins启动脚本
vi jenkins.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添加执行权限
chmod u+x jenkins.sh
5.启动
./jenkins.sh start
6.启动报错
7.解决方案
./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配置(插件站点更新,加速联网)
vi /root/.jenkins/hudson.model.UpdateCenter.xml
将XML内的url的值替换为:http://mirror.xmission.com/jenkins/updates/update-center.json
<?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.安装密钥
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.安装
yum install fontconfig java-17-openjdk
yum install jenkins
5.访问
1.浏览器访问jenkins,端口8888: http://ip:8888/
2.获取管理员密码
cat /root/.jenkins/secrets/initialAdminPassword