当前位置:   article > 正文

jenkins环境搭建_jenkins.sh

jenkins.sh

1.环境说明

keyvalue
环境centos7
jdk版本11
git2.43.0
maven3.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目录下,进行解压

tar -zvxf git-2.43.0.tar.gz
  • 1

3.安装源代码编译环境

yum install -y curl-devel expat-devel openssl-devel zlib-devel gcc-c++ 
yum install -y perl-ExtUtils-MakeMaker automake autoconf libtool make
  • 1
  • 2

3.配置git安装目录

cd git-2.43.0
./configure --prefix=/opt/git
  • 1
  • 2

4.执行git安装命令

make install
  • 1

5.添加环境变量

vi /etc/profile
export GIT_HOME=/opt/git
export PATH=${GIT_HOME}/bin:$PATH 
  • 1
  • 2
  • 3

6.环境变量生效

source /etc/profile
  • 1

3.安装sshpass

 yum install -y sshpass
  • 1

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
  • 1

3.重名名

 mv apache-maven-3.9.6 maven-3.9.6
  • 1

4.添加环境变量

vi /etc/profile
export MAVEN_HOME=/opt/maven-3.9.6
export PATH=${MAVEN_HOME}/bin:$PATH
  • 1
  • 2
  • 3

5.环境变量生效

source /etc/profile
  • 1

6.修改maven基本配置

vi /opt/maven-3.9.6/conf/settings.xml
  • 1

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

配置本地仓库地址

<localRepository>/opt/maven-3.9.6/repository</localRepository>
  • 1

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

1.安装

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

mkdir /opt/jenkins && cd /opt/jenkins/
  • 1

2.下载Jenkins(并且上传)

  • Jenkins安装包下载页:https://www.jenkins.io/download/

  • 各版本war索引页:https://mirrors.jenkins-ci.org/war/

3.添加启动脚本

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

vi jenkins.sh
  • 1

脚本内容如下

#!/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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80

4.jenkins.sh添加执行权限

chmod u+x jenkins.sh
  • 1

5.启动

./jenkins.sh start
  • 1

6.启动报错
在这里插入图片描述
7.解决方案

./jenkins.sh stop
yum -y install fontconfig.x86_64
  • 1
  • 2

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
xmissionhttp://mirror.xmission.com/jenkins/updates/update-center.json

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

3.配置加速

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

vi /root/.jenkins/hudson.model.UpdateCenter.xml
  • 1

将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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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
  • 1
  • 2

2.安装

  yum install fontconfig java-17-openjdk
  yum install jenkins
  • 1
  • 2

5.访问

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

2.获取管理员密码

cat /root/.jenkins/secrets/initialAdminPassword
  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/970293
推荐阅读
相关标签
  

闽ICP备14008679号